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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Forms: To force user choose an option in dropdown menu

    Hello dear coders. I bow down before you
    (I roughly made a search in the forum, but couldn't find the answer. So if it was asked before, please help me find the thread. Im and old guy )

    I have a form (oh how nice), which I used a generator, to create.
    So, I don't know too much about html and php coding. Im a good tweaker, thats all.

    I have a dropdown menu (which is a required field), and 4 options in it. But because the 1st option is default(ly appears), user is able to skip the dropdown without consciously choosing an option.

    I want to force them to choose an option. Not the 1st option which appears.

    I tried to put additional option, which will not exist in php process file, but I was not successfull. Here's the stripped down codes, that I have:

    HTML form file:
    Code:
    <HTML><HEAD><TITLE>My Form</TITLE></HEAD>
    <BODY>
    <b>All fields marked with a * are required:</b><br>
    <form enctype='multipart/form-data' action='process.php' method='post'>
    <table width='50%' border=0>
    <input type=text name='UserChoice'></td></tr>
    <tr><td>Your Choice<font color='#ff0000'>*</font></td>
    <td>
    <select name='UserChoice'>
    <option value='A1'>A1</option>
    <option value='B2'>B2</option>
    <option value='C3'>C3</option>
    <option value='D4'>D4</option>
    </select></td></tr>
    </table>
    <input type='submit' value='Submit Form'>
    </form>
    </BODY></HTML>
    and this is my PHP process file (process.php)
    PHP Code:
    <?php
    include("global.inc.php");
    $errors=0;
    $error="The following errors occured while processing your form input.<ul>";
    pt_register('POST','UserChoice');
    if(
    $UserChoice=="" ){
    $errors=1;
    $error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
    }
    if(
    $errors==1) echo $error;
    else{
    $where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
    $message="Your Choice ".$UserChoice."
    "
    ;
    $message stripslashes($message);
    mail("my@mail.com","Form Submitted at your website",$message,"From: Generator");
    $link mysql_connect("localhost","user","password");
    mysql_select_db("databaseName",$link);
    $query="insert into tableName
    (Your Choice) values
    ('"
    .$UserChoice."'
    )"
    ;
    mysql_query($query);
    $make=fopen("admin/data.dat","a");
    $to_put="";
    $to_put .= $UserChoice."
    "
    ;
    fwrite($make,$to_put);
    ?>

    <!-- Content of the Thank you page -->

    <h2>Thank you!</h2>

    <table width=50%>
    <tr><td>Your Choice </td><td> <?php echo $UserChoice?> </td></tr>
    </table>
    <?php 
    }
    ?>
    You got what it is, but little bit more info:
    process file is able to report, if a required field left empty. And reports it before it posts the form to MySQL database.

    How to solve my problem, dear masters?
    Thank you in advance.

    PS. If I'm not clear enough, please let me know.
    Last edited by ilterocktivity; 07-14-2006 at 02:43 PM.

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,778
    Thanks
    19
    Thanked 155 Times in 146 Posts
    Add a default option to your select tags, like so:

    Code:
    <select name='UserChoice'>
    <option value='default' selected='selected'>--Select An Option--</option>
    <option value='A1'>A1</option>
    <option value='B2'>B2</option>
    <option value='C3'>C3</option>
    <option value='D4'>D4</option>
    </select>
    then in your PHP, have something like:

    PHP Code:
    if ($UserChoice == "default")
    {
        die(
    'You didn;t select an option.');

    Regards, R.J.

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

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    "die" is a pretty nasty code :P

    So, you think I should add this line of code in php file somewhere, or should I understand my existing process.php and adapt the thing you've wrote?

    You know, I have
    PHP Code:
    if($UserChoice=="" ){
    $errors=1
    I believe it'd work, but I want to keep the codes I have, and modify them. So next time I tweak something, I won't feel like an idiot

    So, if I'd attemt to modify it with your advice, I'd do:
    PHP Code:
    if($UserChoice=="default" ){ 
    $errors=1
    $error.="<li>You did not enter one or more of the required fields. Please go back and try again."
    would it be ok, for the rest of the function? Like listing the error message and so on?

    Thanks a lot!

    Edit: Better than asking... I tried it and it works. Thank you for understanding and help, again
    Last edited by ilterocktivity; 07-14-2006 at 03:05 PM.

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Location
    Germany
    Posts
    279
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There are still ways to go through the code given to you, though (as in deselecting every option). I'd suggest using

    PHP Code:
    if(empty($UserChoice)) {
    //Errors and such



  •  

    Posting Permissions

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