0

I have modified the code

to POST prodID to ProductsList.php

  // its a dynamically generated drop menu
   while($rowmnu2=mysql_fetch_assoc($resulmnusub2))
    {
       echo '<li><a  id="'.$rowmnu2['liid'].'" href="#" onclick="passto(this.id)">'.$rowmnu2['title'].'</a></li>
';
    }

and here is my ajax function :

function passto(val){
//window.location.href="ProductsList.php?idd=" + val;
 $.ajax({
    url: 'ProductsList.php', 
    type: "POST",
    data: ({prodID: val}),
    success: function(data){
        //or if the data is JSON
        window.location.href="ProductsList.php";
    }
}); 
}

the passed element to the function is an integer

in the ProductsList.php I have

<?php 

if(!$_POST['prodID']) die("There is no such product!");

echo $_POST['prodID'];


?>

and I get There is no such product! while there should be an INT #

why is that ?

any one knows? all the bellow suggestions are not responding correctly

3
  • The ' onclick="passto(this)"' is sending a whole DOM object to the function. You will need to drill down to the actual piece of data that you are wanting. Commented Sep 8, 2013 at 7:44
  • Try Replace data: {prodID:val}, to data: {prodID:val.id}, Commented Sep 8, 2013 at 7:44
  • @jeff no it doesn't, I get just the ID number when I alert the val Commented Sep 8, 2013 at 8:52

3 Answers 3

2
$(document).ready(function() {
    $("a").click(function(event) {
        myid = $(this).attr('id');

        $.ajax({

        type: "POST",
            url: "ProductsList.php",
            data: {prodID: myid},
        dataType: "json",
            complete:function(){


            window.location("ProductsList.php");
        }

    });
});
});
Sign up to request clarification or add additional context in comments.

Comments

1

if you want to POST id , you can change:

...onclick="passto(this)"...

to

...onclick="passto(this.id)"...

Comments

0

That behavior is normal because you are requesting ProductsList.php twice. the first time with an AJAX request using $.ajax. for that time the id is sent correctly. The problem is that you request ProductsList.php again just after AJAX complete using window.location.href="ProductsList.php"; without sending anything. So the result is as expected, a page printing There is no such product!

You can fix the problem by replacing window.location.href="ProductsList.php"; by this one :

$('body').html(data);

or any other instruction to use properly the returned data.

You can either use my edited code or just edit yours :

echo '<li ><a  id="'.$rowmnu2['link'].'" href="#">'.$rowmnu2['title'].'</a></li>';

JS part :

$('a').click(function() {

    var val = $( this ).attr('id');

    $.ajax({
        type: "POST",
        url: "ProductsList.php",
        data: {prodID:val},
        complete:function(){
            $('body').html(data);  
        } 
   });
});

2 Comments

i have added complete:function(msg){ alert (msg) it alerts (undefined)
this one works success :function(msg){ alert( "Data Saved: " + msg ); pity that I am not able to vote your answer up. but thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.