Satch3000
Satch3000

Reputation: 49422

PHP Variables inside JQuery/Javascript

I have some javascript/jquery code and need to some php into it be the syntax seems to be wrong...

This is what I'm doing:

$.post("myphp.php?something=$phpvariablehere",{ etc....

The result right now is that it's taking $phpvariablehere as a string and not the value of it.

Anyone know the right syntax?

Upvotes: 1

Views: 186

Answers (3)

Shomz
Shomz

Reputation: 37711

Or, for a more readable version (with short tags allowed on the server):

$.post("myphp.php?something=<?= $phpvariablehere ?>"...

Also, your page must be .php, not .js, for this to work with the default settings.

Upvotes: 2

Christopher Bull
Christopher Bull

Reputation: 2629

Use this:

$.post("myphp.php?something=<?php echo $phpvariablehere; ?>",{ etc....

It would be worth while understanding the difference between server-side (PHP) and client-side (Javascript) languages, and how/why they interact with each other. you can't swap between two different languages like you originally attempted - you need to declare a PHP section using these:

<?php ... ?>

or you can insert the variable directly by using:

<?= $phpvariablehere ?>

Upvotes: 4

pedrochaves
pedrochaves

Reputation: 782

Not really a JS question, but here you go:

$.post("myphp.php?something=<?php echo $phpvariablehere ?>"

Upvotes: 4

Related Questions