0

How to use the a variable that is posted from page1 to page2 from jquery.I am using the following scenario

page1.php

    if ( this.checked ) { 
            $.ajax({
                url: 'page2.php',
                type: "POST",
                data: ({name: 145}),
                success: function(data){
               //$("#result").html(data);
                }
            });        

        }

This is not working why,if i separatly opens a page2.php and wants further usage of the post data

    Page2.php
    <?php
    $userAnswer = $_POST['name'];    
    if ($_POST['submit']){
         echo $userAnswer; 
    }       

    ?>
    <form method="post" action="page2.php">
<input type="submit" value="submit" name="submit" />
</form>

if i open page2.php and presses the submit button,the posted value from page1 is not displaying

1
  • Do var_dump($_POST) to see the contents. If the page reloads then there's some Javascript error, try debugging it (Ctrl + Shift + J). Commented Dec 21, 2012 at 19:29

3 Answers 3

1

You aren't sending the "submit" data.

if ( this.checked ) { 
    $.ajax({
        url: 'page2.php',
        type: "POST",
        data: ({name: 145, submit: "submit"}), // note the submit property
        success: function(data){
            //$("#result").html(data);
        }
    });        
}

I just noticed this:

if i open page2.php and presses the submit button,the posted value from page1 is not displaying

That will never happen. The value that you post from an ajax request will not persist to a second request coming from a different page. You want to store that in some sort of persistant storage (e.g. a database), not a variable that will be lost once the request is finished.

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

5 Comments

"Not working dear" is not useful feedback. Please explain what is not working or what the outcome was.
any other alternative i just need to store data temporarily
try using php sessions? so the data is kept server-side between requests
so if i need to display the data,how i do,echo is not working??
Start a session by calling sesion_start() at the beginning of your scripts. Then assign any variable inside of it, e.g. $_SESSION['my_variable'] = 'some value';. You can now call that variable anywhere with echo $_SESSION['my_variable']. You can also see the $_SESSION contents by calling print_r($_SESSION);. Hope this helps
0

Why don't you use ".load()" function instead of post ?

$('#resultat').load('page2.php',{name: 145});

Comments

0
if ( this.checked ) {

$.post("page2.php", {name1: variable1, name2: variable2, ...}, function (result) {

});
}

page2.php

$storeVariable1 = $_POST['name1'];
$storeVariable2 = $_POST['name2'];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.