Ajax POST没有发布到当前页面的onclick

Alright so this has been bugging me for a long time now... I have tried everything but I cant get it to work!

So what I want to have is a link that acts as a button, and once you click it, it POSTs an ID number of the button in the form "{ 'id' : id }"

edit-homepage.php:

<script>
$(function() { // document ready
   $('a.inactive').on('click', function(event) {
     event.preventDefault(); // instad of return false

     var id = $(this).data('id');

     // use $.post shorthand instead of $.ajax
     $.post('edit-homepage.php', {id: id}, function(response) {


       // after you get response from server
       editSlide(id);
     });

   });
 });
</script>

The a href button is created using PHP and I want it to call the ajax function postID( id ) which will post the id so that later I can populate a form via PHP using the posted id.

edit-homepage.php:

echo '<li><a class="inactive" id="slide-'.$info["id"].
'" onClick="postID('.$info["id"].'); editSlide('.$info["id"].'); return false;">'
.'<img src="../images/'.$info["img"].'" width="175"/><p>Edit Slide '
. $info["id"] .'</p></a></li>';

Currently, when I click the link, it opens the alert but it is EMPTY or Undefined. It is supposed to display "ID: 1" for example if the link clicked has a ID of 1.

edit-homepage.php:

    <script>
function editSlide($id) {
        <?PHP

        if (isset ($_POST['id'])) {
            echo "alert('success!2');";
        }$id = !empty($_POST['id']) ? $_POST['id'] : '';
        $data = mysql_query("SELECT * FROM slider WHERE id='$id'") or die(mysql_error()); 
        $info = mysql_fetch_array( $data );?>
        document.getElementById("edit-slide-id").innerHTML="Edit Slide #"+$id;
        document.getElementById("edit-form").style.display = "block";
        document.getElementById("short-title").value="<?PHP echo $info['s_title']; ?>";
}
</script>

Thanks!

With jquery, you don't need to use attributes to attach events, like that:

 $(function() { // document ready
   $('a.inactive').on('click', function(event) {
     event.preventDefault(); // instad of return false

     var id = $(this).data('id');

     // use $.post shorthand instead of $.ajax
     $.post('edit-homepage.php', {id: id}, function(response) {
       alert('ID:' + response);

       // after you get response from server
       editSlide(id);
     });

   });
 });

As of server side, try replacing raw

<?PHP echo $_POST['id']; ?>

With

<?php echo !empty($_POST['id']) ? $_POST['id'] : '' ?>

You likely get notice about Undefined index id, which breaks javascript if there is no post data.

UPDATE

edit-homepage.php shold be separated something like that:

if(!empty($_POST)) {
 // here you process your post data and return
 // only wenever you want to pass to script 
 // not all the html
} else {
 // here you output html and scripts, but don't do request processing
}

You should always remember, that your HTML rendering must always be separated from your logic. It is better to put views in separate files from logic, though it is not required, it is much easier to debug and maintain.

You can not include PHP code that is supposedly to run after the ajax call. The PHP code will be run only to generate the page. Anything you want to include in alert should be provided in the ajax response, in your case the data variable.

You need to use alert('ID: ' + id).

The $_POST['id'] part of the script does not react to the AJAX request. It is whatever the $_POST['id'] value is when the script is output to the browser (i.e. when the page is first loaded).

You will see this if you view the source.

alert ("ID:"+data);

then only you will get response

or

alert("ID"+id);

this will alert the id passes to function

http://jsfiddle.net/U54ME/

$(".checkthisclass").click(function() {
   $.ajax({
      type: "POST",
      url: "edit-homepage.php",
      data: { 'id' : $(this).attr("slideid"); },      
      success: function(data) {
          alert(data);
      }
      });
} 
});

--

<ul>
<li><a class="inactive checkthisclass" id="slide-5" slideid = "5" ><img src="http://blog.entelo.com/wp-content/uploads/2013/04/stackoverflow-logo.png" width="175"/><p>Edit Slide 5</p></a></li>
</ul>