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 10 of 10
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Contact Form Return Error

    Hi

    I have a contact form/questionnaire in php with several drop down options. I would like these to be required fields.

    This is the code for one of the drop downs

    Code:
     <select name="gender" style="width: 100px">
    	  <option value="0"></option>
    	  <option value="1">Male</option>
    	  <option value="1">Female</option>
    	  </select>
    What code would be required to ensure that when the value entered is less than 1 (I.e, the initial blank field) the form returns as an error?

    Apologies if using the values is not the right way to do this.

    I already have this working for name and email address, the coding for that is as follows

    PHP Code:
    if(!isset($_POST['name']) ||
    if(!isset(
    $_POST['email']) ||
    !isset(
    $_POST['gender'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');    
    }
        
        
    $name_field $_POST['name'];// required
        
    $email_field $_POST['email'];// required
        
    $option $_POST['radio'];// not required
        
    $gender $_POST['gender'];// not required
        
    $ethnicity $_POST['ethnicity'];// not required
        
    $age_group $_POST['age_group'];// not required
        
    $education $_POST['education'];// not required
        
    $howlong $_POST['howlong'];// not required
        
    $become $_POST['become'];// not required
        
    $side_regiment $_POST['side_regiment'];// not required
        
    $whyjoin $_POST['whyjoin'];// not required
        
    $manyevents $_POST['manyevents'];// not required
        
    $participant_or_public $_POST['participant_or_public'];// not required
        
    $public_gain $_POST['public_gain'];// not required
        
    $participant_gain $_POST['participant_gain'];// not required
        
    $feeling $_POST['feeling'];// not required
        
    $artefacts_display $_POST['artefacts_display'];// not required
        
    $commemorative $_POST['commemorative'];// not required
        
    $glorifywar $_POST['glorifywar'];// not required
        
    $accuracy $_POST['accuracy'];// not required
        
    $strengths $_POST['strengths'];// not required
        
    $weaknesses $_POST['weaknesses'];// not required
        
    $improvement $_POST['improvement'];// not required
        
    $other_comments $_POST['other_comments'];// not required
        
    $feedback $_POST['feedback'];// not required

    $error_message "";
    $email_exp "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
    if(!
    eregi($email_exp,$email_field)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }
    $string_exp "^[a-z .'-]+$";
    if(!
    eregi($string_exp,$name_field)) {
    $error_message .= 'Your Name may not contain any numbers or symbols.<br />';
    }
    if(
    strlen($error_message) > 0) {
    died($error_message);
    }
    function 
    clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return 
    str_replace($bad,"",$string); 
    So, how do I add drop downs to this?

    Thanks!
    Last edited by kickthat; 11-11-2010 at 06:49 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Yah, definitely wrong

    Code:
     <select name="gender" style="width: 100px">
    	  <option value="0"></option>
    	  <option value="1">Male</option>
    	  <option value="1">Female</option>
    	  </select>
    When you pass a select, like all other inputs, you extract it via its name. So, in this case $_POST['gender']. However, given you have two identical values... regardless of if the user pics Male or Female, you'll get back 1, which won't tell you anything useful. So, what you want is:

    Code:
     <select name="gender" style="width: 100px">
    	  <option>Select One</option>
    	  <option value="m">Male</option>
    	  <option value="f">Female</option>
    	  </select>
    Also, in a situation like this, radio buttons are a better option, visually.

  • #3
    New Coder
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for the response.
    Good advice about the radio buttons, I probably will go for them with gender at least.

    However, I have a few more drop downs with more than 2 values - too many to put into radio buttons.

    Any idea how to write the php to ensure that these return an error if the <option>Select One</option> remains selected? I.E, if the participant forgets to enter a value?

    Thanks!

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by kickthat View Post
    Thanks for the response.
    Good advice about the radio buttons, I probably will go for them with gender at least.

    However, I have a few more drop downs with more than 2 values - too many to put into radio buttons.

    Any idea how to write the php to ensure that these return an error if the <option>Select One</option> remains selected? I.E, if the participant forgets to enter a value?

    Thanks!
    I actually screwed that up. Should be <option value="">Select One</option>, then just test against a blank.

  • #5
    New Coder
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Keleth View Post
    then just test against a blank.
    Sorry to be a pain, but how do I do this? I'm pretty new to this.
    Thanks for your help.

  • #6
    New Coder
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I've gone with this;

    PHP Code:
     $gender_exp "^[A-Z1-9 .'-]+$";
    if(!
    eregi($gender_exp,$gender)) {
    $error_message .= 'You didnt choose an option from the drop-down list'
    Which seems to be doing the job, when the 'Select One' option is given a value of 0.

    Thanks for the help.
    Last edited by kickthat; 11-11-2010 at 06:50 PM.

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Ahhhh! Zomebie code! Kill the eregi!!!!

    eregi is deprecated and no longer in use. Use preg, and as for testing against blank... $variable == ''. Why call a regex function? Hell, if you're testing, you should be testing against valid values. Given you're expecting either m or f, your regex will confirm a lot of things that aren't even options.

    PHP Code:
    if (!in_array($_POST['gender'], array('m''f'))) {
    // Give an error


  • Users who have thanked Keleth for this post:

    kickthat (11-11-2010)

  • #8
    New Coder
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks again.

    Sorry for my ignorance. I have never been taught any coding so everything I have been using has been picked up from the internet. I was using this to return an error because it was what I could find that worked.

    If you wouldn't mind helping a bit more, how do I need to adapt the code in my first post, as I have used eregi to send errors for several other values.

    Does
    PHP Code:
     if (!in_array($_POST['gender'], array('m''f'))) {
    // Give an error

    Still go with

    PHP Code:
     if(!isset($_POST['name']) ||
    if(!isset(
    $_POST['email']) ||
    !isset(
    $_POST['gender'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');    

    That I posted in my first post?

    Thanks again for the help, and sorry for my ignorance :/
    Last edited by kickthat; 11-11-2010 at 06:50 PM.

  • #9
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Code:
     if(!isset($_POST['name']) ||
    if(!isset($_POST['email']) ||
    !isset($_POST['gender'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');	
    }
    That won't work in all situations and is the wrong check. I assume name and email are text inputs? Again, if so, you need to test again blank ($var == '').

  • #10
    New Coder
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts
    OK, I'd worked this out.

    PHP Code:
     <?php
    if(isset($_POST['submit'])) {

        
    $to "re-enactment-heritage@hotmail.co.uk"
        
    $subject "Questionnaire";
        
        function 
    died($error) {
        include(
    "header.php");
                
    // your error code can go here
            
    ?>
            <table class="main-table" cellpadding="0" cellspacing="0">
        <tr>
            <td class="main-topleft" style="height: 21px"></td>
            <td class="main-topline" style="height: 21px"></td>
            <td class="main-topright" style="height: 21px"></td>
        </tr>
        <tr>
            <td class="main-leftline"></td>
            <td class="main-textbox"><?
            
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo 
    "These errors appear below.<br /><br />";
            echo 
    $error."<br /><br />";
            echo 
    "These errors are in Section 1, 'Personal Details'<br /><br /> Click back.<br /><br />";
            
    ?>
            <INPUT TYPE="button" VALUE="Back" onClick="history.go(-1);">
    </td>
    <td class="main-rightline">&nbsp;</td>
        </tr>
        <tr>
            <td class="main-bottomleft">&nbsp;</td>
            <td class="main-bottomline">&nbsp;</td>
            <td class="main-bottomright">&nbsp;</td>
        </tr>
    </table>

    <?
    include("footer.php");
    die(); 
    }

        
    $name_field $_POST['name'];// required
        
    $email_field $_POST['email'];// required
        
    $option $_POST['radio'];// not required
        
    $gender $_POST['gender'];// not required
        
    $ethnicity $_POST['ethnicity'];// not required
        
    $age_group $_POST['age_group'];// not required
        
    $education $_POST['education'];// not required
        
    $howlong $_POST['howlong'];// not required
        
    $become $_POST['become'];// not required
        
    $side_regiment $_POST['side_regiment'];// not required
        
    $whyjoin $_POST['whyjoin'];// not required
        
    $manyevents $_POST['manyevents'];// not required
        
    $participant_or_public $_POST['participant_or_public'];// not required
        
    $public_gain $_POST['public_gain'];// not required
        
    $participant_gain $_POST['participant_gain'];// not required
        
    $feeling $_POST['feeling'];// not required
        
    $artefacts_display $_POST['artefacts_display'];// not required
        
    $commemorative $_POST['commemorative'];// not required
        
    $glorifywar $_POST['glorifywar'];// not required
        
    $accuracy $_POST['accuracy'];// not required
        
    $strengths $_POST['strengths'];// not required
        
    $weaknesses $_POST['weaknesses'];// not required
        
    $improvement $_POST['improvement'];// not required
        
    $other_comments $_POST['other_comments'];// not required
        
    $feedback $_POST['feedback'];// not required
        
    if($_POST['name'] == "") {
    //the name field is empty
    $error_message .= 'You have not entered a name.<br />';
    }
    if(
    $_POST['email'] == "") {
    //the email field is empty
    $error_message .= 'You have not entered an email address.<br />';
    }
    if (!
    in_array($_POST['radio'], array(!""))) {
    $error_message .= 'You have not selected to receive further questions or not.<br />';
    }
    if (!
    in_array($_POST['gender'], array('m''f'))) {
    $error_message .= 'You have not entered a gender.<br />';
    }
    if (!
    in_array($_POST['ethnicity'], array(!""))) {
    $error_message .= 'You have not entered an ethnicity.<br />';
    }
    if (!
    in_array($_POST['age_group'], array(!""))) {
    $error_message .= 'You have not entered an age group.<br />';
    }

    if(
    strlen($error_message) > 0) {
    died($error_message);
    }

        
    $body "From: $name_field\n E-Mail: $email_field\n Option: $option\n Gender: $gender\n Ethnicity: $ethnicity\n Age Group: $age_group\n Education: $education\n How Long: $howlong\n Become: $become\n Side/Regiment: $side_regiment\n Why Join: $whyjoin\n How Many Events: $manyevents\n Participant or Public: $participant_or_public\n Public Gain: $public_gain\n Participant Gain: $participant_gain\n Feeling: $feeling\n Artefacts Display: $artefacts_display\n Commemorative: $commemorative\n Glorify War: $glorifywar\n Accuracy: $accuracy\n Strengths: $strengths\n Weaknesses: $weaknesses\n Improvement: $improvement\n Other Comments: $other_comments\n Feedback: $feedback\n";
    ?>
        <meta HTTP-EQUIV="REFRESH" content="0; url=http://www.re-enactment-heritage.co.uk/thanks.html">
    <?
        mail
    ($to$subject$body);
        

    else {
        echo 
    "blarg!";
    }
    ?>
    The html within the 'function died($error) {' points to a personalised error page. This works but again, I'm sure it's probably not correctly written.

    Gender, as suggested, is now a radio button.
    Ethnicity and age group have the 'Select One' option set as a value of 0, hence array(!"") which will return an error if the value is 0, but will return as positive if any of the other choices is entered.

    Again, apologies for any coding mistakes, I've written it like this as it seems to make sense to me to do it like this - I'm more than happy to be told I'm doing it wrong.

    Thanks, yet again, for helping!


  •  

    Posting Permissions

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