SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    <option value='A' <? if ($_POST== 'A')...

    hi all,

    i tried the whole last night in vain to google for a solution, what i want to do is to retain the value of SELECT BOX in the FORM. SELECT BOX is just a part of the form user need to submit. it should return all the values in the same form after validation(i use php for error-checking) and let the user fill the missing fields or correct the mistakes.

    i remember some tutorail mentioned the method:
    PHP Code:
    <option value='A' <? if ($_POST == 'A'){ echo "selected"; } ?>>A</option>
    but i cant get it to work.

    another problem is the TEXTAREA, cant retain the value either.

    is there's a way using PHP?

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <select name="foo">
    <option value="a"<?php if ($_POST['foo'] == 'a') echo ' selected'?>>a</option>
    <option value="b"<?php if ($_POST['foo'] == 'b') echo ' selected'?>>b</option>

  3. #3
    SitePoint Evangelist Waffles's Avatar
    Join Date
    Nov 2005
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // textarea

    echo '<textarea name="name">'.$_POST['data'].'</textarea>';

    // or

    echo '<textarea name="name">'
    if(isset(
    $_POST['data'])) echo $_POST['data'];
    echo 
    '</textarea>'

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks guy for your kind and quick reply, i'm going to try those code now, get back to you later!

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry guys, but neither of them worked for me. any reason why?

    thx very much.

  6. #6
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just do this:

    PHP Code:
    function isSelected($val1$val2 1) {
        return (
    $val1 == $val2) ? ' selected' '';
    }

    echo 
    '<option value="a"'.isSelected('a'$_POST['var']).'>A</option>';
    echo 
    '<option value="b"'.isSelected('b'$_POST['var']).'>B</option>';
    echo 
    '<option value="c"'.isSelected('c'$_POST['var']).'>C</option>';
    echo 
    '<option value="d"'.isSelected('d'$_POST['var']).'>D</option>'
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  7. #7
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thx triexa, but i'm not quite understand the code.
    am i right to put it this way?

    PHP Code:
    <select name="name">
    <?php
    function isSelected($val1$val2 1) { 
        return (
    $val1 == $val2) ? ' selected' ''
    }
    echo 
    '<option></option>';
    echo 
    '<option value="a"' .isSelected('a'$_POST['name']).'>a</option>';
    echo 
    '<option value="b"' .isSelected('b'$_POST['name']).'>b</option>';
    echo 
    '<option value="c"' .isSelected('c'$_POST['name']).'>c</option>'
    ?>

    </select>

  8. #8
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    umm yes, although not sure what is so different from what I gave you?
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  9. #9
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi guys, i solved the problem now!

    my form: <form name="name" method="post" action="process.php">

    in the process.php, i got
    PHP Code:
    $field "category";
    if(!
    $subcategory || strlen($subcategory trim($subcategory)) == 0)
     { 
    $form->setError($field"* category not chosen"); }
      
    $category $_POST['category']; 
    and inside the form
    PHP Code:
    <select name="category">
    <option></option>
    <option value="A"<?php if ($form->value("category") == "A") {echo "selected";} ?>>A</option>
    ......
    </select>
    <? echo  .$form->error("category"). ?>
    my system is a bit more complicate than this, but this is how it works, hope this will help you guys, though i still needs to look into the TEXTAREA field.

    thanks

  10. #10
    SitePoint Evangelist Waffles's Avatar
    Join Date
    Nov 2005
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what are you trying to accomplish with the textarea? the code I gave you will echo out what was in the $_POST[] variable when you submit the form..I had assumed this is what you wanted by 'retaining' the value...a user types into the area, but misses out another field and so rather than have to retype out the text area, the code i provided keeps it in the box for the user.

  11. #11
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry waffle,
    didnt notice your mesg till now. what you said is exactly i want to do with the textarea. i just dont know why the code you gave me cant echo back the text, i did try very hard to make it work, it might because the other session settings in my whole code.

    thx very much for your help anyway!


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
  •