SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Drop down list

  1. #1
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Drop down list

    Hi,

    This is a simple script, When I select "Microcyb & PHPVALUE" and "Google , Yahoo"
    only Microcyb and Google is printed.

    How can i get the all the values from drop down list

    Code:
    <?php
    
    echo '<form action="dropdown.php" method="GET">';
    $category = array( 1=> "Microcyb & PHPVALUE", 2=> "Google , Yahoo", 3=> "YABB", 4=> "Macromedia", 5=> "PHP", ); 
    $category = str_replace(" ", " ", $category); 
    
    echo '<SELECT name=category>'; 
    foreach ($category as $key => $value) 
    { 
    echo '<OPTION value='.$value.'> '.$value.''; 
    } 
    echo '</select>'; 
    
    echo '<input type ="submit" value="submit">';
    echo '</form>';
    
    
    
    
    	$name = $_REQUEST['category'];
    	print $name;
    
    ?>

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    echo '<OPTION value="'.$value.'"> '.$value.''
    You are not quoting the value, add " as above, the value should be quoted string or a plain integer - this is a html error on your part. If you had viewed "source" code in your browser you would have spotted it.

    BTW
    PHP Code:
    ...  

    echo 
    '</select>' PHP_EOL

    echo 
    '<input type ="submit" value="submit">' PHP_EOL;
    echo 
    '</form>' PHP_EOL
    Adding the constant PHP_EOL adds a line end so your source code can be read easily too, anything that helps you to debug, right?


    PHP Code:
    $category str_replace(" "" "$category);  // uh? 

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Adding the constant PHP_EOL adds a line end so your source code can be read easily too, anything that helps you to debug, right
    Wow, I never knew that! I usually just use
    PHP Code:
    echo 'Hello world'."\n"
    Which I've always thought is far too ugly

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Yeah, its a right kludge isnt it?

    You are just getting used to using single quotes because you are disciplining yourself to keep most html output somewhere else, then you have to revert to "\n" to double quotes, nightmare.

    I learned about PHP_EOL on here too.

  5. #5
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    echo '<OPTION value="'.$value.'"> '.$value.'';
    This works.
    But another problem is text box will omit the string after special characters.
    For ex:
    Code:
     <option <? if(getValue($frm['value']) == 'name') {echo 'selected';} ?> value='name'>name</option>
    When selected the value from this drop down, the string after special character is truncated.
    How to fix this

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    View the html source code, does that look correct?

  7. #7
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    View the html source code, does that look correct?

    Now I tried with the text box value.
    In the text box the value entered was "Hi , how r u"
    <input type="text" name="name" id="search" value="Hi " >

    In the text box only the value is truncated.

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Looks as if you aren't quoting the html properly, if an attribute contains a string like_this, then html is forgiving enough to assign it the value "like_this", but if your string contains a space like that then html splits it at the first space and reads it as "like".

    So you have to make sure the output of your PHP, which itself is quoted, is quoted correctly.

    Where this will work:
    echo '<a href="anotherpage.php?id=' . $id . '">A link</a>' ;
    Giving:
    <a href="anotherpage.php?id=23">A link</a>

    this (missing closing double quote)
    echo '<a href="anotherpage.php?id=' . $id . '>A link</a>' ;

    Will not work:
    <a href="anotherpage.php?id=23>A link</a>


    So it is critical that you get your head round that fact, get used to looking at the source code of the page until you have worked out the best quoting practice that you can not only remember - but apply in every piece of code you write.


Bookmarks

Posting Permissions

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