Forum Moderators: open

Message Too Old, No Replies

list data of combo box still remain

         

heryanto

4:11 am on Jul 18, 2006 (gmt 0)

10+ Year Member



i have two combo box, the first combo box i insert ajax code to change the content the second combo box the problem is when i insert new data or delete one data the content of the second combo box won't change until the browser should been closed

here's the code of javascript :

<script type="text/javascript">
function getScriptPage1(div_id1,content_id1) {
subject_id = div_id1;
content1 = document.getElementById(content_id1).value;
http.open("GET", "../script_page_proses.php?content1=" + escape(content1), true);

http.onreadystatechange = handleHttpResponse;
http.send(null);
}
</script>

----------------------------------
here is the first combo box
------------------------------------

<select name="cmbmesin" id="cmbmesin" onChange="getScriptPage1('output_div1','cmbmesin')" onFocus="nextfield ='cmbproses';">
<? $query=mysql_db_query($db_name,"select * from tamesin_new1");
while ($result=mysql_fetch_array($query)){?>
<? if ($result['nama_mesin']==$cmbmesin1 or $result['mesinid']==$cmbmesin1){?>
<option selected value="<? print $result['bagianid'].','.$result['mesinid'];?>"><?
print $result['nama_mesin'];?></option>
<? }else{?>
<option value="<? print $result['bagianid'].','.$result['mesinid'];?>"><? print $result['nama_mesin'];?></option>
<? }}?>
</select>

----------------------------------------------
and here the php ajax code
----------------------------------------
<?php
include "database.php";
connect_db();
// You can do anything with the data. Just think of the possibilities!
$bagianidaktif=explode(',',$content1);
$sqlstr="select * from taproses where mesinid='$bagianidaktif[1]' order by mesinid";
$query=mysql_db_query($db_name,$sqlstr);
$z=0;
while($hasil=mysql_fetch_array($query)){
$prosesid[$z]=$hasil['prosesid'];
$mesinid[$z]=$hasil['mesinid'];
$nama_proses[$z]=$hasil['nama_proses'];
$z++;
}

print "<select name=\"cmbproses\" id=\"cmbproses\" onFocus=\"nextfield ='txtno_spk';\">";
for ($y=0;$y<$z;$y++){
print "<option value=\"".$prosesid[$y]."\">".$nama_proses[$y]."</option>";
}
print "</select>";

?>

please anyone can help me,...thanks

RonPK

12:05 pm on Jul 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



http.onreadystatechange = handleHttpResponse;

So

handleHttpResponse
is the method that should do something with the stuff that the PHP script returns. Can you please post the relevant code?

Some remarks about the PHP script:

1. I'm not 100% sure but I think the content-type of server output for AJAX handling should be either xml or text/plain. In your case you should add

header('Content-type: text/plain');

before the print statements.

2. $sqlstr="select * from taproses where mesinid='$bagianidaktif[1]' order by mesinid";
It's safer to validate and/or escape any user input before inserting it into a database query. (Do a search with Google for 'sql injection' if you want to read more). This is better:
$sqlstr="select * from taproses where mesinid='" . mysql_real_escape_string($bagianidaktif[1]) . "' order by mesinid";

3. $query=mysql_db_query($db_name,$sqlstr);
As of PHP version 4.0.6, mysql_db_query should not be used. Use mysql_query($sqlstr).