Page 1 of 1
small combo box help?
Posted: Mon Sep 26, 2005 6:02 am
by rami
i am trying to make combo box which list a field from a table and when a a data is selected in the list it displays all the data about that rom
combo
apple
orage
....
when apple is clicked the things in row of apple should display in another page
i have made this
combo.php
Code: Select all
<form action="myscript.php" method="POST">
<select name="program"><option >select one</option>
<?php
require_once ('../mysql_connect1.php');
$query = "SELECT batch_id,program from batch";
$result = mysql_query ($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
echo '<option value="'.$row['batch_id'].'">'.$row['program'].'</option>';
}
mysql_close();
?>
</select>
<input type="submit" value="Show Data!">
</form>
myscript php
Code: Select all
<?php
require_once ('../mysql_connect.php');
$id = $_POST['batch_id'];
$result = mysql_query("select * from batch where id = $id");
if (!$result)
die(mysql_error()); // this *should* not happen - but it is safe to look for SQL-Errors
if (!mysql_num_rows($result))
die("Oops! No Entry found for $id");
$row = mysql_fetch_assoc($result);
foreach($row as $key => $value)
echo "$key = $value<br \>\n";
?>
the combo.php is working fine but there is error is myscript.php
whats is correct code where is the error..
i think the data from combo.php is not being send to myscript.php
please help..
rami
Posted: Mon Sep 26, 2005 6:11 am
by ruchit
don't use
$row = mysql_fetch_assoc($result);
foreach($row as $key => $value)
echo "$key = $value<br \>\n";
use
$row=mysql_fetch_array($result)
Posted: Mon Sep 26, 2005 6:37 am
by shiznatix
there is no $_POST['batch_id'] there is $_POST['program'] look at the name of your select field
Posted: Mon Sep 26, 2005 9:10 am
by rami
feyd | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
the code doesnt work even if i do
Code: Select all
<select name="id"><option >select one</option>
isn't this line
Code: Select all
echo '<option value="'.$row['batch_id'].'">'.$row['program'].'</option>';
sending data to next file to here
i will try but is this correct code then
Code: Select all
<?php
require_once ('../mysql_connect.php');
$id = $_POST['batch_id'];
$result = mysql_query("select * from batch where id = $id");
if (!$result)
die(mysql_error()); // this *should* not happen - but it is safe to look for SQL-Errors
if (!mysql_num_rows($result))
die("Oops! No Entry found for $id");
$row=mysql_fetch_array($result)
foreach($row as $key => $value)
echo "$key = $value<br \>\n";
?>
any way here is all codes and i guess may be single line or two is incorrect casuing problem so can some body copy and correct that one line......(or 2 which is mistake)
matter or just may be 2 minutes....
thanks for help...
feyd | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Mon Sep 26, 2005 9:20 am
by shiznatix
rami wrote:<select name="id">
there is not <select name="batch_id">, it reads id and that means that $_POST['batch_id'] does not exist, but $_POST['id'] DOES exist
Posted: Mon Sep 26, 2005 9:46 am
by feyd
use of mysql_fetch_array() here will print twice as many things to the screen.. not really useful..
Posted: Mon Sep 26, 2005 2:46 pm
by pilau
Yes, feyd's right... Mysql_fetch_array() returns twice as data, which half of it is actually almost never used.
And mysql_fetch_assoc() is faster.
Posted: Tue Sep 27, 2005 5:20 am
by rami
thanks for all the help provided ...i did
Code: Select all
<select name="batch_id"><option >select one</option>
in combo.php
and
Code: Select all
$row=mysql_fetch_array($result);
foreach($row as $key => $value)
in myscript.php
and it worked ....
by the way how can make combo box from another combo box
have any body seen a combo box which takes data from a table like and displays
combo 1
vegetable
fruit it has it won unique_id
...
(from one table)
and when vegetable is clicked it displays the another combo box,in another page like
combo 2
cabbage
brinjal
.....it has unique id
when cabbage is clicked then its dispalys all the data in the row of that cabbage in that table...
can some body explain(may be show only) me with the same code ..copy and pasting ....the above code....
another problem is i have form in which i have used combo box for entry
Code: Select all
<?php
$query = "SELECT batch_id,program from batch";
$result = @mysql_query ($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
echo "<option value=\"{$row['batch_id']}\">{$row['program']}</option>\n";
}
mysql_close(); // Close the database connection.
?>
in database program is varchar...
but even if i do
echo "<option value=\"{$row['program']}\">{$row['program']}
the program doesnt enters to database just it works with batch_id
in php i have
Code: Select all
}
if (($_POST['program'] > 0))
{
$p=$_POST['program'];
}
else
{
$p = FALSE;
echo '<p><font color="red">Please select at least one category!</font></p>';
}
thanks for help