0

So I am using the following answer I've gotten from another question,

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function doSomething() {
    $.get("somepage.php");
    return false;
}
</script>

<a href="#" onclick="doSomething();">Click Me!</a>

I'm just wondering how I can pass a couple more variables from the page this code is on to the somepage.php (the page that I am calling).

Also, once that .php page is ran, how can I then use some of the data that it generates back on the page that I ran this code from (basically to update the user that it has been ran by updating a field possibly).

It'd be great if someone could at least tell me how I can pass php variables over to the other .php page.

By the way, I know there is a similar question here: Run PHP code when user clicks link and pass variables

That also has an answer, but I am trying to use the code that I provided, since it's very simplistic.

2 Answers 2

2

You want an ajax post call:

//Inside your function 
var someVar = <?php echo $variable ?>;
var anotherVar = <?php echo $newVar ?>;

$.post("somepage.php", {firstParam : someVar, secondParam : anotherVar}, function(data) {
    //this is your response data from serv
    console.log(data);
});

You ccan access that data you sent then:

$paramOne = $_POST['firstParam'];
$paramTwo = $_POST['secondParam'];
echo "some response";

And your data from above will be "some response". In a nutshell;

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

15 Comments

Sorry, I'm quite a newbie in Ajax/jQuery. The first snipped you provided would call the .php page and then the firstParam : someVar part would be the variable that I want to pass? Now, can I just add the php value there? I have a variable in php: `<?php echo $myVariable; ?> that I need to pass. I actually need to pass multiple variables. So how would I go about doing that?
and how would I use them in my "somepage.php"?
Why would you be passing php generated variables back to the serv, they're already there, and unless they've been changed somewhere else, shouldn't they already be what you need?
If I use something like include or require in my somepage.php file it will run the php code on the included file again, for me to use the variable. Would that be the only way of going about it?
I see, one second, updating answer.
|
0

You can pass variables to php page using a POST.

here a example:

Pass data from jQuery to PHP for an ajax post

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.