The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Passing Javascript variable to Php variable - How?
Discuss Passing Javascript variable to Php variable - How? in the JavaScript Development forum on Dev Shed. Passing Javascript variable to Php variable - How? JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 17th, 2001, 01:30 AM
|
|
Member
|
|
Join Date: Jun 2001
Location: KL@MY
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Passing Javascript variable to Php variable - How?
Hi,
I started Php quite recently and found it very easy to code. Besides that, there are many online forums/resources available.
But I can't seem to get javascript variable to be passed over to a php function. Can someone pls give an example of how this can be done.
My purpose of this, is to have an select combo box with multiple options. And for each option selected, (onChange="RunSQL();"), a javascript function is called and and sql statement is assigned to a variable. However my var is in Jscript, but i can't get Php to use that var from Jscript.
So how can this be solved?
|

June 17th, 2001, 01:33 AM
|
|
Member
|
|
Join Date: Apr 2001
Location: Seattle
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
i dunno much about JS. I jsut know enough just to be functiona (as I suck at designing, but pro at PHPl.
try this
<script .... bla bla>
<!--
strAddress = "http://site.com/script.php?var1=" + var ;
location.href = strAddess;
-->
</script>
forgive me if I got my string concatination all screwed up between Vbscript and JS. I am very tired. I hope that helped some. I'm not sure if it did. I tried
-Jerome
|

June 17th, 2001, 02:23 AM
|
|
Code Junky
|
|
Join Date: Jan 2001
Location: Central Missouri
Posts: 358
Time spent in forums: 8 h 11 m 4 sec
Reputation Power: 0
|
|
|
About the 'only' way you're going to pass data between JS and PHP is to perform some sort of refresh.. after all JS and PHP are mutually exclusive animals working on two different levels.
Once the document has loaded, it's past PHP.
Now, you could use JS to call an event which triggers the document to reload, sending it back to the PHP interpretor to
see the new vars... maybe a window.reload() or this.submit()..
I chewed through this same dilemma recently..
In short, JS will only execute JS functions..
and PHP only executes PHP Functions...
You can have PHP create your JS, populating it with the appropriate variables.. so that when you say, select item x, it's value can then be carried via the form post back to PHP for whatever's next.
I guess you could say it's something of a one-direction thing.
Taking the other example shown, you could use PHP to
create the var..
<script .... bla bla>
<!--
strAddress = "http://site.com/script.php?var1=<?php echo $var; ?>";
location.href = strAddess;
-->
</script>
In this fashion, when PHP renders the web document the value of $var is embeded into the JS, to be executed appropriately, using JS to call document x with the appropriate argument.
|

June 17th, 2001, 02:28 AM
|
|
Member
|
|
Join Date: Jun 2001
Location: KL@MY
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
passing jscript to php For SQL statement processing
Erm the reason for me passing the jscript var over to php is to have the page reloaded with new SQL statements based on the option selected
This is how it is
I have a select option box where one can select any option
<select name="thebox" onchange="GetSQL();">
<Option value=1>A
<Option value=2>B
<Option value=3>C
<script lang="jscript">
function GetSQL() {
Switch (Option.value)
Case 1:
var jSQL="Select field1 from table1 where field1 like 'A%'";
Case 2:
var jSQL="Select field1 from table1 where field1 like 'B%'";
Case 3:
var jSQL="Select field1 from table1 where field1 like 'C%'";
//do something to reload the page and then passing the jSQL to $phpSQL statement to Php
}
1st, is this a good way of doing it?
2nd, if this is the only way, how could it be done, passing the jSQL to $phpSQL???
I know that jscript is client side and Php is server side... so this is a very bad move....
could anyone suggest a better way? or if its ok to continue with my method above, what is the better way of doing it?
|

June 17th, 2001, 03:26 AM
|
|
Code Junky
|
|
Join Date: Jan 2001
Location: Central Missouri
Posts: 358
Time spent in forums: 8 h 11 m 4 sec
Reputation Power: 0
|
|
|
My apologies, seems I misunderstood your question....
If it were me, I'd simply leave JS out of it.. it's not really necessary.. simply add onChange="this.submit()" to the select menu, when a selection/change occurrs, the form is submitted, and the value of the select name field is passed to
php via HTTP_POST_VAR[]....
There's no real need to put JS into this mix..
|

June 17th, 2001, 04:24 AM
|
|
Member
|
|
Join Date: Jun 2001
Location: KL@MY
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
what if I want the results of the SQL statement to be displayed on the same page itself.
which means that when i've selected the option
the SAME page is displayed but this time the results from the field is added into another select box
<script lang="jscript">
function GetSQL() {
Switch (Option.value)
Case 1:
var jSQL="Select field1 from table1 where field1 like 'A%'";
Case 2:
var jSQL="Select field1 from table1 where field1 like 'B%'";
Case 3:
var jSQL="Select field1 from table1 where field1 like 'C%'";
//do something to reload the same page and then passing the jSQL to $phpSQL statement to Php
}
//Run the Php_dbase function to display the desired results of the SQL statement (taken from phpSQL that was from jSQL) to the select box...
function Take_From_DB() {
@include('adodb/adodb.inc.php'); // load code common to ADODB
ADOLoadCode('access'); // load Access code
$con = &ADONewConnection(); // create a connection
$con->Connect('CD'); // connect to MS-Access using DSN name
/*ACCEPTS VAR SQL; FROM JSCRIPT AND STORE INTO $SQL*/
$recordSet = &$con->Execute($SQL);
/*echo "
<form name = \"CW\">
<select name=\"theBOX\" size=\"15\" multiple >";*/
echo "<TABLE><TR><TD><form name=\"CW\" method=\"GET\" Action=\"\">
<Select name=\"theBOX\" size=\"15\" multiple >";
while(!$recordSet->EOF) {
$num = $recordSet->fields[0].' '.@$recordSet->fields[1];
echo "<option value>$num";
$recordSet->MoveNext();
}
this means that i will have to reload the same page but pass the SQL statement in a form's input value over to the same but new page itself... i can see that i have to do this bcos Php is a server side script thus the page has to be reloaded. but javascript is client side.
how do we code it ?
Last edited by philcp : June 17th, 2001 at 04:28 AM.
|

June 17th, 2001, 05:33 AM
|
|
Code Junky
|
|
Join Date: Jan 2001
Location: Central Missouri
Posts: 358
Time spent in forums: 8 h 11 m 4 sec
Reputation Power: 0
|
|
|
simple, just set your form action to : action="<?php echo $PHP_SELF; ?>" you form then posts to the current document, assuming you PHP code to parse the post are within the document, it's all cool..
just a quick idea..
<?php
if ($submit || $selection) {
$sql = "SELECT mystuff FROM table WHERE id='$selection'";
//do my query
//output my query results...
}
<form action="<?php echo $PHP_SELF; ?>" name='this' method="post">
<select name="selection" onChange="this.submit();">
<option value="1">View Record 1</option>
<option value="2">View Record 2</option>
</select>
Also.. you can further script the select the menu with PHP, to 'select' the last selected option when the document loads..
To be honest, I use a method similar to this for displaying various records, for the most part the refresh is almost un-noticable..
naturally, you can use whatever you need as your option text and value attributes.. in this example, all I've done is passed the selected value to the script, by submitting the form..
this of course let's PHP conduct the query, "if" $selection is present.. (or) if I were using a submit button named 'submit' this would also execute.
|

June 17th, 2001, 11:24 AM
|
|
Member
|
|
Join Date: Jun 2001
Location: KL@MY
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Hi,
Thanks for your continuous help.
Finally, i've managed to accomplish what I wanted to do. But I still am puzzled as to why when i used
"this.submit();" in the codes below
<form action="<?php echo $PHP_SELF; ?>" name='this' method="post">
<select name="selection" onChange="this.submit();">
i have an error message appearing when i make my selection
the error says:
A Runtime Error has occured.
Line:5
Error:Object doesn't support this property or method
But when I changed this.submit() to submit(this.selection)
i did not get any error...
I can't reason this out. Do you have any idea why the former didn't work but the later did?
|

June 17th, 2001, 01:02 PM
|
|
Code Junky
|
|
Join Date: Jan 2001
Location: Central Missouri
Posts: 358
Time spent in forums: 8 h 11 m 4 sec
Reputation Power: 0
|
|
|
I've seen that happen before, and simply changing from an onChange event to onSelect solved.. also, this is what I actually use: onChange="this.form.submit();"
The answer is really more that I have no idea.. doesn't make a lot of sense... really.
But, at least you found a method that works..
|

June 17th, 2001, 01:58 PM
|
|
Member
|
|
Join Date: Apr 2001
Location: Seattle
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
security
Just a tip. Its a bad Idea to put SQL/evaluated code in editable forms (such as JS.) Many bad things can happen when you do that.
-Jerome
|

June 17th, 2001, 08:39 PM
|
|
Member
|
|
Join Date: Jun 2001
Location: KL@MY
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
What sort of reprecussion would happen??
Mind if you clear the air or if you could provide a link to an article about it?
|

June 17th, 2001, 10:29 PM
|
|
Member
|
|
Join Date: Apr 2001
Location: Seattle
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
simple concept
Its a rather simple concept:
Hacker Comes along. Checks the source of you JS. Sees that your SQL is inserted via the page, and is dynamic.
Hacker forges code.
Bad things could happen when hacker can give commands to your system. I'm not sure if its really serouis, but I've always been advised against it. Sorry if I got your panties all in a twist
-Jerome
|

June 17th, 2001, 10:45 PM
|
|
Member
|
|
Join Date: Jun 2001
Location: KL@MY
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Nah its ok it wasn't twisted... I'm ok with it... bcos i avoided JS just like what Sherman advised...
Anyway thanks for the tip.... 
|

June 18th, 2001, 07:33 AM
|
|
Code Junky
|
|
Join Date: Jan 2001
Location: Central Missouri
Posts: 358
Time spent in forums: 8 h 11 m 4 sec
Reputation Power: 0
|
|
|
Phillip,
We briefly talked about the onChange and that JS property error, where, even thought this.submit() and this.form.submit() should work, sometimes you get the "property doesn't support.. blah"...
well, I "finally" figured out "why" you get that error...
The error has something to do with a conflict.. if you have a "submit" button in the same form as the onChange submit, then the two clash... and you get the error.
If you remove one or the other, then all is cool.
In a way, this makes perfect sense, but in another way, it doesn't.
In my case, I was going to use an "onChange" to load up information in the db from a specific record.. and a submit button to 'save the changes'..
anyway, I thought I'd share what I found on the subject.
|

June 18th, 2001, 07:43 AM
|
|
Member
|
|
Join Date: Jun 2001
Location: KL@MY
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Coincidence? - I think NOT
Well,
I was going to do the same thing.
Having had my page work out well using the this.submit() function to resend new values to $PHP_SELF, I wanted to add another submit button to send the new values to be inserted into a database.
Well with your caution, I know now that the submit keyword makes matters a lil' more complicated with the existense of the submit function being called during an onChange event and at the same time having a submit button itself.
To solve it all would be to play around with the words. And like you said - all is cool !!!
Thanks for your time... perhaps I could have the chance in asking you some other questions in time to come.
And ofcourse I'd try to help, but my knowledge in Php is swallow for the moment so....  But I'll do whatever I can....
Last edited by philcp : June 18th, 2001 at 07:47 AM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|