Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-03-2010, 08:13 PM   PM User | #1
imran.rajani
New to the CF scene

 
Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
imran.rajani is an unknown quantity at this point
JavaScript with PHP

Hi Friends,

I don't know if this request has to be posted on PHP forum or this forum is ok.

I've drop down menu.

Code:
<select name="sel_ship" id = "sel_ship" onchange="getShip();">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
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 in .php but its not working.

Code:
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
imran.rajani is offline   Reply With Quote
Old 03-03-2010, 09:51 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You need to understand the fundamentals of client-server programming, especially as it applies to a browser-based client.

*ANY* server-side code in your page (in your case, that means any PHP code) is execute LONG BEFORE the page is sent to the browser (well..."LONG" maybe be milliseconds, but that is forever for a computer).

That means that, in your code
Code:
function getShip() {
    var shipId;
    shipId = document.getElementById("sel_ship").value;
    <?php $_SESSION['shipid']=$_GET['shipId'];?>
}
the PHP code set the SESSION value *BEFORE* the browser EVER SAW this page. It's already been done. Milliseconds or seconds or even minutes ago.

NOTHING AT ALL is happening in PHP when the <select> is changed!!!

*ONLY* when the ENTIRE <FORM> is sent back to the server will PHP get a chance to see the <select> value.

***************

Unless...

Unless you use AJAX (or AJAX-like coding) to send the data back to a *separate* PHP page as soon as the selection is made.

Yes, you *can* do that, but I question what the point of it is.

So you change the session value when the user changes the <select>. And then a moment later he changes his mind, chooses another <option>, and you have to send another AJAX request to change the session value.

But what's the point??? NO OTHER USER can see that session value. And the only time your PHP code is going to be able to use the session value (presumably) is on the next PHP page that the user asks for.

So why not just change the session value only when the <form> is submitted???
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
$_get, java variable, javascript with php, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:12 PM.


Advertisement
Log in to turn off these ads.