LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2004, 08:56 PM   #1
Elijah
Member
 
Registered: Feb 2003
Location: Philippines
Distribution: Debian, Mandrake, Redhat
Posts: 90

Rep: Reputation: 15
Adding javascript variables to a PHP query


I've been tring to use this postgres insert but it doesn't work 'cause of the 'mchecked[i] variable which is assigned in javascript, how do I do this in PHP without causing syntax errors?

<script>
...
...
<?
$query="INSERT INTO mailinglists(mdateapply, mpriority, account,
mailinglist, mreason, mstatus) VALUES('$date', '10', '$tb_account_name', ?>'mch
ecked[i]'<?,'$tb_reason ','For Approval')";
?>

...
...

</script>

 
Old 11-02-2004, 11:42 PM   #2
Elijah
Member
 
Registered: Feb 2003
Location: Philippines
Distribution: Debian, Mandrake, Redhat
Posts: 90

Original Poster
Rep: Reputation: 15
I tried passing the mchecked[i] javascript array to a php variable but it's not working either. Anyone know how to pass a javascript variable to a php variable?

Last edited by Elijah; 11-02-2004 at 11:46 PM.
 
Old 11-03-2004, 08:22 AM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
PHP and JavaScript are processed at totally different times. PHP is processed on the server, and the resulting HTML (including JavaScript) is sent to the browser, which then takes over executing any JavaScript, etc. So... You cannot use JavaScript to directly set PHP variables, because by the time your JavaScript executes, the PHP has already finished running.

The alternative is to have your JavaScript switch to another PHP page, using your input variables as either POST or GET variables the PHP can then read. (e.g. the URL to it might be something like
http: //yourserver/yourpage.php?javascriptvariable=whatever)

Last edited by deiussum; 11-03-2004 at 08:24 AM.
 
Old 11-03-2004, 10:58 AM   #4
Courthold
Member
 
Registered: Nov 2003
Posts: 58

Rep: Reputation: Disabled
One method I used to pass javascript variables to PHP was when i was trying to get the screen resolution. To do this I did the javascript code, which then refreshed the page with ?variable=value at the end of the url , which PHP can then pick up as a standard variable. Not sure if this helps in your situation.
 
Old 11-03-2004, 07:38 PM   #5
Elijah
Member
 
Registered: Feb 2003
Location: Philippines
Distribution: Debian, Mandrake, Redhat
Posts: 90

Original Poster
Rep: Reputation: 15
Can I be able to pass the variable without going to another page? also, how can I refresh the same page? ...

I did a:
Code:
window.location="application_submitted.php?mchecked='+mchec
ked+'";
and fetched it with:
Code:
$mcheckedphp = $_GET['mchecked'];
It didn't work, mchecked has no value

Last edited by Elijah; 11-03-2004 at 07:40 PM.
 
Old 11-07-2004, 05:36 PM   #6
mgatny
Member
 
Registered: Mar 2004
Posts: 41

Rep: Reputation: 15
Can you post a more complete code snippet?
 
Old 11-07-2004, 07:22 PM   #7
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
I'm religiously opposed to javascript, so I don't know much about it, but shouldn't this:
Code:
window.location="application_submitted.php?mchecked='+mchecked+'";
be something more like this?
Code:
window.location="application_submitted.php?mchecked=" + mchecked[i];
 
Old 11-07-2004, 07:28 PM   #8
Elijah
Member
 
Registered: Feb 2003
Location: Philippines
Distribution: Debian, Mandrake, Redhat
Posts: 90

Original Poster
Rep: Reputation: 15
I can't, I just decided not to use the refresh and pass method. I removed those lines and decided to let php be the array instead of a javascript array. Well, it's not been working out either ... php & js doesn't get along too well.

The snippet deserves to be on a different thread, else it'll be off topic.

Thanks for the help though.

edit: I just opened a new thread, I've been having trouble with arrays & checkboxes using both php & javascript:
http://www.linuxquestions.org/questi...02#post1280002

Last edited by Elijah; 11-07-2004 at 07:59 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Are PHP session variables held in memory when you leave a PHP site? Locura Programming 11 11-16-2008 08:37 PM
passing variables by reference in javascript djgerbavore Programming 2 06-07-2005 11:34 PM
Can I use PHP variables in javascript? nickiv Programming 1 06-07-2005 05:41 AM
adding $PATH variables karupt Linux - Newbie 5 03-07-2004 06:27 PM
willing to pay $$$ PHP / Javascript multiple paged form; carrying over variables micro_sucks General 1 12-26-2003 09:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration