0

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 }"

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
function postID( id ){
    $.ajax({
      type: "POST",
      url: "edit-homepage.php",
      data: { 'id' : id },      
      success: function(data) {
          alert("ID: <?PHP echo $_POST['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.

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.

Thanks!

2
Contributors
7
Replies
22
Views
3 Years
Discussion Span
Last Post by pritaeas
0

The problem is, I want to use PHP. I want, instead of refreshing the page and having the link being ../edit-homepage.php?id=1 (this is what i had initially), I want to dynamically pass the ID when someone clicks the link to PHP $_POST['id'] on the same page without reloading and populate a form with data from a database. I may not be doing it correctly, but in this case, can anyone tell me how to do this?

0

No, it does not work. When I alert data, it actually returns my entire website in HTML, including all the scripts etc. It is not actually returning the data I specified....

0

This is what I want to happen.

<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>

when editSlide(id); is called it will pass the ID to PHP via $_POST['id']; and populate a form with data from a database (without refreshing the page):

<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>
0

The PHP code above should go in a separate PHP file, which you can then call with the $.post

In that PHP file echo your data with json_encode so it's easy to use.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.