1

I have an HTML form which i populate from a database. On submit we load a page called "viewgame.php". Now what i want is here to run some scripts to populate some tables with data but how exactly can i pass the variable which i got from the form ex. $_POST['gameNo'] to the other php file though JavaScript?

Below is some of my code JS function

function refreshGameinfo() {
    var load = $.get('gameinfo_sc.php');
    $(".gameinfo").html('Refreshing');
    load.error(function() {
        console.log("Mlkia kaneis");
        $(".gameinfo").html('failed to load');
        // do something here if request failed
    });
    load.success(function(res) {
        console.log("Success");
        $(".gameinfo").html(res);
    });
    load.done(function() {
        console.log("Completed");
    });
}

How can i pass the $POST_['gameNo'] to the gameinfo_sc.php file so that i can get the correct results?

2
  • Use $_GET in your php file Commented Jan 7, 2014 at 9:51
  • Or save the data into a $_SESSION. Commented Jan 7, 2014 at 9:52

3 Answers 3

3

Try this

var load = $.get('gameinfo_sc.php',{gameNo:"1212"});

In your php file you can access it using

$_GET['gameNo']

For post method use

var load = $.post('gameinfo_sc.php',{gameNo:"1212"});

In your php file you can access it using

$_POST['gameNo']
Sign up to request clarification or add additional context in comments.

1 Comment

thanks worked great, var load = $.get('gameinfo_sc.php',{gameNo:"<?php echo $_POST['gameNo']; ?>"}); and used $_GET['gameNo'] to get it.
1

You are trying to post $POST_['gameNo'] to gameinfo_sc.php but $.get isn't the right method for post, its actually for http get. you can also do this by using $.post http://api.jquery.com/jquery.post/

function refreshGameinfo() {
        $.ajax({
            type: "POST",
            url: "gameinfo_sc.php",
            data: {gameNo: data},
            cache: false,
            success: function(html){
                console.log( "Success" );
                $(".gameinfo").html(res);
            },
            error:function(html){
                console.log("Mlkia kaneis");
                $(".gameinfo").html('failed to load');
            }
        });
    }

try this

Comments

0

You can do it like this:

(in html layout):

<input type="hidden" id="gameNo" value="<?=$_POST['gameNo']?>" />

(in js file):

var gameNo = $('#gameNo').val();
var load = $.get('gameinfo_sc.php', {gameNo: gameNo});
....

UPDATE:

If your server doesn't support short open tags, you can write:

<input type="hidden" id="gameNo" value="<?php echo $_POST['gameNo'] ?>" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.