Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to convert my javascript variable to php variable. I can get the value of "1" when i click the link but i want to echo it using PHP or i want to store it on a PHP variable.

this is my Javascript and PHP code on the same page.

<?php $userid = 1; ?>

<a href="#" onclick="sendEmail(<?php echo $userid; ?>)" > Send Mail </a>

<script type="text/javascript">
function sendEmail(userid){
    var sendID = userid;
        $(document).ready(function(){
                $.ajax({
                    type: "POST",
                    url: "ajax.php", 
                    data: { toID: sendID },
                        dataType: 'json',
                        cache: false,
                        success: function( toID ){
                        alert( toID ); }
                });
        });
}

<?php

$userid = $_POST["toID"];
echo $userid;

?></script>

no display when i echo it.

thanks.

share|improve this question
1  
Maybe some should point out that js is run on the client and php on the server. –  Hyperboreus 2 days ago
add comment

put on hold as off-topic by Ed Cottrell, Mike W, Łukasz L., Alexis Pigeon, Paul Lammertsma 2 days ago

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist" – Ed Cottrell, Łukasz L., Alexis Pigeon, Paul Lammertsma
If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers

You can't do this, because your ajax request go to another request, and your current php file can't get it.

yourFile.php -> Ajax Request -> yourFile.php
|                               |
|                               |-- here your $_POST['todID'] has the value, but it's other thread
|-- here your $_POST['toID'] is empty

UPDATE 1: You can use success callback to show results or do any.

success: function( data ) {
  alert( data ); //<--- this have the result of your ajax request
  javaScriptVar = data;
}

UPDATE 2: If you need send email in the same file that shows the form, you need put at head:

<?php
if(isset($_POST['toID'])) {
 sendmail($_POST['toID'], "subject", "body");
}
?>
share|improve this answer
 
what should i do? –  user3109628 2 days ago
 
I updated my question –  Ignacio Ocampo 2 days ago
 
what if i will put this code on sendEmail.php <?php $userid = $_POST["toID"]; echo $userid; ?> –  user3109628 2 days ago
 
You can't call php functions within html code, so, '<a href="#" onclick="sendEmail(<?php echo $userid; ?>)" > Send Mail </a>' won't work, you need... i will update my answer... –  Ignacio Ocampo 2 days ago
add comment

The ajax request returns the results of echo $userid; to your javascript; it won't display it on the current page, because it's a different request.

share|improve this answer
 
what should i do? –  user3109628 2 days ago
 
What you have described is impossible; the results of the Ajax call will never be visible to PHP code in the same file on the same request. The user's request and the Ajax request are two separate things. –  Ed Cottrell 2 days ago
add comment

According to your code you are passing a userid to ajax request and getting another userID, Do one thing to achieve that:

<input type="hidden" name="userid" id="userid" value="<?php echo $userid; ?>">
<a href="#" onclick="sendEmail()" > Send Mail </a>

and in you js code:

function sendEmail(){
var sendID = $('#userid');
    $(document).ready(function(){
            $.ajax({
                type: "POST",
                url: "ajax.php", 
                data: { toID: sendID },
                    dataType: 'json',
                    cache: false,
                    success: function( toID ){
                        //alert( toID ); 
                        // update the userID
                        sendID(toID);
                    }
            });
    });
}
share|improve this answer
 
what will happen on my PHP code where will i echo it? on the ajax.php? if yes, can i put the mail function on ajax.php? –  user3109628 2 days ago
 
@user3109628 where is your ajax.php file? For what you are using $userid? –  jogesh_pi 2 days ago
 
cause you use url: "ajax.php" so im wondering what's that for.. im using $userid to query a user.. –  user3109628 2 days ago
 
why if i alert var sendID it displays [object Object]? –  user3109628 2 days ago
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.