SELECT checking in forms

Hello all,
PHP I am a complete beginner, but I’m trying to. I have a form with PHP control where is INPUT and SELECT.
I do not know if I wrote well checking and send a SELECT. If it is not so please help me fix it.
:-[ :-[
[php]

<?php $titleErr = $nameErr = $emailErr = $commentErr = ""; $title = $name = $email = $comment = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if($_POST['title'] == '0') { echo '

Please select an option from the select box.

'; } else { echo '

You have selected: ', $_POST['title'], '.

'; } if (empty($_POST["name"])) { $nameErr = "Must enter the Name!"; } else { $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "Only letters and white space allowed"; } } if (empty($_POST["email"])) { $emailErr = "Must enter the name Email!"; } else { $email = test_input($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "Invalid email format"; } } if (empty($_POST["comment"])) { $commentErr = "Must enter the name comment"; } else { $comment = test_input($_POST["comment"]); if (!preg_match("/^[a-zA-Z ]*$/",$comment)) { $commentErr = "Only letters and white space allowed"; } } if ($titleErr=="" AND $nameErr=="" AND $emailErr=="" AND $commentErr=="") { $to = "@ my mail"; $from = $_POST['email']; $title = $_POST['title']; $name = $_POST['name']; $subject = "Info from"; $subject2 = "Copy of your form submission"; $comment = $title . " " . $name . " " . "\n\n" . $_POST['comment']; $comment2 = "Here is a copy of your comment " . $name . "\n\n" . $_POST['comment']; $headers = "From:" . $from; $headers2 = "From:" . $to; mail($to,$subject,$comment,$headers); mail($from,$subject2,$comment2,$headers2); header("Location: thanks.html"); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?>

[/php]

[sup]

Write us what you need to know * required field.
 Title: <select name="title">
            <option value="0"> choose </option>
            <option value="1"> Mr </option>
            <option value="1"> Mrs </option>
            <option value="1"> Ms </option>
            <option value="1"> Miss </option>
        </select>
 <br><br>     
 Name: <input type="text" name="name" size="30" value="<?php echo $name;?>">
 <span class="error"><big> * </big> <?php echo $nameErr;?></span><span id="example"> / First Name and Last Name / </span>
 <br><br>
 E-mail: <input type="text" name="email" size="40" value="<?php echo $email;?>">
 <span class="error"><big> * </big> <?php echo $emailErr;?></span>
 <br><br>
 Comment:<span class="error" style="position:relative; top:2px;"><big> * </big></span>
          <br><br>
         <textarea name="comment" rows="7" cols="55"> <?php echo $comment;?></textarea>
         <span class="error"><?php echo $commentErr;?></span>
 <br>
 <p class="navod"> Turn the picture into the correct position ! </p>
 
<div  id = "rocaptcha_placeholder" ></div>
  <script  type = "text/javascript"  src = "http://rocaptcha.com/api/js/?key=... my key ..." ></script>
  <script  type = "text/javascript" >
       RoCaptcha . init ( "rocaptcha_placeholder" );
  </script>
 <br>
<input type="submit" name="submit" value="Submit" id="send">
</fieldset>
[/sup]

Thank you to all for your help

willH, you have errors in your SELECT clause. You have several of the options set to the same value.
So, this code:
[php]

choose
Mr
Mrs
Ms
Miss

[/php]
Should be:
[php]

choose
Mr
Mrs
Ms
Miss

[/php]
OR, even better:
[php]

choose
Mr
Mrs
Ms
Miss

[/php]
Then, to use that info, you can use:
[php]
if ($_POST[‘title’]!=“0”) echo '

You have selected: ', $_POST[‘title’], ‘.

’;
[/php]
As you see, if they forgot to pick a title, you do not want to display it. If they picked one, it will not have
the value of zero and will display as you want it to be…

Hope that helps…
( Also, please place all of your code inside the PHP tags, even HTML. Makes it easier for us to use it! )

Thank you for advice and repair.
If value of zero see “Please select an option!” If is selected one from options so OK.
Is it possible it written like this?

[php]

if ($_POST[‘title’]!=“0”) echo '

Please select an option! ', $_POST[‘title’], ‘.

’;

[/php]

Please still help to do a check CHECKBOX:

[php]

[/php]

Is it possible it written like this?

[php]

if ($_POST[‘agree’]!=“0”) echo '

Must accept the terms! ', $_POST[‘agree’], ‘.

’;

[/php]

Well, yes, you can do the checkbox that way, but, it is not needed.
You do not need the hidden field. Let’s explain to you…

A checkbox is displayed on the form as NOT-SELECTED unless you set it that way. Normally, you do not.
Therefore, if the user posts the form with the checkmark checked, you can just see if it is posted in the PHP
code. If the user does NOT check it, then it is not included inside the posted array. Therefore, you just need
to check if it is set or not. So, remove the hidden field and change the code to compare if it is “1”.

The != in the IF clause means IF NOT EQUAL. So, you need to check that the value is NOT 1. You test it
for NOT 0… (If it is NOT EQUAL TO 1, then they did not agree. HOpe that explains it…

So could it be?

[php]

[/php]

and checking in PHP

[php]
if (!empty($_POST[‘agree’])) {
echo “Must accept the terms!”;
}
[/php]

It is well written ???

Please check:
I need the phone number verification in PHP.

[php]
if (empty($_POST[“phone”])) {
$phoneErr = “Must enter the Phone!”;
} else {
$phone = test_input($_POST[“phone”]);

   if(!$phone || strlen($phone) < 11)
{
    $error .= "Please enter your phone number without spaces.<br />";
}
 }

[/php]

Yes, the checkbox should work that way.

Now, phone numbers are tricky to validate. It depends on your area and how user might enter their phone
numbers. In my area, it is usually entered like this: 999-99-9999, but, in a site I am working on in another
country, it is +99-99-9999. There are thousands of sites on the net that talk about the best way to handle
phone numbers. How should you handle it?

First, know your area’s that might enter phone numbers on your site. Create a list of how the numbers are
formatted. In your code sample it is obvious that you are allowing 10 digits for a phone number. That is the
standard way that most phone code is based on. Most phone code also makes sure the user enters only
numbers. Depending on how complicated you want to get here are some ideas on validation for these…

Remove all spaces. This can be done with one line of code.
Remove dashes ( - ) as it is common in most of the world to use them such as 99-999-9999 here… Again,
one line of code…
Check rest of their number entry for just numbers to make sure they did not enter special characters. One
line for that…

Here are simple ways to handle that list. It uses your code, but, adds in the extra checking…

[php]
if (empty($_POST[“phone”])) {
$phoneErr = “Must enter the Phone!”;
} else {
$phone = test_input($_POST[“phone”]); // Trim, remove slashes…
$phone = str_replace(" “, “”, $phone); // Remove spaces…
$phone = str_replace(”-", “”, $phone); // Remove dashes…
if((preg_match("/[^0-9]/", ‘’, $phone)) && strlen($phone) < 11) { // Numbers only, max 10 numbers…
$error .= “Phone number is invalid, please re-enter your phone number.
”;
}
}
[/php]
As you can see, it still uses your test_input to do those functions, but removes spaces and dashes in case
the user enters them. Then, it checks for numbers-only using the preg_match function. I have seen a very
large number of validation routines for phone numbers on the internet. I found a great one that actually
uses formats like 99-99-9999, 9-999-99-999 etc to layout the allowed formats for phone numbers and then
checks it for only those valid formats. I have found that it is not as important to do this. Basically, you need
to just remove spaces and dashes and make sure the rest is just numbers. So, if a user does type in any
space or dash, your routine still works for them. Nice that way! So, 99-999-999 would just give a result of
99999999 which would work. Or, 99 999-9999 would also give 999999999 which would also work.
Hope that helps!

The phone number will be only English. Example: 0744-xxxxxxx
Goes is do this style? as example.

So, use the code I posted. It will remove any space or dash the user types. Then, change the compare to
be <12 not <11 as you showed 11 characters. So, they need to enter 11. Or, you can force them to give you
exactly 11 numbers by checking for !=11 (not equal to 11)…

Let us know if that works for you…

Ernie, how to finish and test form so let you know.
It’s more an order form. A total number of 24 boxes, including 14 fields is compulsory, 8 fields is SELECT ,
1 field is CHECKBOX, 1 field is TEXTAREA. I know I had taken big bite, but that to need .
If I had a problem, can I please get in touch? Thank you.

Yes, willH, we are here to help. When you have a problem with your code, just post here and we can help
you fix up the issues. It is more easy to do one at a time, but, we can help. This site was created to give
help to new programmers and experienced ones, too. A large number of expert programmers are here and
will jump in to help you.

So, please feel free to ask your questions. A large form is not a problem. Just let us know what your next
problem is and we will help…

If you feel you need private help with this, you can contact any of us in a “Personal Message”. Just click on
the “My Messages” tab above and select “Send a Message”. There are programmers on this site which will
also do a job for you for pay. But, I will help you for free… Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service