Sydowh Sydowh - 1 year ago 60
HTML Question

Submit form on same page without reloading and use POST variable

I need to get the value of a post variable from a form and transform this to a PHP variable to use it on the same page without reloading it

Actually I got this :

$(function() {
$("#submit_post").click(function() {
var select = $("select").val();


<form method="post">
<select name="select" id="select">
<option value="1">Test</option>
<input type="submit" id="submit_post" value="Envoyer" onclick="return false;"/>

<div id="result"></div>

When I do :

<?php var_dump($_POST["select"]); ?>

I got : null

But on div result I got : 1


I need to lake this "1" a php variable

Answer Source

Your code runs fine on my server. Maybe you aren't totally clear on the function of the superglobals.

If the "result" div contains "1" after you press the button, then that means process.php is correctly receiving your POST request and echoing back the value of $_POST["select"]. You will get "NULL" if you try to just navigate your browser to process.php, because when you do so you are making a separate request which doesn't contain any POST variables. The superglobal arrays don't persist between different calls to process.php unless you create that functionality using $_SESSION, a DB, or some kind of text/json/xml storage system. The following changes to your PHP will allow you to click your button and then separately navigate to process.php and see your data:

if ($_POST["select"]) {
    $_SESSION["data"] = ($_POST["select"]);

Please correct me if I have made the wrong assumptions and this is not helpful.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download