PDA

View Full Version : dynamic drop downs


Ripon
07-13-2012, 04:53 AM
Dear All,

I have new in PHP & MySql. I need two dynamic drop down box which will popolate the data from database. After watching your tutorial I am able to solve my problem. But now I need another option. I want this dropdown box value into my another database. I am able to do this bt problem is, instead of name it is storing the correspondent integer value. another issue is when I select one drop down box value its auto saves the first box value in database without giving chance to select the second drop down box. I am submitting the code below for your to see where I make mistake.

dropdown.php

<?php
require_once("config.php");

$program = $_POST['program'];


if ($program){

$query = sprintf("SELECT * FROM program_name where program_name='$program'");
$result = @mysql_query($query);
$rowModel = mysql_fetch_array($result);

}
?>
<head>
<title>Untitled Document</title>
</head>

<body>

<form id="form1" name="form1" method="post" action="insert_ac.php" >

<select name="program" onChange="document.forms[0].submit()">
<option value="">Select Make</option>
<option value="1" <?php if(!(strcmp(1, $program))){echo "selected";}?>>Team A</option>
<option value="2" <?php if(!(strcmp(2, $program))){echo "selected";}?>>Team B</option>
</select>


<select name="Item_Code">
<option value="">Select Model</option>
<?php do { ?>
<option value="<?php echo $rowModel['program']; ?>"><?php echo $rowModel['Item_Code']; ?></option>
<?php }while ($rowModel = mysql_fetch_array($result)); ?>
</select>
<input type="submit" name="Submit" value="Submit Your Order Information...">
</form>

</body>
</html>


insert_ac.php

<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="date"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form

$program=$_POST['program'];

$Item_Code=$_POST['Item_Code'];

// Insert data into mysql
$sql="INSERT INTO $tbl_name(program, Item_Code)VALUES('$program', '$Item_Code')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='index.php'>Back to main page</a>";
}

else {
echo "ERROR";
}
?>

<?php
// close connection
mysql_close();
?>

thanks to all in advance.

Ripon