简体   繁体   中英

Ajax is not loading php file

Here is my ajax code:

function saveChanges(object){ 
  var id = object.value;
  alert('HI'+id);
  $.ajax({
    url : 'ajaxChangeProfile.php',
    type: 'POST',
    data: {'id':id}
  });
}

Here is where it is being called

<select onchange="saveChanges(this);" name="changeChar" id="changeChar">

So when I change the value of the select box, I get the alert which contains the correct message and id.

At the top of the php file I am trying to send to I have a query which will add a number to a test table in my database, but this is not being added.

All I need to do in the php file is change 2 session variables and make 1 mySQL query.

Thanks

EDIT 1:

function saveChanges(object){ 

  var id = object.value;
  alert('HI'+id);
  $.ajax({
    url : 'ajaxChangeProfile.php',
    type: 'POST',
    data: {'id':id},
    success: function() {
      alert('WORKING');
    }
  });
}

I tried adding the done() callback, but it stopped the alert working, so I wasn't sure what to do, with the success added the message still displays but still doesn't load the page. Is it okay only having an alert in the success or do I need something else?

EDIT 2:

function saveChanges(object){ 

  var id = object.value;
  alert('HI'+id);
  $.ajax({
    url : 'ajaxChangeProfile.php',
    type: 'POST',
    data: {'id':id},
    success: function(data) {
      alert(data);
    }
  });
}

I have made the changed suggested by the answerer, still the same issue, the first alert is being shown, with the correct id, but then nothing on the php file is happening.

in your success function you aren't returning the data. try adding the error function as well.

success: function(data){
    alert(data);
},
error: function(xhr, status, error){
    alert(error);
}

use Jquery Onchange event

<select name="changeChar" id="changeChar"></select>

In javascript

$(document).ready(function(){
    $('#changeChar').change.(function(){
        var id = $(this).val();
        alert('HI'+id);
        var datastring = 'id='+id;
        $.ajax({
           url : 'ajaxChangeProfile.php',
           type: 'POST',
           data: datastring,
           success: function(data) {
              alert(data);
           }
        });    
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM