Results 1 to 2 of 2

Thread: help in drop out form

  1. #1
    Join Date
    Feb 2011
    Posts
    24

    help in drop out form

    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

  2. #2
    Join Date
    Jun 2007
    Posts
    295
    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:

    Code:
    <?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:

    Code:
    echo"<option value='$name'>$name</option>";
    This will allow you to see the code a little better.
    Last edited by resdog; 18 Feb 2011 at 10:13 AM. Reason: add comment about double quotes and variables

Similar Threads

  1. binding a selection from a drop down menu to a form variable
    By eskimo in forum Web Design, HTML Reference and CSS
    Replies: 0
    Last Post: 10 May 2009, 09:48 AM
  2. Contact form with dependent drop-down menus
    By berdot in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 1
    Last Post: 30 Apr 2008, 07:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •