I refer to this question: Javascript value to PHP with Jquery

I tried with below code in file named a.php:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
 var ms = 9000;
function test()
{
    $.ajax({ url: "a.php",
         data: {"test":ms},
         type: 'get',
         success: function(output) {
                      $('#testing').html(output);
                  }
    });
}
test();
</script>
<?php
$ms = $_GET["test"];
echo "I am getting below value:";
echo $ms;
?>

Then I point browser to http://localhost/learn/a.php but got error message & value of $ms is not shown as expected:

( ! ) Notice: Undefined index: test in C:\wamp\www\learn\a.php on line 17

$ms = $_GET["test"]; <-- The line 17 in a.php

I tried another simpler code (b.php) below:

<script src="js/jquery.min.js"></script>
<script type="text/javascript">
 var ms = 3000;
 $.get("http://localhost/learn/b.php", { "test": ms } );
</script>
<?php
$ms = $_GET["test"];
echo $ms;
?>

Then I point browser to http://localhost/learn/b.php but got similar error message & no value of $ms displayed:

( ! ) Notice: Undefined index: test in C:\wamp\www\learn\b.php on line 7 Below is code of line 7 $ms = $_GET["test"];

Please advice. Thanks.

share|improve this question

33% accept rate
feedback

4 Answers

up vote 3 down vote accepted

OK, looking at this code:

<script src="js/jquery.min.js"></script>
<script type="text/javascript">
 var ms = 3000;
 $.get("http://localhost/learn/b.php", { "test": ms } );
</script>
<?php
$ms = $_GET["test"];
echo $ms;
?>

I'm presuming that this is all in one file. You have two bits of code in two different languages that are interpreted in different places. First, you have the Javascript at the top. This is not interpreted by your server. It is returned to the browser just as HTML is.

Later, you have a piece of PHP. We're still on the server, and haven't sent anything to the browser yet. You look for a $_GET['test'] value. Your URL was http://localhost/learn/b.php: plainly there are no GET values in that URL, hence the error.

When your code is sent to the browser, the browser sees the line $.get and does an AJAX request. This is another HTTP request. It does not modify the original request, so it doesn't mitigate the error you received above. With this request, your browser will send http://localhost/learn/b.php?test=3000 to the server, and there won't be an error. However, because you're not doing anything with the response, you aren't seeing the effects of this second request.

share|improve this answer
Thanks for excellent explanation. But beside GET, POST, is there any other way to pass js values to PHP? Asking so because I am trying a pull some data returned by API "example.com/jsapi";. Currently, data is displayed on website using <div id="panel"></div> in html and document.getElementById("panel") in js. I want to get text inside "panel" into PHP variables then inserted into MySQL. Any advice or reference to relevant docs. – learnJQueryUI May 23 '11 at 10:13
3  
@Steve No. PHP is server-side, JS is client-side. The way these two communicate is HTTP, i.e. GET/POST. – lonesomeday May 23 '11 at 10:15
you said "However, because you're not doing anything with the response, you aren't seeing the effects of this second request." Can you provide the code to see effects of this second request? – learnJQueryUI May 23 '11 at 15:49
Use a callback function. I'd read a good tutorial on jQuery and AJAX before you go any further, rather than relying on copy-and-paste code. Have a look at jqfundamentals. – lonesomeday May 23 '11 at 18:17
feedback

It looks like you're confusing where you should place your Javascript and PHP. Your PHP file should simply have the logic of receiving and using the Javascript variable, not any Javascript itself. So essentially you need to split this logic.

In your HTML file:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script>
$(function() {
    var ms = 3000;

    $.ajax({
        url: 'a.php',
        data: { test: ms },
        success: function(response) {
            $('#testing').html(response);
        }
    });
});
</script>

In your PHP file (a.php);

<?php
$ms = $_GET['ms'];
echo 'I am getting the value: ' . $ms;
?>
share|improve this answer
Can you elaborate? how to name html file, or put html code into a.php? And how call from browser? using http://../a.php give error. if http://../a.php?test=100, it returns 100, not 3000 as expected. – learnJQueryUI May 23 '11 at 15:52
feedback

When you make the request with XHR, you don't simply access the page with a new request and the value is still there (HTTP is stateless).

You need to do something in the callback with jQuery.

share|improve this answer
it sounds right! Can you provide additional code so that a.php can work as expected. – learnJQueryUI May 23 '11 at 15:55
feedback

Perhaps with this url, you need to precise the GET parameter "test" in the URL

http://localhost/learn/a.php?test=hello

share|improve this answer
wow, superfast, thanks. Does this mean GET or POST must be used to pass js variable to php, or any other way around? – learnJQueryUI May 23 '11 at 10:02
1  
I guess yes, in your case I think you miss a form to call the test javascript function then It will automatically call the a.php script with the GET parameter already set – krifur May 23 '11 at 10:09
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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