3

I want to convert a javascript variable to a php variable.

This is the jQuery code I use:

$('#add').click(function(){ $.post( 'user.php', {saveidnumber: idnumber}, function(){ console.log('done') } ); }); 

php:

if(isset($_POST['saveidnumber'])){ $saveid = $_POST['saveidnumber']; echo $saveid; } 

It look likes the jQuery work because there are no errors in the console but the php never echo's the $saveid variable. What am I doing wrong?

2
  • 1
    where do u want to see php's echo? Commented Jan 30, 2015 at 12:19
  • you have to recieve it in javascript part.function(res){ console.log(res); } Commented Jan 30, 2015 at 12:23

3 Answers 3

4

Try:

$.post('user.php', {saveidnumber: idnumber}, function(result){ console.log(result) }); 

The variable result is the HTML printed in user.php.

The variable $saveid can be viewed in the console.

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

Comments

1

When you echo $saveid it return as response so you have to pass function argument which catch return value from server side and you can print it explain in example.

$('#add').click(function() { $.post('user.php', {saveidnumber: idnumber}, function(data){ console.log(data); //here return your $saveid; } ); }); 

6 Comments

what's the difference between previous answer?
@KirillPisarev the difference is the person whos answering should explain the reason too.
@KirillPisarev this is not my answer.
@KirillPisarev I answering with explanation with appropriate comment and formatted output so OP can understand easily.
i forgot to close the same page in other browser tab, so here's how your answer looked like....
|
0

I think you misunderstood stateless concept of PHP mixed with ajax asynchronuous requests. It works this way: 1. someone sends a request to get user.php site 2. server gets request and process it 3. server returns requested site and that's it

So when you're sending POST using javascript (jQuery in your case) from within that page it is a separate request with separate response. It implies that you have to handle this response by yourself in javascript. See the example:

$('#add').click(function() { $.post('user.php', {saveidnumber: idnumber}, function(data){ console.log('done'); $('.some-class').text(data); } ); }); 

This will insert a response to any HTML element with class some-class like

<div class="some-class"></div> 

2 Comments

Ah, so it is not posable to save javascript variables to php? The reason I want to save javascript into php is that I finally want to save them to a database.
That's the other thing. I saw echo in your code and thought you just want to output some value. I know what you want to do but you're using the wrong word. You don't want to save js var to php, you want to send the this var to php. To be precise, you want to send the value of this var. In that case you're code from the question is almost correct - it work's but you won't see the echoed value (I explained the reason above). Instead change echo $saveid; to file_put_contents('output.txt', $saveid);. Execute it and look in the output.txt file. ``

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.