Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Mar 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    retain values in option and select

    When the form is loaded for the second time, i.e, I validate the datas in page 1 and it is reloaded in page2, if the fields entered has to be changed. While doing this I want the values entered by the user has to be retained. I can do this for text fields, without any problem. I don't know how to retain for a combo box, and option buttons. Please help me. Thanks

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if you are building the select by iterating an array of possible values, just test against the passed value during each index loop and echo ' selected="selected"'; if they match - eg

    $passed_value = "two";
    $sel_array = array("one","two","three");
    echo '<select>
    ';
    foreach($sel_array as $s_a)
    {
    echo '<option value="' .$s_a. '"';
    echo ($passed_value == $s_a) ? ' selected="selected"' : '' ;
    echo '>' .$s_a. '</option>
    ';
    }
    echo '</select>
    ';
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    302
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Please excuse my collossal ignorance; I am very new to php. I have searched as best I can (google, php.net, this forum) to find out how to validate a dropdown list. For the following code, how does one present the "if" and "else" statements for the possibility that someone has chosen "twenty" instead of "eighteen"

    Code:
    $passed_value = "eighteen";
    $sel_array = array("eighteen","nineteen","twenty", "twenty one");
    echo '<select name="numbers" id="numbers">';
    foreach($sel_array as $s_a)
    {
    echo '<option value="' .$s_a. '"';
    echo ($passed_value == $s_a) ? ' selected="selected"' : '' ;
    echo '>' .$s_a. '</option>';
    }
    echo '</select>';
    Also, what does one do with the space in "twenty one". I was under the impression that spaces were not allowed. Or is that just for id?

    The reason I ask all this is that I have a mailform that I'm creating that has a couple of dropdown lists.

    I hope my questions made sense and that if they did, I will be able to understand the answers.
    Last edited by llizard; 02-17-2005 at 05:37 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Jumping to the conclusion that you have no issues with sending the data back (either through globals on, $_GET, $_POST, $_SESSION or whatever) validating is really simple:
    PHP Code:
    // This is an array of all stuff that was sent back.
    // We will send it back using $_SESSION['formfields'] as an array.  We'll make some radio and select menus.  $_SESSION['formfields'] is also serialized.

    //... This is whatever php/html you have prior to the form.  We are jumping to the fields.
    $fields = array();
    $fields unserialize($_SESSION['formfields']);

    // $fields contains an array of previous data:  Looks like so:
    /*$fields = array('sel1' => 'one', 'radio1' => '1');
    */

    $selectfields = array('one''two''three');
    // Select fields allows dynamic generation of the content
    echo '<select name="numbers1" id="numbers1">';
    foreach (
    $selectfields AS $key => $val)
    {
         echo 
    '<option value="' $val '"' . ($val == $fields['sel1']) ? ' selected = "selected"' '') . '">' $val '</option>';
    }
    echo 
    '</select>';
    // Lets add a radio:
    echo '<input type="radio" name="radio" value="1"' . ($fields['radio1'] == 1) ? ' selected = "selected"' '') . ' />Yes'
    Ok, so what we have is pretty much identical to what you had seen earlier, which is a nice simplistic way to do it. I added in the radio for just incase, as you will probably use one at one time or another.
    In any case, I hope you have that down. So, as for the validation, I don't understand what you are asking for. If you are using the dropdowns to select, as you metioned a form for mailing, say an email recipient, its an easy task:
    PHP Code:
    if ($_POST['recipient'] == 'Jimmy')
    {
         
    $address 'jimmy@hishouse.com';
    }
    else if (
    $_POST['recipient'] == 'Bobby')
    {
         
    $address 'bobby.friend@hishouse.com';
    }
    else if (......) 
    But, since I don't really know what its all for, or what the form is doing, I can't really help you out much with how the validation process' as to occur. As for your question about 'twenty one' the space is fine, since it is a string value. However, when used in your form value's, most browsers should encode whatever values are set, but if they don't and you have problems you may need to use a urlencode() to help you out. Let us know if you need more assistance.
    Last edited by Fou-Lu; 02-17-2005 at 07:25 PM.

  • #5
    Regular Coder
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    302
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you for your reply, Fou-Lu.

    Here's what I'm trying to do. In a mailform I have a dropdown list:

    Code:
    <form action="mailform.php" method="post" name="form">
    Name: <input name="name" type="text" id="name" size="45"><br />
    E mail: <input name="email" type="text" id="email" size="45"><br />
    <select name="numbers1" id="numbers1">
    <option value="">please choose a number</option>
    <option value="nineteen">nineteen</option>
    <option value="twenty">twenty</option>
    <option value="twenty one">twenty one</option>
    </select><br />
    <input name="Submit" type="submit" value="Submit">
    </form>
    The above isn't exactly what is on the form (there are two drop down lists as well as two textareas and several textinputs) but you get the gist.

    I have the form spit back an error message if the email address is not a valid email address or if the name section is blank. (This is the validating part that I was talking about.) Here's a possible scenario: Someone selects "twenty" on the dropdown list but puts in an invalid email address. The form is 'returned' to them with the name filled in, a note saying that the email address has to be re-entered because it was invalid and (here's where my difficulties are) ideallly the dropdown list showing "twenty". But it's not showing "twenty". It's reverting to the default of "nineteen" instead of showing the value of "twenty" that had been chosen.

    As for what I'm wanting to do with it, once the form is filled in correctly, it will be mailed so the recipient will know what values the form sender has chosen. (Did that make sense??)

    I think that I put in the coding you suggested for the dropdown list into mailform.php correctly but it does exactly what the other version did (as described above....

    I didn't understand why this was included.

    Code:
    /*$fields = array('sel1' => 'one', 'radio1' => '1');
    */
    Don't the /* */ surrounding the code cause the php to ignore it?

    --------------------------------
    (Also, thank you for the radio button stuff. I have something that works but it's always nice to compare coding.)

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yes, the php is commented out to show what it looks like, while the actual variable was stated above that. Looks to me like the bigger problem is sending the data back, its not populated. Easiest way to do this: sessions.
    PHP Code:
    // This is mailform.php.  All we'll check for is that the email is valid, then send back if false:
    function validate_email(&$email)
    {
         if (
    preg_match('/^[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.[a-z]{2,4}$/i'$email))
         {
               return 
    true;
         }
         else
         {
               return 
    false;
         }
    }

    if (!
    validate_email($_POST['email']))
    {
         
    // Invalid, send them back:
         
    $_SESSION['populatefields'] = addslashes(serialize($_POST));
         
    header("location:backtoform.php"); //Maybe a false if already sent

    Ok, so now a session variable named populatefields contains all of the data you need. Back to your mail form:
    PHP Code:
    // This is our handy form that the user sees.
    $populatedfields stripslashes(unserialize($_SESSION['populatefields']));
    unset(
    $_SESSION['populatefields']);
    // This is where $populatedfields is created, and why my original example had it commented out.
    /* Continue with your old codes.
    Use ($populatedfields['value'] == $value) ? ' selected="selected"':'' where nessessary.  */ 
    Ok, another really easy way to do this, and avoid all of the junk above, is to combine everything into one script. If validation becomes false, allow it to abort its current functions and continue with the script. This will allow you to use $_POST back into itself without creating any problems:
    PHP Code:
    // mail.php combined... sort of

    if ($_POST['action'] == 'dosend')
    {
         
    // This is where validation of the email is done, and mail sent from.
         
    exit; // exit if good, send back if bad
    }

    if (
    $_REQUEST['action'] == 'fillform')
    {
         
    // Fillform actions, the sendmail form.  Quick example:
    echo '<form action="' $_SERVER['PHP_SELF'] . '" method="post" name="form" id="form">';
    echo 
    'Name: <input name="name" type="text" id="name" size="45" value="' $_POST['name'] . '" /><br />';
    echo 
    'E mail: <input name="email" type="text" id="email" size="45" value="' $_POST['email'] . '" /><br />';
    echo 
    '<select name="numbers1" id="numbers1">
    <option value="">please choose a number</option>
    <option value="nineteen"' 
    . ($_POST['numbers1'] == 'nineteen') ? ' selected = "selected"' '' ' />nineteen</option>
    <option value="twenty"' 
    . ($_POST['numbers1'] == 'twenty') ? ' selected = "selected"' '' ' />twenty</option>
    <option value="twenty one"' 
    . ($_POST['numbers1'] == 'twenty one') ? ' selected = "selected"' '' ' />twenty one</option>
    </select><br />'
    ;
    echo 
    '<input type="hidden" name="action" value="dosend" />';
    echo 
    '<input name="Submit" type="submit" value="Submit" />';
    echo 
    '</form>';

    So you have your form. You need to send it back to self, with an action 'dosend'. 'dosend' has its action validated before the form is generated, if 'dosend' validates true, it will execute its mailform and exit the script. You would never see the form fields. If it validates false, send it back to $_REQUEST['action'] == 'fillform'. This is nice, since its still in the same script $_POST is still usable for you. If you have problems with the form showing blank originally, use !isset($_REQUEST) function to send it where you need it to go. I hope this is more what you needed.

  • #7
    Regular Coder
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    302
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu
    I hope this is more what you needed.
    Many thanks for your reply, Fou-Lu. It is indeed more than I needed. But sadly, it is also less than I needed.

    I feel like I'm being a bit of a pest. But I'm not skillful enough to apply what you wrote to what I have already. I am already validating within the form and when the form sends successfully, I show a "thank you for sending" message as well as the completed form but without the submit button. Everything works well except the dropdown list. If an error is made, the list goes back to the default OR disappears entirely. If the form is filled in correctly, it sends correctly but the confirmation page does not display the correct field for the dropdown list.

    Please excuse me if this is just asking the same question again.

    I tried applying Ökii's example by doing
    Code:
     
    
    if ( $passed_value == "twenty") {
    $passed_value = "twenty";
    
    etc. etc.
    But that didn't work at all...

    Here is the full code that I am trying to use for the mailform.

    Code:
    <?php
    
    echo "<form action=\"mailform.php\" method=\"post\" name=\"form\">";
    if (($name == "") || ($email == "") ||  ($numbers1 == "") || ($reply == ""))
    {
    	echo "<p>An error has occurred and the form cannot be sent. Please make corrections below.</p>";
    }
    
    if ($name == "") {
    echo "Name: <input name=\"name\" type=\"text\" id=\"name\" size=\"45\"><br />";
    }
    
    else {
    echo "Name: <input name=\"name\" type=\"text\" value=\"$name\" id=\"name\" size=\"45\"><br />";
    }
    
    list($userName, $mailDomain) = split("@", $email);
    $sender_email = $userName."&#064;".$mailDomain;
    
    
    if ($email == "") {
    echo "E mail: <input name=\"email\" type=\"text\" id=\"email\" size=\"45\"><br />";
    }
    elseif ((!ereg(".+\@.+\..+", $email)) || (!ereg("^[a-zA-Z0-9_@.-]+$", $email)))
    {
    echo "invalid address; please retype E mail: <input name=\"email\" type=\"text\" id=\"email\" size=\"45\"><br />";
    $email = "";  
    }
    else {
    echo "E mail: <input name=\"email\" type=\"text\" value=\"$email\" id=\"email\" size=\"45\"><br />";
    }
    
    /* this is where I'm getting messed up - shouldn't there
        be an if  ($numbersl == "") ??? */
    
    if  ($numbersl == "") {
    echo "<select name=\"numbers1\" id=\"numbers1\">
    <option value=\"\">please choose a number</option>
    <option value=\"nineteen\">nineteen</option>
    <option value=\"twenty\">twenty</option>
    <option value=\"twenty one\">twenty one</option>
    </select><br />";
    }
    else {
    
    // no idea how to have option checked and stay checked
    }
    
    
    if ($reply == "")
    {
       echo "Would you like to receive a reply?<br />
    <input type=\"radio\" name=\"reply\" value=\"$yes\" class=\"radio\" />yes <input type=\"radio\" name=\"reply\" value=\"$no\" class=\"radio\" />no<br />";
    }
    else
    {
       if ($reply == "$yes")
       {
       echo "Would you like to receive a reply?<br />
    <input type=\"radio\" name=\"reply\" value=\"$yes\" class=\"radio\" checked=\"checked\" />yes <input type=\"radio\" name=\"reply\" value=\"$no\" class=\"radio\" />no<br />";
    
       }
       else if ($reply == "$no")
       {
       echo "Would you like to receive a reply?<br />
    <input type=\"radio\" name=\"reply\" value=\"$yes\" class=\"radio\" />yes <input type=\"radio\" name=\"reply\" value=\"$no\" class=\"radio\" checked=\"checked\" />no<br />";
       }
       else
       { // this should never happen, but just in case
          echo "Would you like to receive a reply?<br />
    <input type=\"radio\" name=\"reply\" value=\"$yes\" class=\"radio\" />yes <input type=\"radio\" name=\"reply\" value=\"$no\" class=\"radio\" />no<br />";
       }
    } 
    // $goesto - address to which form is to be sent
    $domain="domain.com";
    
    // split up the email address in attempt to protect from spam
    $goesto = "recipient"."@".$domain ;
    
    // $subject - set to the Subject line of the email
    
    $subject = "subject heading for mailform" ;
    if (($name == "") || ($email == "") ||  ($numbers1 == "") || ($reply == ""))
    {
    echo "<input name=\"Submit\" type=\"submit\" value=\"Submit\"> ";                           
    echo "<input name=\"reset\" type=\"reset\" id=\"reset\" value=\"Clear Form\">";
    }
    else
    {
    $message = "Name: $name\nE mail: $email\nNumber: $numbers1\nReply: $reply\n";
    $extra = "From: $name\r\nReply-To: $email\r\n";
    mail ($goesto, $subject, $message, $extra);
    
    echo "<div id=\"thnks\" style=\"position:absolute;top:0; left:15px;\">
    <p>Thank you for your enquiry, $name.  A response will be sent to $userName&#064;$mailDomain as soon as possible.</p></div>";
    }
    
    echo "</form>";
    
    ?>

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I'll tell you what, I'll take a look at it when I get home from work, and let you know the best route of doing this. Shouldn't be a problem at all, the task seems more of an esthetic thing than an error thing, but obviously there is a problem of some sorts. In any case, I'll let you know!

  • #9
    Regular Coder
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    302
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Fou-Lu, that's very kind of you. Thank you.

    It's not just an aesthetic problam. The confirmation should show what values have been sent. That is the only way the sender has of confirming. I suppose I could leave it off altogether or do a cc to the sender and avoid the issue entirely.

    And I'm reluctant to instruct people to go back one page to fix errors. I gather that in some particularly troublesome infamous browsers, form values get erased when going back one page.

  • #10
    Regular Coder
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    302
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I'm not sure that I've explained myself very well. So I've put together an example to show what it is that I mean.

    Go to
    http://etherwork.net/cwc_test/fake/
    and tack ejm_selecttest.html on the end of the URL

    I know that I could put the value of the dropdown list into a hidden input. But if people have made mistakes on the form, I want to give them the option of changing the choice in the dropdown menu.

    (I hope that made sense.)
    Last edited by llizard; 02-21-2005 at 11:20 AM. Reason: correcting grammatical error

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yeah, I gotcha. I've been sick the last couple of days so all I do is work and sleep. I'll get one together in the morning for you, should be a simple alteration.

  • #12
    Regular Coder
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    302
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Sorry to hear you've been sick, Fou-Lu. I muddled away at this on my own and suddenly this morning as I was making coffee, I came to a realization!

    I had tried
    $passed_value = "$s_a";

    and that wasn't working. But as I was testing, I found I could get the form to show what had been chosen. As you suspected, it was a very small tweak that was required. I realized I had to use

    $passed_value = "$numbers";

    It works like a charm!

    Code:
    echo "<p>Number: ";
    $passed_value = "$numbers";
    $sel_array = array("nineteen","twenty","twenty one");
    echo "<select name=\"numbers\" id=\"numbers\">";
    foreach($sel_array as $s_a)
    {
    echo "<option value=\"" .$s_a. "\"";
    echo ($passed_value == $s_a) ? " selected=\"selected\"" : "" ;
    echo ">" .$s_a. "</option>";
    }
    echo "</select></p>";
    Many thanks for you help, Fou-Lu. I couldn't have done it without you.


  •  

    Posting Permissions

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