small combo box help?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

small combo box help?

Post 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
ruchit
Forum Commoner
Posts: 53
Joined: Mon Sep 26, 2005 6:03 am

Post 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)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

there is no $_POST['batch_id'] there is $_POST['program'] look at the name of your select field
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

Post by rami »

feyd | Please use

Code: Select all

and

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

Code: Select all

$id = $_POST['batch_id']; ....
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

and

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]
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use of mysql_fetch_array() here will print twice as many things to the screen.. not really useful..
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post 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.
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

Post 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
Post Reply