|
-
May 9th, 2007, 02:05 PM
#1
Passing Variables (JS --> PHP)
Is there any way to pass variables from JS to PHP?
I thought of a couple ways myself...
1. Create a window with a url like this: http://www.site.com/JS2PHP.php?varlist=Name:JSinputname|Age:50|Exit:Yes, then use PHP to split the "|" then split the ":" and then after 1 second, automatically close the popup since the variables are passed... then what? Can you do this with PHP 5?
PHP Code:
<?PHP
// JS2PHP.php
$VarList = $_GET["varlist"];
?>
Will that variable be accesible from this?
PHP Code:
<?PHP
// EchoVar.php
echo $VarList;
?>
2. using this
HTML Code:
<SCRIPT>
var JSVAR = nameinput.value;
document.write("<?PHP $JS_Var = \"" + JSVAR + "\"; ?>");
</SCRIPT>
-
May 9th, 2007, 03:59 PM
#2
Re: Passing Variables (JS --> PHP)
There are only two ways to pass a JavaScript variable to PHP. You can implement these with AJAX or by loading a new page.
1. Using $_GET.
2. Using $_POST.
Note: Your second option will not work because the server-side code (php) would already have been executed before the JavaScript (client-side) could write it using document.write. Since PHP is server-side it cannot be written dynamically like JavaScript.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
May 10th, 2007, 02:47 AM
#3
Re: Passing Variables (JS --> PHP)
-
May 17th, 2007, 11:41 AM
#4
Re: Passing Variables (JS --> PHP)
Ok thanks...but can you access $num1 in script1.php from script2.php?
Do you have to make it global?
-
May 17th, 2007, 11:48 AM
#5
Re: Passing Variables (JS --> PHP)
In order to access PHP variables between PHP scripts you can do a number of things.
1. Work with sessions and make it a session variable.
2. Store it in a cookie.
3. Use include(), include_once(), require(), or require_once().
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|