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=""></script>
function postID( id ){
      type: "POST",
      url: "edit-homepage.php",
      data: { 'id' : id },      
      success: function(data) {
          alert("ID: <?PHP echo $_POST['id']; ?>");

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.


Recommended Answers

All 7 Replies

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?

You are passing it. data is what the page returns. So perhaps you want to alert that.

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

It shows everything that edit-homepage.php outputs. So perhaps you want something else to happen.

This is what I want to happen.

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


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

function editSlide($id) {

        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']; ?>";

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.