Click to See Complete Forum and Search --> : Javascript + Php + Javascript


rogernem
October 13th, 2006, 12:40 PM
Here is my 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';

PeejAvery
October 13th, 2006, 01:24 PM
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
$id = @$_GET['id'];

$sql = "SELECT * FROM `table` WHERE `id` = '$id' ORDER BY `id` ASC";
?>

danbopes
October 15th, 2006, 05:01 AM
$id = @$_GET['id'];

Whats with the @ symbol? If id isnt set, then $id will just become nothing.

PeejAvery
October 15th, 2006, 03:05 PM
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.

danbopes
October 16th, 2006, 04:37 AM
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.

PeejAvery
October 16th, 2006, 11:28 AM
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.