Page 1 of 1

select chained not showing in $_post method

Posted: Mon Feb 24, 2014 8:39 pm
by juraganerka
anyone please help me. i try to showing selected option value from mysql.
i have 2 selected option,first is showing data from mysql.after the first one got selected,the second one will refer id from the previously. it means,all 2 select option are connected.but when i selected the the first,my second selected value not showing. i use $_POST method to call value.
i give my code bellow
This is my main form.

Code: Select all

<html>
    <head>
    <title>Load Data Customer</title>
    <script language="javascript" src="jquery.js"></script>
    <script>
	$(document).ready(function(){
		$('#customer').change(function(){
		var customer = $(this).val();
		$.ajax({
			type: 'POST', // Metode pengiriman data menggunakan POST
            url: 'customer.php', // File yang akan memproses data
            data: 'nama_customer=' + customer, // Data yang akan dikirim ke file pemroses
            success: function(response) { // Jika berhasil
                $('#person').html(response); // Berikan hasil ke id kota
            }
        });
    });
	</script>
    </head>
<body>
<table>
<tr>
<td valign=top>Customer	</td><td>
<select name="customer" id="customer" class="normal-select">
<option selected>- Pilih Customer -</option>
<?php 
include "connect.php";
$tampil="SELECT * FROM customparent";
if ($res = mysql_query($tampil)) {
    while ($row = mysql_fetch_array($res)) {
echo "<option value=’$row[id]‘>$row[customer]</option>";
}
}
?>
</select>
</td>
</tr>
<tr>
<td>Person</td>
<td><select name="person" id="person" class="normal-select">
<option selected>- Pilih Person -</option>
</select>
</td>
</tr>
</body>
</html>
this file use to call value.

Code: Select all

<?php
include "connect.php";
$tampil="SELECT * FROM customchild WHERE id='$_POST[nama_customer]'";
$option= '';
if($tampil = mysql_query ($tampil)) {
	while($r=mysql_fetch_array($tampil)){
	$option.= "<option value=\"$r[id]\">$r[person]</option>"; 
	}
	echo '<select name="person">'.$option.'</select>';
  }

?>


Re: select chained not showing in $_post method

Posted: Thu Mar 06, 2014 11:08 am
by PhpExile
$tampil="SELECT * FROM customchild WHERE id='$_POST[nama_customer]'";

could it be you spelled name wrong?

Also:

its needs to be $row['customer']. You have to use quotes around the word in the [ ] brackets. Also the $_POST only is created by <form method="post" action="myfile.php"> and it is only available on myfile.php. If you want to continue to use the value from $_POST you either need to create a hidden field on your form or set the $_POST value to a $_SESSION that way you can call it from anywhere on your website as long as the browser isn't closed. I hope I help clear some things up.

Re: select chained not showing in $_post method

Posted: Thu Mar 06, 2014 11:15 am
by Celauran
Inside your while loop, check the current ID against the one contained in the POST array and add selected="selected" if they match.