Page 1 of 1
Javascript variable in Php
Posted: Wed Mar 03, 2010 11:39 am
by imran.rajani
Hi Friends,
I don't know if this request has to be posted on JavaScript forum or this forum is ok.
I've drop down menu dynamically generated by php while.
Code: Select all
<select name="sel_ship" id = "sel_ship" onchange="getShip();">
<option value="<?php echo $rowShip['hs_id'];?>"><?php echo $rowShip['hs_optname']; ?></option></select>
Drop down menu is working fine.
Every option will have value from 1 to 10.
I needed to have selected option value in $_SESSION['shipid'] upon any change on drop down menu so i used JavaScript code.
I wrote below but its not working.
Code: Select all
function getShip() {
var shipId;
shipId = document.getElementById("sel_ship").value;
<?php $_SESSION['shipid']=$_GET['shipId'];?>
}
If i replace $_GET['shipId'] with some static value like "2", it works but not with $_GET[].
JavaScript variable shipId is also working.
I need help because already tried a lot but failed.
Thanks in advance.
Regards
Re: Javascript variable in Php
Posted: Wed Mar 03, 2010 2:09 pm
by davex
Hi,
Ok I
think I see what you are getting at and your problem.
The PHP code is executed when the page is called and only then. This will evaluate your PHP line in the JS function when the page is created and sent to the browser.
When the actual getShip() JavaScript function is called the PHP will have already written the output to whatever
Code: Select all
$_SESSION['shipid']=$_GET['shipId'];
was when the
page was called - this will not change.
You can't set a PHP session variable from within JavaScript without doing some sort of AJAX-style call back to a script.
I think if you have a look at the browser source for the page you will see what I mean - the PHP line will just evaluate to something which will remain the same regardless of the JavaScript action.
Hope that is clear - sorry if I have missed what you are trying to do.
Regards,
Dave.
Re: Javascript variable in Php
Posted: Wed Mar 03, 2010 2:27 pm
by imran.rajani
Dave,
Is there any alternate solution except AJAX which'll be difficult for me because i'm not even intermediate in JavaScript.
I checked source in browser and found line
Code: Select all
<b>Notice</b>: Undefined index: shipId in <b>D:\wamp\www\UniMedStore\cart.php</b> on line <b>8</b><br />
replaced ;
Code: Select all
<?php $_SESSION['shipid']=$_GET['shipId'];?>
I'm actually wanted to store whatever value comes in dropdown menu for next page use.
Like you said php executes only when page is called so i though javascript might be helpful with this way.
Re: Javascript variable in Php
Posted: Thu Mar 04, 2010 1:07 am
by davex
Hi,
Ok I think the easiest way for you to accomplsh this is by handling the clicks in JavaScript and then appending any information you want onto the query string.
For example:
index.php
Code: Select all
<?php
if (isset($_REQUEST['some_variable'])) echo "Some Variable = ".$_REQUEST['some_variable']." in PHP<BR />";
?>
<SCRIPT TYPE="text/javascript">
var someVariable="";
function goLink(url, query) // to to a link - url is the url, query contains the query string thus far
{
if (someVariable != "") // if someVariable is set add it into query
{
if (query == "") query = "some_variable=" + someVariable;
else query = query + "&some_variable=" + someVariable;
}
var href = url;
if (query != "") url = url + "?" + query; // build full URL
document.location.href = href; // redirect to URL
}
function selectChange()
{
someVariable = document.getElementById("some_select").value;
}
</SCRIPT>
<FORM>
<SELECT ID="some_select" onClick="selectChange();">
<OPTION VALUE="1">One</OPTION>
<OPTION VALUE="2">Two</OPTION>
<OPTION VALUE="3">Three</OPTION>
</SELECT></FORM> <BR />
<A HREF="javascript:goLink('index.php','');">Go with nothing else in query string</A><BR />
<A HREF="javascript:goLink('index.php','another_variable=something&yet_another=something+else');">Go with other variables set as well</A><BR />
This is probably far from ideal but it just keeps the value of someVariable and then by directing clicks to a JavaScript function to set the document.location.href (the page being browsed) you can control what other variables are appended to the URL in the GET/Query string.
Cheers,
Dave.