-
October 13th, 2006, 11:40 AM
#1
Javascript + Php + Javascript
Here is my code:
Code:
<script language="javascript">
function show_list(){
var val = document.myform.master.options[document.myform.master.selectedIndex].value;
if(val==19){
<?
$sql = 'select * from table where id=19 ORDER BY id ASC';
$res = mysql_query($sql);
$tot = mysql_num_rows($res);
for($i=0;$i<$tot;$i++){
$r = mysql_fetch_array($res);
?>
document.myform2.master.options[<?=$i?>]=new Option("<?=$r[city]?>", <?=$r[id]?>, true, false)
<? } ?>
}
}
</script>
The problem is that I´m setting the id value and I dont want to do that.
How can I put the javascript value (val) into this php line below?
sql = 'select * from table where id=19 ORDER BY id ASC';
I´d like something like this:
sql = 'select * from table where id='+val+' ORDER BY id ASC';
-
October 13th, 2006, 12:24 PM
#2
Re: Javascript + Php + Javascript
Remember that PHP is server-side and therefore is interpreted before your JavaScript. That means you have to create the JavaScript and then send it to the server.
You can do this through 2 options, using a $_POST form or putting it in the url and using $_GET.
PHP Code:
<?php
$id = @$_GET['id'];
$sql = "SELECT * FROM `table` WHERE `id` = '$id' ORDER BY `id` ASC";
?>
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
October 15th, 2006, 04:01 AM
#3
Re: Javascript + Php + Javascript
Originally Posted by peejavery
$id = @$_GET['id'];
Whats with the @ symbol? If id isnt set, then $id will just become nothing.
-
October 15th, 2006, 02:05 PM
#4
Re: Javascript + Php + Javascript
Originally Posted by danbopes
Whats with the @ symbol? If id isnt set, then $id will just become nothing.
And will also cause an error to be written to the log. I use the @ to avoid excess logging of something I already know.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
October 16th, 2006, 03:37 AM
#5
Re: Javascript + Php + Javascript
Originally Posted by peejavery
And will also cause an error to be written to the log. I use the @ to avoid excess logging of something I already know.
I never knew it logged empty variables...wait where? on the server(like the error_log file), cause I never seen it log any of this anywhere.
-
October 16th, 2006, 10:28 AM
#6
Re: Javascript + Php + Javascript
In the PHP folder on the server is file named error.log. All errors are written there. If you call $_GET['var']; and there is not [u]var=[/url] in the URL line, you will get a silent log error.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|