getting Javascript variable into PHP variables?

Started by jrcarr, September 22, 2004, 09:28:06 PM

Previous topic - Next topic

jrcarr

How do you go about getting the contents of Javascript variables into PHP variables and visa verse? Example code would help.  Thanks
Jack Carr

[Unknown]

Javascript to PHP:

'index.php?var=' + variable;

PHP to Javascript:

echo 'var test = "', $variable, '";';

-[Unknown]

jrcarr

What if the Javascript and PHP are all in the same file or the Javascript is includeded into the PHP page.  I'm trying to take a Javascript variable and put it into a PHP variable on the same page.  Thanks

Jack
Jack Carr

[Unknown]

Quote from: jrcarr on September 22, 2004, 10:08:59 PM
What if the Javascript and PHP are all in the same file or the Javascript is includeded into the PHP page.  I'm trying to take a Javascript variable and put it into a PHP variable on the same page.  Thanks

You can't.  Javascript is AFTER PHP is done and over with.  They don't happen at the same time.

-[Unknown]

jrcarr

Ok, that makes sense and I wasn't thinking about the Server side vs Client side scripting.  So what about calling a php function and passing the javascript variable to it?

Jack
Jack Carr

[Unknown]

Well, see, here's how it works.

Server -> Parse PHP -> Get HTML -> Client (no PHP anymore) -> Parse JavaScript.

So, the JavaScript cannot interact directly with the PHP on the same page; it's gone.  If you go to View -> Source, and you don't see it... neither can JavaScript.

-[Unknown]

jrcarr

So then the only option is to either call a new page or even the same page passing the javascript variable like you suggested your first post:

Quote
Javascript to PHP:

'index.php?var=' + variable;

Thanks [Unknown]

Jack
Jack Carr

Irontooth

I have a possible solution for you, it worked for me, but I'm not sure it'll work for you. I have an extensive form created in HTML, that passes values from PHP. Many of the values are hidden values. I also have drop-down lists that I wanted to add to the PHP variables in the hidden input values of the form.

Since the PHP variables needed to be added to the values of the drop-down list, and I didn't want the form to submit every time the user selected an option, I used javacript on the drop-down list to assign a value to the hidden input, and had the php variable assigned in the javascript. If that doesn't make sense, I'll give you an example:


<?php
$phpvar = "whatever is in your PHP variable";
?>
<form name="My_form" method="post">
<select name="My_select" onChange="document.My_form.hidden_form_element.value = '<?=$phpvar;?>' + hxxp:this.form.my [nonactive]_select.value;>
<option value="1">Number 1</option>
<input type="hidden" name="hidden_form_element" value="">
<input type="submit" name="submit">
</form>


When the form is submitted the hidden input will contain a concatenated variable. But will then be passed to whatever page the form action opens. If this is not quite what you were looking for, playing around with the javascript with the same idea might help. However, until a form is submitted, variables in PHP will not be updated. You can put a javascript variable into a form and submit it to a page where it's taken into a PHP variable.

I hope this helped.

Advertisement: