0

When users fill values in node form input fields I set cookies of those values with help of javascript code, example :

    var date = new Date();
    date.setTime(date.getTime() + (60 * 60 * 1000));

 $('#edit-field-topic-und').on('change', function() {
            var section =  $(this).find("option:selected").val();
            console.log(section);
            if(section){
                $.cookie("section", section, { expires: date });
            }
        })

        $('#edit-field-topic2-und').on("change", function () {
            var topic = $(this).val();
            console.log(topic);
            if(topic){
                $.cookie("topic", topic, { expires: date });
            }
        });

        $('#edit-field-language-und').on('change', function() {
            var language =  $(this).find("option:selected").val();
            console.log(language);
            if(language){
                $.cookie("language", language, { expires: date });
            }
        })

If user does not submits from and next time come to node add form page, I set default values in from with help of form alter:

case 'link_node_form':
      /*show default values from cookie*/
      if (empty($form['nid']['#value'])) {



        if(isset($_COOKIE['section'])) {
          $form['field_topic']['und']['#default_value'] = $_COOKIE['section'];
        }

        if(isset($_COOKIE['topic'])) {
          $form['field_topic2']['und']['#default_value'] = $_COOKIE['topic'] ;
        }

        if(isset($_COOKIE['language'])) {
          $form['field_language']['und']['#default_value'] = $_COOKIE['language'];
        }


      // clear cookies after from submit
        $form['#submit'][] = 'custom_overrides_link_node_form_submit';


      }

Now when user submit form and node got created successfully cookie is not clearing or the cookies got values of last submitted node.

Please suggest how to clear / unset cookies when form get submitted.

function custom_overrides_link_node_form_submit($form, &$form_state) {



  if(isset($_COOKIE['section'])) {

    unset($_COOKIE['section']);
  }

  if(isset($_COOKIE['topic'])) {

    unset($_COOKIE['topic']);
  }

  if(isset($_COOKIE['language'])) {

    unset($_COOKIE['language']);
  }


}
0

By setting it to one hour ago time with empty value I managed to make it working according to my requirements.

function custom_overrides_link_node_form_submit($form, &$form_state) {



  if(isset($_COOKIE['section'])) {

    unset($_COOKIE['section']);
    setcookie("section", '', time() - 3600);
  }

  if(isset($_COOKIE['topic'])) {

    unset($_COOKIE['topic']);
     setcookie("topic", '', time() - 3600);
  }

  if(isset($_COOKIE['language'])) {
     unset($_COOKIE['language']);
     setcookie("language", '', time() - 3600);
  }


}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.