0

I'm trying to pass a Jquery variable (items) to a php variable $itemdestination on the same page, when pushing the button .bid, which is a submit button:

<script>
    $(".bid").click(function () {
        var items = $(this).closest("tr")   // Finds the closest row <tr> 
            .find(".nr")     // Gets a descendent with class="nr"
            .text();         // Retrieves the text within <td>
        var itemdestination = $(this).closest("tr")   // Finds the closest row <tr> 
            .find(".no")     // Gets a descendent with class="nr"
            .text();

        $("#prøve").append(items, itemdestination);       // Outputs the answer

        $.ajax({
            url: 'Jobs.php',
            type: 'POST',
            data: {items: items}
        });
    });
</script>

<?php

if (isset($_POST['button_pressed'])) {

    $itemdestination = $_POST['items'];

    //Email information
    $admin_email = "[email protected]";
    $email = $userName;
    $subject = "Bid registered on your cargo";
    $comment = $itemdestination;

    echo $admin_email;
    echo $comment;

  //send email
  mail($email, "$subject", $comment, "From:" . $admin_email);

  }
?>
 <script>
alert("Your bid is registered");
</script>
<?php

}
?>

But nothing is passed with the method i'm using. What am I doing wrong??

3 Answers 3

1

Isn't this:

if(isset($_POST['button_pressed']))

should be:

if(isset($_POST['items']))

Seems that you are checking for wrong posted variable.

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

3 Comments

It does work according to passing the variable. But then the alert is never coming, when the button is pushed?
@NicolajNyegaard i don't see any alert(). in your code.
sorry, that's because I forgot the rest of the code. It is edited in the original post know.
1

Remove the following if statement and try:

if(isset($_POST['button_pressed'])) 

2 Comments

But $_POST['items']; is still PHP variable ,please read OP's Question.
$_POST['items'] is not mentioned in the question. The word "with" suggests the problem is with the caller - the AJAX request. Perhaps you can clarify (edit) the question? Or provide an interpretation?
1

You need to set a post field for button_pressed in you js (in order for your php to run), then actually do something with the returned data:

$.ajax({
    url: 'Jobs.php',
    type: 'POST',
    data: {items: items, button_pressed: true},
    success: function(data){
        console.log(data);//do something with the data returned
    }
});

EDIT if this bound to the click event on a submit button, you must stop the form submitting normally with event.preventDefault :

$(".bid").click(function (ev) {
    ev.preventDefault();
    //the rest of your code here

3 Comments

This does work, but then the e-mail is sent out twice. The first with the passed varaible, and the second mail is just blank?
@NicolajNyegaard then the script is being called twice, once from ajax, one from somewhere else. is .bid a submit button by any chance? I will edit with a solution if so
yes .bid is a submit button whit the bid as the id.

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.