JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rating: Thread Rating: 3 votes, 3.33 average. Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 17th, 2001, 01:30 AM
philcp philcp is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: KL@MY
Posts: 18 philcp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to philcp Send a message via Yahoo to philcp
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?

Reply With Quote
  #2  
Old June 17th, 2001, 01:33 AM
jerome jerome is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2001
Location: Seattle
Posts: 5 jerome User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to jerome Send a message via AIM to jerome Send a message via Yahoo to jerome
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

Reply With Quote
  #3  
Old June 17th, 2001, 02:23 AM
Robert_J_Sherman Robert_J_Sherman is offline
Code Junky
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: Central Missouri
Posts: 358 Robert_J_Sherman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 11 m 4 sec
Reputation Power: 0
Send a message via ICQ to Robert_J_Sherman Send a message via MSN to Robert_J_Sherman
Facebook
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.

Reply With Quote
  #4  
Old June 17th, 2001, 02:28 AM
philcp philcp is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: KL@MY
Posts: 18 philcp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to philcp Send a message via Yahoo to philcp
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?

Reply With Quote
  #5  
Old June 17th, 2001, 03:26 AM
Robert_J_Sherman Robert_J_Sherman is offline
Code Junky
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: Central Missouri
Posts: 358 Robert_J_Sherman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 11 m 4 sec
Reputation Power: 0
Send a message via ICQ to Robert_J_Sherman Send a message via MSN to Robert_J_Sherman
Facebook
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..

Reply With Quote
  #6  
Old June 17th, 2001, 04:24 AM
philcp philcp is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: KL@MY
Posts: 18 philcp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to philcp Send a message via Yahoo to philcp
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.

Reply With Quote
  #7  
Old June 17th, 2001, 05:33 AM
Robert_J_Sherman Robert_J_Sherman is offline
Code Junky
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: Central Missouri
Posts: 358 Robert_J_Sherman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 11 m 4 sec
Reputation Power: 0
Send a message via ICQ to Robert_J_Sherman Send a message via MSN to Robert_J_Sherman
Facebook
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.

Reply With Quote
  #8  
Old June 17th, 2001, 11:24 AM
philcp philcp is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: KL@MY
Posts: 18 philcp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to philcp Send a message via Yahoo to philcp
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?

Reply With Quote
  #9  
Old June 17th, 2001, 01:02 PM
Robert_J_Sherman Robert_J_Sherman is offline
Code Junky
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: Central Missouri
Posts: 358 Robert_J_Sherman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 11 m 4 sec
Reputation Power: 0
Send a message via ICQ to Robert_J_Sherman Send a message via MSN to Robert_J_Sherman
Facebook
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..

Reply With Quote
  #10  
Old June 17th, 2001, 01:58 PM
jerome jerome is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2001
Location: Seattle
Posts: 5 jerome User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to jerome Send a message via AIM to jerome Send a message via Yahoo to jerome
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

Reply With Quote
  #11  
Old June 17th, 2001, 08:39 PM
philcp philcp is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: KL@MY
Posts: 18 philcp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to philcp Send a message via Yahoo to philcp
What sort of reprecussion would happen??

Mind if you clear the air or if you could provide a link to an article about it?

Reply With Quote
  #12  
Old June 17th, 2001, 10:29 PM
jerome jerome is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2001
Location: Seattle
Posts: 5 jerome User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to jerome Send a message via AIM to jerome Send a message via Yahoo to jerome
Wink 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

Reply With Quote
  #13  
Old June 17th, 2001, 10:45 PM
philcp philcp is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: KL@MY
Posts: 18 philcp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to philcp Send a message via Yahoo to philcp
Wink

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....

Reply With Quote
  #14  
Old June 18th, 2001, 07:33 AM
Robert_J_Sherman Robert_J_Sherman is offline
Code Junky
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: Central Missouri
Posts: 358 Robert_J_Sherman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 11 m 4 sec
Reputation Power: 0
Send a message via ICQ to Robert_J_Sherman Send a message via MSN to Robert_J_Sherman
Facebook
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.

Reply With Quote
  #15  
Old June 18th, 2001, 07:43 AM
philcp philcp is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: KL@MY
Posts: 18 philcp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to philcp Send a message via Yahoo to philcp
Talking 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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Passing Javascript variable to Php variable - How?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap