How does I get my Ajax post call working?

I’m quite new to JS/Ajax and I need to get a Ajax post call to work.

This is the code I’m using:

<form id="my-form" method="POST">
<input type="button" name="serialize" id="serialize" value="Serialize">
<input type="submit" name="save" id="save" value="Save">

if ($_POST['save']){
  echo 'save';
  $ser_string = "optieID[1]=null&optieID[4]=1&optieID[11]=4&optieID[7]=null&optieID[5]=7&optieID[2]=null&optieID[6]=2&optieID[8]=2&optieID[3]=null&optieID[9]=3&optieID[10]=3&optieID[12]=null";
  //$test = urldecode($ser_string);

  $result = array();
  parse_str($ser_string, $result);

  // Loop through results
  foreach($result["optieID"] as $child_id=>$parent_id)
    $wpdb->update('option_connection', array('parent_id'=>$parent_id), array('child_id'=>$child_id));

As you can see, I have currently a hard input code working. But I now need to change this to the Ajax post call.

So when I click on the submit save button: I must be able to print_r($_POST['input']); and use that post input instead of the hard $ser_string. I think it must be something like this: $set_string = $_POST['input'];

But if I add the below Javascript code to my file, it won’t work and I don’t seem to get it to work.

  var input = $('.sortable').nestedSortable('serialize');
    type: "POST",
    url: this.href,
    data: 'input': input,

I hope someone can take a look and help me in the right direction.
Thanks in advance

Read more here: How does I get my Ajax post call working?

Leave a Reply

Your email address will not be published. Required fields are marked *