0

Am passing a variable to JQuery via the data-pid in a href link in payments.php and using AJAX to pass this variable to pmntPopup.php, however the variable is not being passed on

payments.php

<td class="listingTextLeft">
<a href="" data-pid="<?php echo $row[0] ?>" class="pmntDetail"><?php echo $row[20] ?></a>
</td>
<script>
        $( ".pmntDetail" ).click(function( paymentID ) {
            paymentID.preventDefault();
            paymentID.stopPropagation();
            var pmntid = $(this).data("pid");
            console.log("ID: ", pmntid);
            $.ajax({
                type: "POST",
                url: "pmntPopup.php",
                data: {pmntid : pmntid },
                success:function(data) {
                    console.log(pmntid);
                    $("#pmntDetailPopup").modal({position: ["5%"]});
                }
            });
        });
</script>

The console log in both instances show the correct value for pmntid but when when trying to use POST to retrieve it in pmntPopup.php below I just get the 'Payment Is Not Carried' message.

pmntPopup.php

<?php
    if(isset($_POST['pmntid'])) {
        $pmntid = $_POST['pmntid'];
    } else {
        echo "Payment Is Not Carried";
    }
?>

I've searched this site and from what I can tell this should work, I've probably missed something really basic or doing something really stupid ... or both.

Console POST output:

Console POST Output

Hi @Jay, have already posted a picture of the POST output from the console above, the picture below shows the Popup window output if that's any help:

Popup Post Output

As requested the console response output is shown below: Console response

14
  • Have you watched the request / response in your browser's console window? Any errors? Commented Feb 25, 2015 at 20:54
  • Have you echo ` $_POST['pmntid']` ? Commented Feb 25, 2015 at 20:56
  • No, no errors in the console and it gives the correct pmntid. Have added a picture showing this. @Sulthan, yes have echo'd the $_POST['pmntid'] and it comes up blank. Commented Feb 25, 2015 at 21:11
  • Something is echoing back the '21', what else is in pmntPopup.php? Commented Feb 25, 2015 at 21:19
  • 1
    Your update doesn't provide any more insight. What is in the response tab of the Post? Commented Feb 25, 2015 at 21:50

3 Answers 3

0

I'm not sure what you're doing with the paymentID variable, but this should work for you:

$( ".pmntDetail" ).click(function( ) {
    var pmntid = $(this).data("pid");
    console.log("ID: ", pmntid);
    $.ajax({ ...
Sign up to request clarification or add additional context in comments.

2 Comments

Those line breaks and all that were in there were because the code was not formatted properly @Wignu
Ah thought it might be formatting. Tried the code again without it, gives the following error: PST http://localhost/pmntPopup.php x as if it now cannot find the pmntPopup.php file.
0

Try this, the response of the ajax call will be set to data variable of your success callback function.

payments.php

<td class="listingTextLeft">
<a href="#" data-pid="<?php echo $row[0] ?>" class="pmntDetail"><?php echo $row[20] ?></a>
</td>
<script>
        $( ".pmntDetail" ).click(function( event ) {
            event.preventDefault();
            var pmntid = $(this).data("pid");
            console.log("ID: ", pmntid);
            $.ajax({
                type: "POST",
                url: "/UCM/pmntPopup.php",
                data: {pmntid : pmntid },
                success:function(data) {
                    console.log(data); // YOU WILL RECEIVE THE RESPONSE IN data variable.
                    $("#pmntDetailPopup").modal({position: ["5%"]});
                }
            });
        });
</script>

pmntPopup.php

<?php
    if(isset($_POST['pmntid'])) {
        echo $_POST['pmntid'];
    } else {
        echo "Payment Is Not Carried";
    }
?>

[Also, I have put the complete url of the file, ie., "/UCM/pmntPopup.php" to your ajax url param]

1 Comment

Thanks Rao, but still have the same problem, no pmntid passed through.
0

Try this instead.

<td class="listingTextLeft">
<a href="" data-pid="<?php echo $row[0] ?>" class="pmntDetail"><?php echo $row[20] ?></a>
</td>
<script type="text/javascript">
$( ".pmntDetail" ).click(function(e) {
e.preventDefault();
var pmntid = $(this).data("pid");
console.log("ID: ", pmntid);
var request = $.ajax({
url: "pmntPopup.php",
type: "POST",
data: {pmntid : pmntid},
dataType: "html"
});
request.done(function(msg) {
console.log( msg );
});
request.fail(function(jqXHR, textStatus) {
alert( "Connection error: " + textStatus );
});
});
</script>

pmntPopup.php

<?php
    if(isset($_POST['pmntid'])) {
        $pmntid = $_POST['pmntid'];
         echo $pmntid;
    } else {
        echo "Payment Is Not Carried";
    }
?>

As an alternative solution try jQuery post

$.post( "pmntPopup.php", {pmntid : pmntid})
.done(function( data ) {
alert( "Data Loaded: " + data );
});

9 Comments

Made no difference sorry, variable still not passed through.
@PeterDarmis "Use single quotes on data-index", why?
@Rao based on a working example stackoverflow.com/questions/8046482/…
Yes I tried both, even tried it with single quotes around pid. Still no go unfortunately.
Then as @Rao posts in his answer you should check the path that you post data. I don't know where pmntPopup.php is located in your server if it is in the same folder with payments.php.
|

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.