title: Where to put the validation code of php form validation
body: <p>If I am doing form validation for the user input, I have to put the phpcode in the page with form right?</p>
  
  <p>Also in the <code>&lt;form&gt;</code> tag do I have to specify anything. I mean if there is an error the user will have to return to the same input page with errors showing up. If it was successful input, the user should goto order-process.php</p>
  
  <p><strong>PHP Validation Code:</strong></p>
  
  <pre><code>&lt;?php
  $name = $_POST['name'];
  $nameregex = '/^[a-zA-Z]+\s?[a-zA-Z]+$/';
  if (!preg_match($nameregex, $name)) {
      echo '&lt;font color=red&gt;Please enter a valid name&lt;/font&gt;';
  }
  $email = $_POST['email'];
  $emailregex = '/^[a-zA-Z\_\-\.0-9]+\@[a-zA-Z\_\-\.0-9]+$/';
  if (!preg_match($emailegex, $email)) {
      echo '&lt;font color="red"&gt;please Enter a valid email id&lt;/font&gt;';
  }
  $address = $_POST['address'];
  $addressregex = '/^[0-9a-zA-Z\,\s]+$/';
  if (!preg_match($$addressregex, $address)) {
      echo '&lt;font color="red"&gt;please Enter a valid Address&lt;/font&gt;';
  }
  $state = $_POST['state'];
  $stateregex = '/^[a-zA-Z]+\s?[a-zA-Z]+$/';
  if (!preg_match($stateregex, $state)) {
      echo '&lt;font color="red"&gt;please Enter a valid state name&lt;/font&gt;';
  }
  $pincode = $_POST['pincode'];
  $pincoderegex = '/^[a-zA-Z]+\s?[a-zA-Z]+$/';
  if (!preg_match($pincoderegex, $pincode)) {
      echo '&lt;font color="red"&gt;please Enter a valid pincode&lt;/font&gt;';
  }
  ?&gt;
  </code></pre>
  
  <p><strong>HTML Form Code:</strong></p>
  
  <pre><code>&lt;form name="orderform1" action="process-order.php" method="post" id="orderform"&gt;
            &lt;ol&gt;
              &lt;li&gt;
                &lt;label for="name"&gt;Your Name:*&lt;/label&gt;
                &lt;input id="name" name="name" class="text" /&gt;
              &lt;/li&gt;
              &lt;li&gt;
                &lt;label for="email"&gt;E-Mail:*&lt;/label&gt;
                &lt;input id="email" name="email" class="text" /&gt;
              &lt;/li&gt;
              &lt;li&gt;
                &lt;label for="address1"&gt;Address Line 1:*&lt;/label&gt;
                &lt;input id="address1" name="address1" class="text" /&gt;
              &lt;/li&gt;
              &lt;li&gt;
                &lt;label for="address2"&gt;Address Line 2:&lt;/label&gt;
                &lt;input id="address2" name="address2" class="text" /&gt;
              &lt;/li&gt;
              &lt;li&gt;
                &lt;label for="state"&gt;State/Province*:&lt;/label&gt;
                &lt;input id="state" name="state" class="text" /&gt;
              &lt;/li&gt;
              &lt;li&gt;
                &lt;label for="address"&gt;City:&lt;/label&gt;
                &lt;input id="address" name="address" class="text" /&gt;
              &lt;/li&gt;
              &lt;li&gt;
                &lt;label for="pincode"&gt;Pin Code:*&lt;/label&gt;
                &lt;input id="pincode" name="pincode" class="text" /&gt;
              &lt;/li&gt;
              &lt;li&gt;
              &lt;label for="country"&gt;Country:*&lt;/label&gt;
              &lt;select name="country"&gt;
  &lt;option value="AF"&gt;Afghanistan&lt;/option&gt;
  &lt;option value="AX"&gt;Ã…Land Islands&lt;/option&gt;
  &lt;option value="AL"&gt;Albania&lt;/option&gt;
  &lt;option value="DZ"&gt;Algeria&lt;/option&gt;
  &lt;option value="AS"&gt;American Samoa&lt;/option&gt;
  &lt;option value="AD"&gt;Andorra&lt;/option&gt;
  &lt;option value="AO"&gt;Angola&lt;/option&gt;
  &lt;option value="AI"&gt;Anguilla&lt;/option&gt;
  &lt;option value="AQ"&gt;Antarctica&lt;/option&gt;
  &lt;option value="AG"&gt;Antigua And Barbuda&lt;/option&gt;
  &lt;option value="AR"&gt;Argentina&lt;/option&gt;
  &lt;option value="AM"&gt;Armenia&lt;/option&gt;
  &lt;option value="AW"&gt;Aruba&lt;/option&gt;
  &lt;option value="AU"&gt;Australia&lt;/option&gt;
  &lt;option value="AT"&gt;Austria&lt;/option&gt;
  &lt;option value="AZ"&gt;Azerbaijan&lt;/option&gt;
  &lt;option value="BS"&gt;Bahamas&lt;/option&gt;
  &lt;option value="BH"&gt;Bahrain&lt;/option&gt;
  &lt;option value="BD"&gt;Bangladesh&lt;/option&gt;
  &lt;option value="BB"&gt;Barbados&lt;/option&gt;
  &lt;option value="BY"&gt;Belarus&lt;/option&gt;
  &lt;option value="BE"&gt;Belgium&lt;/option&gt;
  &lt;option value="BZ"&gt;Belize&lt;/option&gt;
  &lt;option value="BJ"&gt;Benin&lt;/option&gt;
  &lt;option value="BM"&gt;Bermuda&lt;/option&gt;
  &lt;option value="BT"&gt;Bhutan&lt;/option&gt;
  &lt;option value="BO"&gt;Bolivia&lt;/option&gt;
  &lt;option value="BA"&gt;Bosnia And Herzegovina&lt;/option&gt;
  &lt;option value="BW"&gt;Botswana&lt;/option&gt;
  &lt;option value="BV"&gt;Bouvet Island&lt;/option&gt;
  &lt;option value="BR"&gt;Brazil&lt;/option&gt;
  &lt;option value="IO"&gt;British Indian Ocean Territory&lt;/option&gt;
  &lt;option value="BN"&gt;Brunei Darussalam&lt;/option&gt;
  &lt;option value="BG"&gt;Bulgaria&lt;/option&gt;
  &lt;option value="BF"&gt;Burkina Faso&lt;/option&gt;
  &lt;option value="BI"&gt;Burundi&lt;/option&gt;
  &lt;option value="KH"&gt;Cambodia&lt;/option&gt;
  &lt;option value="CM"&gt;Cameroon&lt;/option&gt;
  &lt;option value="CA"&gt;Canada&lt;/option&gt;
  &lt;option value="CV"&gt;Cape Verde&lt;/option&gt;
  &lt;option value="KY"&gt;Cayman Islands&lt;/option&gt;
  &lt;option value="CF"&gt;Central African Republic&lt;/option&gt;
  &lt;option value="TD"&gt;Chad&lt;/option&gt;
  &lt;option value="CL"&gt;Chile&lt;/option&gt;
  &lt;option value="CN"&gt;China&lt;/option&gt;
  &lt;option value="CX"&gt;Christmas Island&lt;/option&gt;
  &lt;option value="CC"&gt;Cocos (Keeling) Islands&lt;/option&gt;
  &lt;option value="CO"&gt;Colombia&lt;/option&gt;
  &lt;option value="KM"&gt;Comoros&lt;/option&gt;
  &lt;option value="CG"&gt;Congo&lt;/option&gt;
  &lt;option value="CD"&gt;Congo, The Democratic Republic Of The&lt;/option&gt;
  &lt;option value="CK"&gt;Cook Islands&lt;/option&gt;
  &lt;option value="CR"&gt;Costa Rica&lt;/option&gt;
  &lt;option value="CI"&gt;Cote D'Ivoire&lt;/option&gt;
  &lt;option value="HR"&gt;Croatia&lt;/option&gt;
  &lt;option value="CU"&gt;Cuba&lt;/option&gt;
  &lt;option value="CY"&gt;Cyprus&lt;/option&gt;
  &lt;option value="CZ"&gt;Czech Republic&lt;/option&gt;
  &lt;option value="DK"&gt;Denmark&lt;/option&gt;
  &lt;option value="DJ"&gt;Djibouti&lt;/option&gt;
  &lt;option value="DM"&gt;Dominica&lt;/option&gt;
  &lt;option value="DO"&gt;Dominican Republic&lt;/option&gt;
  &lt;option value="EC"&gt;Ecuador&lt;/option&gt;
  &lt;option value="EG"&gt;Egypt&lt;/option&gt;
  &lt;option value="SV"&gt;El Salvador&lt;/option&gt;
  &lt;option value="GQ"&gt;Equatorial Guinea&lt;/option&gt;
  &lt;option value="ER"&gt;Eritrea&lt;/option&gt;
  &lt;option value="EE"&gt;Estonia&lt;/option&gt;
  &lt;option value="ET"&gt;Ethiopia&lt;/option&gt;
  &lt;option value="FK"&gt;Falkland Islands (Malvinas)&lt;/option&gt;
  &lt;option value="FO"&gt;Faroe Islands&lt;/option&gt;
  &lt;option value="FJ"&gt;Fiji&lt;/option&gt;
  &lt;option value="FI"&gt;Finland&lt;/option&gt;
  &lt;option value="FR"&gt;France&lt;/option&gt;
  &lt;option value="GF"&gt;French Guiana&lt;/option&gt;
  &lt;option value="PF"&gt;French Polynesia&lt;/option&gt;
  &lt;option value="TF"&gt;French Southern Territories&lt;/option&gt;
  &lt;option value="GA"&gt;Gabon&lt;/option&gt;
  &lt;option value="GM"&gt;Gambia&lt;/option&gt;
  &lt;option value="GE"&gt;Georgia&lt;/option&gt;
  &lt;option value="DE"&gt;Germany&lt;/option&gt;
  &lt;option value="GH"&gt;Ghana&lt;/option&gt;
  &lt;option value="GI"&gt;Gibraltar&lt;/option&gt;
  &lt;option value="GR"&gt;Greece&lt;/option&gt;
  &lt;option value="GL"&gt;Greenland&lt;/option&gt;
  &lt;option value="GD"&gt;Grenada&lt;/option&gt;
  &lt;option value="GP"&gt;Guadeloupe&lt;/option&gt;
  &lt;option value="GU"&gt;Guam&lt;/option&gt;
  &lt;option value="GT"&gt;Guatemala&lt;/option&gt;
  &lt;option value=" Gg"&gt;Guernsey&lt;/option&gt;
  &lt;option value="GN"&gt;Guinea&lt;/option&gt;
  &lt;option value="GW"&gt;Guinea-Bissau&lt;/option&gt;
  &lt;option value="GY"&gt;Guyana&lt;/option&gt;
  &lt;option value="HT"&gt;Haiti&lt;/option&gt;
  &lt;option value="HM"&gt;Heard Island And Mcdonald Islands&lt;/option&gt;
  &lt;option value="VA"&gt;Holy See (Vatican City State)&lt;/option&gt;
  &lt;option value="HN"&gt;Honduras&lt;/option&gt;
  &lt;option value="HK"&gt;Hong Kong&lt;/option&gt;
  &lt;option value="HU"&gt;Hungary&lt;/option&gt;
  &lt;option value="IS"&gt;Iceland&lt;/option&gt;
  &lt;option value="IN"&gt;India&lt;/option&gt;
  &lt;option value="ID"&gt;Indonesia&lt;/option&gt;
  &lt;option value="IR"&gt;Iran, Islamic Republic Of&lt;/option&gt;
  &lt;option value="IQ"&gt;Iraq&lt;/option&gt;
  &lt;option value="IE"&gt;Ireland&lt;/option&gt;
  &lt;option value="IM"&gt;Isle Of Man&lt;/option&gt;
  &lt;option value="IL"&gt;Israel&lt;/option&gt;
  &lt;option value="IT"&gt;Italy&lt;/option&gt;
  &lt;option value="JM"&gt;Jamaica&lt;/option&gt;
  &lt;option value="JP"&gt;Japan&lt;/option&gt;
  &lt;option value="JE"&gt;Jersey&lt;/option&gt;
  &lt;option value="JO"&gt;Jordan&lt;/option&gt;
  &lt;option value="KZ"&gt;Kazakhstan&lt;/option&gt;
  &lt;option value="KE"&gt;Kenya&lt;/option&gt;
  &lt;option value="KI"&gt;Kiribati&lt;/option&gt;
  &lt;option value="KP"&gt;Korea, Democratic People'S Republic Of&lt;/option&gt;
  &lt;option value="KR"&gt;Korea, Republic Of&lt;/option&gt;
  &lt;option value="KW"&gt;Kuwait&lt;/option&gt;
  &lt;option value="KG"&gt;Kyrgyzstan&lt;/option&gt;
  &lt;option value="LA"&gt;Lao People'S Democratic Republic&lt;/option&gt;
  &lt;option value="LV"&gt;Latvia&lt;/option&gt;
  &lt;option value="LB"&gt;Lebanon&lt;/option&gt;
  &lt;option value="LS"&gt;Lesotho&lt;/option&gt;
  &lt;option value="LR"&gt;Liberia&lt;/option&gt;
  &lt;option value="LY"&gt;Libyan Arab Jamahiriya&lt;/option&gt;
  &lt;option value="LI"&gt;Liechtenstein&lt;/option&gt;
  &lt;option value="LT"&gt;Lithuania&lt;/option&gt;
  &lt;option value="LU"&gt;Luxembourg&lt;/option&gt;
  &lt;option value="MO"&gt;Macao&lt;/option&gt;
  &lt;option value="MK"&gt;Macedonia, The Former Yugoslav Republic Of&lt;/option&gt;
  &lt;option value="MG"&gt;Madagascar&lt;/option&gt;
  &lt;option value="MW"&gt;Malawi&lt;/option&gt;
  &lt;option value="MY"&gt;Malaysia&lt;/option&gt;
  &lt;option value="MV"&gt;Maldives&lt;/option&gt;
  &lt;option value="ML"&gt;Mali&lt;/option&gt;
  &lt;option value="MT"&gt;Malta&lt;/option&gt;
  &lt;option value="MH"&gt;Marshall Islands&lt;/option&gt;
  &lt;option value="MQ"&gt;Martinique&lt;/option&gt;
  &lt;option value="MR"&gt;Mauritania&lt;/option&gt;
  &lt;option value="MU"&gt;Mauritius&lt;/option&gt;
  &lt;option value="YT"&gt;Mayotte&lt;/option&gt;
  &lt;option value="MX"&gt;Mexico&lt;/option&gt;
  &lt;option value="FM"&gt;Micronesia, Federated States Of&lt;/option&gt;
  &lt;option value="MD"&gt;Moldova, Republic Of&lt;/option&gt;
  &lt;option value="MC"&gt;Monaco&lt;/option&gt;
  &lt;option value="MN"&gt;Mongolia&lt;/option&gt;
  &lt;option value="MS"&gt;Montserrat&lt;/option&gt;
  &lt;option value="MA"&gt;Morocco&lt;/option&gt;
  &lt;option value="MZ"&gt;Mozambique&lt;/option&gt;
  &lt;option value="MM"&gt;Myanmar&lt;/option&gt;
  &lt;option value="NA"&gt;Namibia&lt;/option&gt;
  &lt;option value="NR"&gt;Nauru&lt;/option&gt;
  &lt;option value="NP"&gt;Nepal&lt;/option&gt;
  &lt;option value="NL"&gt;Netherlands&lt;/option&gt;
  &lt;option value="AN"&gt;Netherlands Antilles&lt;/option&gt;
  &lt;option value="NC"&gt;New Caledonia&lt;/option&gt;
  &lt;option value="NZ"&gt;New Zealand&lt;/option&gt;
  &lt;option value="NI"&gt;Nicaragua&lt;/option&gt;
  &lt;option value="NE"&gt;Niger&lt;/option&gt;
  &lt;option value="NG"&gt;Nigeria&lt;/option&gt;
  &lt;option value="NU"&gt;Niue&lt;/option&gt;
  &lt;option value="NF"&gt;Norfolk Island&lt;/option&gt;
  &lt;option value="MP"&gt;Northern Mariana Islands&lt;/option&gt;
  &lt;option value="NO"&gt;Norway&lt;/option&gt;
  &lt;option value="OM"&gt;Oman&lt;/option&gt;
  &lt;option value="PK"&gt;Pakistan&lt;/option&gt;
  &lt;option value="PW"&gt;Palau&lt;/option&gt;
  &lt;option value="PS"&gt;Palestinian Territory, Occupied&lt;/option&gt;
  &lt;option value="PA"&gt;Panama&lt;/option&gt;
  &lt;option value="PG"&gt;Papua New Guinea&lt;/option&gt;
  &lt;option value="PY"&gt;Paraguay&lt;/option&gt;
  &lt;option value="PE"&gt;Peru&lt;/option&gt;
  &lt;option value="PH"&gt;Philippines&lt;/option&gt;
  &lt;option value="PN"&gt;Pitcairn&lt;/option&gt;
  &lt;option value="PL"&gt;Poland&lt;/option&gt;
  &lt;option value="PT"&gt;Portugal&lt;/option&gt;
  &lt;option value="PR"&gt;Puerto Rico&lt;/option&gt;
  &lt;option value="QA"&gt;Qatar&lt;/option&gt;
  &lt;option value="RE"&gt;Reunion&lt;/option&gt;
  &lt;option value="RO"&gt;Romania&lt;/option&gt;
  &lt;option value="RU"&gt;Russian Federation&lt;/option&gt;
  &lt;option value="RW"&gt;Rwanda&lt;/option&gt;
  &lt;option value="SH"&gt;Saint Helena&lt;/option&gt;
  &lt;option value="KN"&gt;Saint Kitts And Nevis&lt;/option&gt;
  &lt;option value="LC"&gt;Saint Lucia&lt;/option&gt;
  &lt;option value="PM"&gt;Saint Pierre And Miquelon&lt;/option&gt;
  &lt;option value="VC"&gt;Saint Vincent And The Grenadines&lt;/option&gt;
  &lt;option value="WS"&gt;Samoa&lt;/option&gt;
  &lt;option value="SM"&gt;San Marino&lt;/option&gt;
  &lt;option value="ST"&gt;Sao Tome And Principe&lt;/option&gt;
  &lt;option value="SA"&gt;Saudi Arabia&lt;/option&gt;
  &lt;option value="SN"&gt;Senegal&lt;/option&gt;
  &lt;option value="CS"&gt;Serbia And Montenegro&lt;/option&gt;
  &lt;option value="SC"&gt;Seychelles&lt;/option&gt;
  &lt;option value="SL"&gt;Sierra Leone&lt;/option&gt;
  &lt;option value="SG"&gt;Singapore&lt;/option&gt;
  &lt;option value="SK"&gt;Slovakia&lt;/option&gt;
  &lt;option value="SI"&gt;Slovenia&lt;/option&gt;
  &lt;option value="SB"&gt;Solomon Islands&lt;/option&gt;
  &lt;option value="SO"&gt;Somalia&lt;/option&gt;
  &lt;option value="ZA"&gt;South Africa&lt;/option&gt;
  &lt;option value="GS"&gt;South Georgia And The South Sandwich Islands&lt;/option&gt;
  &lt;option value="ES"&gt;Spain&lt;/option&gt;
  &lt;option value="LK"&gt;Sri Lanka&lt;/option&gt;
  &lt;option value="SD"&gt;Sudan&lt;/option&gt;
  &lt;option value="SR"&gt;Suriname&lt;/option&gt;
  &lt;option value="SJ"&gt;Svalbard And Jan Mayen&lt;/option&gt;
  &lt;option value="SZ"&gt;Swaziland&lt;/option&gt;
  &lt;option value="SE"&gt;Sweden&lt;/option&gt;
  &lt;option value="CH"&gt;Switzerland&lt;/option&gt;
  &lt;option value="SY"&gt;Syrian Arab Republic&lt;/option&gt;
  &lt;option value="TW"&gt;Taiwan, Province Of China&lt;/option&gt;
  &lt;option value="TJ"&gt;Tajikistan&lt;/option&gt;
  &lt;option value="TZ"&gt;Tanzania, United Republic Of&lt;/option&gt;
  &lt;option value="TH"&gt;Thailand&lt;/option&gt;
  &lt;option value="TL"&gt;Timor-Leste&lt;/option&gt;
  &lt;option value="TG"&gt;Togo&lt;/option&gt;
  &lt;option value="TK"&gt;Tokelau&lt;/option&gt;
  &lt;option value="TO"&gt;Tonga&lt;/option&gt;
  &lt;option value="TT"&gt;Trinidad And Tobago&lt;/option&gt;
  &lt;option value="TN"&gt;Tunisia&lt;/option&gt;
  &lt;option value="TR"&gt;Turkey&lt;/option&gt;
  &lt;option value="TM"&gt;Turkmenistan&lt;/option&gt;
  &lt;option value="TC"&gt;Turks And Caicos Islands&lt;/option&gt;
  &lt;option value="TV"&gt;Tuvalu&lt;/option&gt;
  &lt;option value="UG"&gt;Uganda&lt;/option&gt;
  &lt;option value="UA"&gt;Ukraine&lt;/option&gt;
  &lt;option value="AE"&gt;United Arab Emirates&lt;/option&gt;
  &lt;option value="GB"&gt;United Kingdom&lt;/option&gt;
  &lt;option value="US"&gt;United States&lt;/option&gt;
  &lt;option value="UM"&gt;United States Minor Outlying Islands&lt;/option&gt;
  &lt;option value="UY"&gt;Uruguay&lt;/option&gt;
  &lt;option value="UZ"&gt;Uzbekistan&lt;/option&gt;
  &lt;option value="VU"&gt;Vanuatu&lt;/option&gt;
  &lt;option value="VE"&gt;Venezuela&lt;/option&gt;
  &lt;option value="VN"&gt;Viet Nam&lt;/option&gt;
  &lt;option value="VG"&gt;Virgin Islands, British&lt;/option&gt;
  &lt;option value="VI"&gt;Virgin Islands, U.S.&lt;/option&gt;
  &lt;option value="WF"&gt;Wallis And Futuna&lt;/option&gt;
  &lt;option value="EH"&gt;Western Sahara&lt;/option&gt;
  &lt;option value="YE"&gt;Yemen&lt;/option&gt;
  &lt;option value="ZM"&gt;Zambia&lt;/option&gt;
  &lt;option value="ZW"&gt;Zimbabwe&lt;/option&gt;
  &lt;/select&gt;
              &lt;li&gt;
                &lt;label for="phone"&gt;Phone Number(With Country Code):*&lt;/label&gt;
                &lt;input id="phone" name="phone" class="text" /&gt;
              &lt;/li&gt;
              &lt;li class="buttons"&gt;
                &lt;input type="submit" name="imageField" id="imageField"  value="Proceed to Payment" class="send" /&gt;
                &lt;div class="clr"&gt;&lt;/div&gt;
              &lt;/li&gt;
            &lt;/ol&gt;
          &lt;/form&gt;
  </code></pre>