PDA

View Full Version : help in drop out form



tomexsans
18 Feb 2011, 09:32 AM
THE DATABASE
------------------------------------------------------------------
CREATE DATABASE IF NOT EXISTS sample;
------------------------------------------------------------------
DROP TABLE IF EXISTS `sample`;
CREATE TABLE `sample` (
`sampleid` int(10) unsigned NOT NULL auto_increment,
`sample_name` varchar(45) NOT NULL,
PRIMARY KEY (`sampleid`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
------------------------------------------------------------------
INSERT INTO `sample` (`sampleid`,`sample_name`) VALUES
(1,'New Zealand'),
(2,'New York'),
(3,'America'),
(4,'Philippines'),
(5,'MATI'),
(6,'New Found Land'),
(7,'First Second Third'),
(8,'Cant Read Second ');
------------------------------------------------------------------



THE CODE

<?php
//connect to sql
$con= mysql_connect("localhost","root","");
if(!$con){
die("error in connecting to localhost".mysql_error());
}
//select database
$selectdb=mysql_select_db(sample);

//query the names
$qry= "select * from sample order by sampleid";
$result=@mysql_query($qry);

//if form has submitted
if(isset($_POST['test'])){

$sample=$_POST['sample'];
echo "<h1>".$sample."</h1>";
}

?>


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<select type="text" name="sample">";
<option>-- SELECT --</option>

<?php
$count=1;
while($row=mysql_fetch_array($result,MYSQL_NUM)){
$name=$row[1];
echo"<option value=".$name.">".$name."</option>";
$count++;
}?>

</select>
<input type="submit" name="test" value="test">

------------------------------------------------------------------------------


THE PROBLEM


Hello Guys,


Need help, please run the Database and PHP code that i have encoded
my problem goes like this.
i have a form in HTML which has a drop down menu, on which the menu
that is in the drop down form are from or being queried from a database,
now my problem is that, when a user clicks a word for example "NEW ZEALAND",
the drop down menu will return me the word "NEW" insted of "NEW ZEALAND", or
if i'll try "NEW YORK" it will retun me the first word insted of the two words,
now what i need is to get all the words intact like if i click "NEW YORK" the
form will give me "NEW YORK" and so on.i need to get the full words because it is
needed so much. the problem is that it i do not know
whether it is the server-side script or the database, or my code is just
messed up and lacking some thing please help need your suggestions what do you think.


more power to you guys

resdog
18 Feb 2011, 10:10 AM
Try surrounding the value with quotes. As of right now, it's not quoted, so the space is causing the HTML to think the second (or third) part is not part of the value:



<?php
$count=1;
while($row=mysql_fetch_array($result,MYSQL_NUM)){
$name=$row[1];
echo"<option value='".$name."'>".$name."</option>"; //I put a single quote AFTER the equals sign and before the first > sign
$count++;
}?>



Also, one cool thing about php is that if you are using double quotes in an echo statement, you don't have to break out of it to include variables. PHP will interpret the variables as they are. This only works if you are using double quotes, so this would work as well as what you have:



echo"<option value='$name'>$name</option>";


This will allow you to see the code a little better.