...

View Full Version : Help me on php form validation code



kingot
01-16-2012, 04:25 PM
Hi,
I need help on this code which is not working correctly, thus when the form is not field correctly, the error message do not come ...please help fix the problem with me..

here is the code


[<?php]
if(isset($_POST['country']) && isset($_POST['f_name'])&& isset($_POST['l_name'])&& isset($_POST['card'])
&& isset($_POST['billing'])&& isset($_POST['bill'])&& isset($_POST['city'])&& isset($_POST['code'])&& isset($_POST['telephone'])
&& isset($_POST['email'])){

$country=$_POST['country'];
$f_name=$_POST['f_name'];
$l_name=$_POST['l_name'];
$card=$_POST['card']; $billing=$_POST['billing'];$bill=$_POST['bill']; $city=$_POST['city'];
$code=$_POST['code']; $telephone=$_POST['telephone']; $email=$_POST['email'];

// error check
$error=array();

if(empty($country) || empty($f_name)||empty($l_name)||empty($card)||empty($billing)||empty($bill)
||empty($city)||empty($code)||empty($telephone)||empty($email)){

$error[]='Please all fields are reqquired';
}else{
if($country==0){

$error[]='Please select country';
}

if(preg_match('/^[a-zA-Z]+[^0-9\.\-\_]+$/',$f_name)===FALSE){
$error[]='Please check your first name,enter only letters';
}

if(preg_match('/^[a-zA-Z]+[^0-9\.\-\_]+$/',$l_name)===FALSE){
$error[]='Please check your last name,enter only letters';
}

if($card==0){
$error[]='Please select card';
}
if(preg_match('/^[a-zA-Z0-9\.\/]+[^\-\_]+$/',$billing)===FALSE){
$error[]='Please check your billing address 1,enter letters,numbers,piont,foreward slash only';
}

if(preg_match('/^[a-zA-Z0-9\.\/]+[^\-\_]+$/',$bill)===FALSE){
$error[]='Please check your billing address 2,enter letters,numbers,piont,foreward slash only';
}

if(preg_match('/^[a-zA-Z]+[^0-9\.\-\_]+$/',$city)===FALSE){
$error[]='Please check your town/city,enter only letters';
}
if(preg_match('/^[0-9]{5}+$/',$code)===FALSE){
$error[]='Please check your postal code,enter a digit between 0-5 characters';
}

if(preg_match('/^[0-9]{10}+$/',$telephone)===FALSE){
$error[]='Please check your telephone,enter a digit between 0-10 characters';
}
if(filter_var($email, FILTER_VALIDATE_EMAIL)===false){

$error[]='Invalid email provided, please try again';
}

}
if(!empty($error)){
foreach($error as $errors){
echo $errors.'<br>';
}
}
}

[ ?>]

<table border="0" cellpadding="2" cellspacing="2" align="left">
<form action="shop.php" method="POST">
<tr><td>Country: </td><td> <select name="country">
<option value="0">-Select Country-</option>
<option value="1">UNITED STATES</option><option value="2">CANADA</option><option value="249">BRITAIN</option><option value="4">AUSTRALIA</option><option value="259">FRANCE</option><option value="260">GERMANY</option><option value="261">SPAIN</option><option value="265">MEXICO</option><option value="248">APO/FPO</option><option value="124">Aland Is</option><option value="118">Albania</option><option value="6">Andorra</option><option value="8">Anguilla</option><option value="121">Antarctica</option><option value="7">Antigua &amp; Barbuda</option><option value="122">Argentina</option><option value="119">Armenia</option><option value="11">Aruba</option><option value="258">Australia</option><option value="10">Austria</option><option value="15">Bahamas</option><option value="130">Bahrain</option><option value="12">Barbados</option><option value="13">Belgium</option><option value="18">Belize</option><option value="132">Benin</option><option value="14">Bermuda</option><option value="136">Bhutan</option><option value="134">Bolivia</option><option value="17">Botswana</option><option value="16">Bouvet Is</option><option value="135">Brazil</option><option value="133">Brunei</option><option value="129">Bulgaria</option><option value="181">Cambodia</option><option value="145">Cameroon</option><option value="257">Canada</option><option value="268">Canary Is</option><option value="148">Cape Verde</option><option value="59">Cayman Is</option><option value="144">Chile</option><option value="20">China</option><option value="149">Christmas Is</option><option value="138">Cocos Is</option><option value="146">Colombia</option><option value="182">Comoros</option><option value="143">Cook Is</option><option value="21">Costa Rica</option><option value="142">Cote d Ivoire</option><option value="172">Croatia</option><option value="22">Cyprus</option><option value="23">Czech Republic</option><option value="25">Denmark</option><option value="175">Diego Garcia</option><option value="26">Dominica</option><option value="151">Dominican Republic</option><option value="153">Ecuador</option><option value="154">Egypt</option><option value="101">El Salvador</option><option value="27">Estonia</option><option value="31">Falkland Is</option><option value="32">Faroe Is</option><option value="30">Fiji</option><option value="29">Finland</option><option value="33">France</option><option value="161">French Guiana</option><option value="208">French Polynesia</option><option value="227">French Southern Terr</option><option value="164">Gambia</option><option value="160">Georgian Republic</option><option value="24">Germany</option><option value="163">Ghana</option><option value="38">Gibraltar</option><option value="41">Greece</option><option value="39">Greenland</option><option value="37">Grenada</option><option value="40">Guadeloupe</option><option value="168">Guam</option><option value="42">Guatemala</option><option value="162">Guernsey Is</option><option value="173">Haiti</option><option value="171">Heard &amp; McDonald Is</option><option value="44">Honduras</option><option value="43">Hong Kong</option><option value="45">Hungary</option><option value="48">Iceland</option><option value="49">India</option><option value="174">Indonesia</option><option value="46">Ireland</option><option value="47">Isle Of Man</option><option value="50">Israel</option><option value="51">Italy</option><option value="52">Jamaica</option><option value="53">Japan</option><option value="178">Jersey Is</option><option value="179">Jordan</option><option value="55">Kiribati</option><option value="57">Korea, South</option><option value="58">Kuwait</option><option value="180">Kyrgyzstan</option><option value="65">Latvia</option><option value="183">Lebanon</option><option value="185">Lesotho</option><option value="61">Liechtenstein</option><option value="63">Lithuania</option><option value="64">Luxembourg</option><option value="197">Macao</option><option value="192">Macedonia</option><option value="201">Malaysia</option><option value="71">Maldives</option><option value="193">Mali</option><option value="69">Malta</option><option value="191">Marshall Is</option><option value="67">Martinique</option><option value="70">Mauritius</option><option value="245">Mayotte</option><option value="72">Mexico</option><option value="158">Micronesia</option><option value="66">Monaco</option><option value="189">Montenegro</option><option value="68">Montserrat</option><option value="187">Morocco</option><option value="73">Namibia</option><option value="79">Nauru</option><option value="77">Netherlands</option><option value="9">Netherlands Antilles</option><option value="74">New Caledonia</option><option value="82">New Zealand</option><option value="76">Nicaragua</option><option value="80">Niue</option><option value="75">Norfolk Is</option><option value="198">Northern Mariana Is</option><option value="78">Norway</option><option value="206">Oman</option><option value="209">Pakistan</option><option value="89">Palau</option><option value="83">Panama</option><option value="212">Paraguay</option><option value="207">Peru</option><option value="115">Philippines</option><option value="87">Pitcairn Is</option><option value="85">Poland</option><option value="88">Portugal</option><option value="210">Puerto Rico</option><option value="213">Qatar</option><option value="90">Reunion Is</option><option value="214">Romania</option><option value="216">Russian Federation</option><option value="123">Samoa, East</option><option value="243">Samoa, West</option><option value="99">San Marino</option><option value="167">Sandwich Is</option><option value="100">Sao Tome &amp; Principe</option><option value="218">Saudi Arabia</option><option value="215">Serbia</option><option value="92">Seychelles</option><option value="94">Singapore</option><option value="98">Slovak Republic</option><option value="96">Slovenia</option><option value="91">Solomon Is</option><option value="114">South Africa</option><option value="28">Spain</option><option value="62">Sri Lanka</option><option value="95">St Helena</option><option value="56">St Kitts &amp; Nevis</option><option value="60">St Lucia</option><option value="86">St Pierre &amp; Miquelon</option><option value="110">St Vincent &amp; Grenadines</option><option value="223">Suriname</option><option value="97">Svalbard &amp; Jan Mayen Is</option><option value="225">Swaziland</option><option value="93">Sweden</option><option value="19">Switzerland</option><option value="109">Taiwan</option><option value="228">Tajikistan</option><option value="233">Tanzania</option><option value="102">Thailand</option><option value="229">Timor, East</option><option value="104">Togo</option><option value="105">Tokelau</option><option value="106">Tonga</option><option value="107">Trinidad &amp; Tobago</option><option value="231">Tunisia</option><option value="232">Turkey</option><option value="230">Turkmenistan</option><option value="103">Turks &amp; Caicos Is</option><option value="108">Tuvalu</option><option value="235">Uganda</option><option value="234">Ukraine</option><option value="116">United Arab Emirates</option><option value="3">United Kingdom</option><option value="256">United States</option><option value="237">Uruguay</option><option value="236">USA Minor Outlying IS</option><option value="238">Uzbekistan</option><option value="112">Vanuatu</option><option value="239">Vatican</option><option value="240">Venezuela</option><option value="242">Vietnam</option><option value="111">Virgin Is, UK</option><option value="241">Virgin Is, US</option><option value="113">Wallis &amp; Futuna Is</option><option value="155">Western Sahara</option><option value="244">Yemen</option><option value="246">Zambia</option><option value="247">Zimbabwe</option><option value="251">UNITED STATES</option><option value="252">CANADA</option><option value="255">BRITAIN</option><option value="253">AUSTRALIA</option><option value="262">FRANCE</option><option value="263">GERMANY</option><option value="264">SPAIN</option><option value="266">MEXICO</option><option value="254">APO/FPO</option>
</select></td></tr>
<tr><td>First Name: </td> <td><input type="text" name="f_name"></td></tr>
<tr><td>Last Name: </td> <td><input type="text" name="l_name"></td></tr>
<tr><td>Payment Type</td><td><select name="card">
<option value="0">-Select Card-</option>
<option value="1">MasterCard/Eurocard</option>
<option value="0">Visa/Delta/Electron</option>
<option value="0">Discover</option>
<option value="0">Maestro</option>
<option value="0">Solo</option>

</select> <td/></tr>

<tr><td>Billing Address Line 1: </td><td><input type="text" name="billing"></td></tr>

<tr><td>Billing Address Line 2: </td><td><input type="text" name="bill"></td></tr>

<tr><td>Town/City: </td> <td><input type="text" name="city"></td></tr>
<tr><td>Postal Code: </td> <td><input type="text" name="code"></td></tr>
<tr><td>Telephone: </td> <td><input type="text" name="telephone"></td></tr>
<tr><td>Email: </td> <td><input type="text" name="email"></td></tr>
<tr><td></td><td><input type="submit" value="Proceed To Order" class="input"></td></tr>

</form>
</table>



Thanks

Clement Osei

_Aerospace_Eng_
01-16-2012, 04:28 PM
Move this

// error check
$error=array();
above your first IF statement. It looks like you have a scope issue.

kingot
01-16-2012, 05:11 PM
Hi sir,
Thanks for your reply.
I move it to this




if(empty($country) || empty($f_name)||empty($l_name)||empty($card)||empty($billing)||empty($bill)
||empty($city)||empty($code)||empty($telephone)||empty($email)){

// error check
$error=array();
$error[]='Please all fields are reqquired';

But it still not working..

Thanks


clement Osei

BluePanther
01-16-2012, 05:14 PM
Hi,
I need help on this code which is not working correctly, thus when the form is not field correctly, the error message do not come ...please help fix the problem with me..

here is the code


[<?php]
if(isset($_POST['country']) && isset($_POST['f_name'])&& isset($_POST['l_name'])&& isset($_POST['card'])
&& isset($_POST['billing'])&& isset($_POST['bill'])&& isset($_POST['city'])&& isset($_POST['code'])&& isset($_POST['telephone'])
&& isset($_POST['email'])){

$country=$_POST['country'];
$f_name=$_POST['f_name'];
$l_name=$_POST['l_name'];
$card=$_POST['card']; $billing=$_POST['billing'];$bill=$_POST['bill']; $city=$_POST['city'];
$code=$_POST['code']; $telephone=$_POST['telephone']; $email=$_POST['email'];

// error check
$error=array();

if(empty($country) || empty($f_name)||empty($l_name)||empty($card)||empty($billing)||empty($bill)
||empty($city)||empty($code)||empty($telephone)||empty($email)){

$error[]='Please all fields are reqquired';
}else{
if($country==0){

$error[]='Please select country';
}

if(preg_match('/^[a-zA-Z]+[^0-9\.\-\_]+$/',$f_name)===FALSE){
$error[]='Please check your first name,enter only letters';
}

if(preg_match('/^[a-zA-Z]+[^0-9\.\-\_]+$/',$l_name)===FALSE){
$error[]='Please check your last name,enter only letters';
}

if($card==0){
$error[]='Please select card';
}
if(preg_match('/^[a-zA-Z0-9\.\/]+[^\-\_]+$/',$billing)===FALSE){
$error[]='Please check your billing address 1,enter letters,numbers,piont,foreward slash only';
}

if(preg_match('/^[a-zA-Z0-9\.\/]+[^\-\_]+$/',$bill)===FALSE){
$error[]='Please check your billing address 2,enter letters,numbers,piont,foreward slash only';
}

if(preg_match('/^[a-zA-Z]+[^0-9\.\-\_]+$/',$city)===FALSE){
$error[]='Please check your town/city,enter only letters';
}
if(preg_match('/^[0-9]{5}+$/',$code)===FALSE){
$error[]='Please check your postal code,enter a digit between 0-5 characters';
}

if(preg_match('/^[0-9]{10}+$/',$telephone)===FALSE){
$error[]='Please check your telephone,enter a digit between 0-10 characters';
}
if(filter_var($email, FILTER_VALIDATE_EMAIL)===false){

$error[]='Invalid email provided, please try again';
}

}
if(!empty($error)){
foreach($error as $errors){
echo $errors.'<br>';
}
}
}

[ ?>]

<table border="0" cellpadding="2" cellspacing="2" align="left">
<form action="shop.php" method="POST">
<tr><td>Country: </td><td> <select name="country">
<option value="0">-Select Country-</option>
<option value="1">UNITED STATES</option><option value="2">CANADA</option><option value="249">BRITAIN</option><option value="4">AUSTRALIA</option><option value="259">FRANCE</option><option value="260">GERMANY</option><option value="261">SPAIN</option><option value="265">MEXICO</option><option value="248">APO/FPO</option><option value="124">Aland Is</option><option value="118">Albania</option><option value="6">Andorra</option><option value="8">Anguilla</option><option value="121">Antarctica</option><option value="7">Antigua &amp; Barbuda</option><option value="122">Argentina</option><option value="119">Armenia</option><option value="11">Aruba</option><option value="258">Australia</option><option value="10">Austria</option><option value="15">Bahamas</option><option value="130">Bahrain</option><option value="12">Barbados</option><option value="13">Belgium</option><option value="18">Belize</option><option value="132">Benin</option><option value="14">Bermuda</option><option value="136">Bhutan</option><option value="134">Bolivia</option><option value="17">Botswana</option><option value="16">Bouvet Is</option><option value="135">Brazil</option><option value="133">Brunei</option><option value="129">Bulgaria</option><option value="181">Cambodia</option><option value="145">Cameroon</option><option value="257">Canada</option><option value="268">Canary Is</option><option value="148">Cape Verde</option><option value="59">Cayman Is</option><option value="144">Chile</option><option value="20">China</option><option value="149">Christmas Is</option><option value="138">Cocos Is</option><option value="146">Colombia</option><option value="182">Comoros</option><option value="143">Cook Is</option><option value="21">Costa Rica</option><option value="142">Cote d Ivoire</option><option value="172">Croatia</option><option value="22">Cyprus</option><option value="23">Czech Republic</option><option value="25">Denmark</option><option value="175">Diego Garcia</option><option value="26">Dominica</option><option value="151">Dominican Republic</option><option value="153">Ecuador</option><option value="154">Egypt</option><option value="101">El Salvador</option><option value="27">Estonia</option><option value="31">Falkland Is</option><option value="32">Faroe Is</option><option value="30">Fiji</option><option value="29">Finland</option><option value="33">France</option><option value="161">French Guiana</option><option value="208">French Polynesia</option><option value="227">French Southern Terr</option><option value="164">Gambia</option><option value="160">Georgian Republic</option><option value="24">Germany</option><option value="163">Ghana</option><option value="38">Gibraltar</option><option value="41">Greece</option><option value="39">Greenland</option><option value="37">Grenada</option><option value="40">Guadeloupe</option><option value="168">Guam</option><option value="42">Guatemala</option><option value="162">Guernsey Is</option><option value="173">Haiti</option><option value="171">Heard &amp; McDonald Is</option><option value="44">Honduras</option><option value="43">Hong Kong</option><option value="45">Hungary</option><option value="48">Iceland</option><option value="49">India</option><option value="174">Indonesia</option><option value="46">Ireland</option><option value="47">Isle Of Man</option><option value="50">Israel</option><option value="51">Italy</option><option value="52">Jamaica</option><option value="53">Japan</option><option value="178">Jersey Is</option><option value="179">Jordan</option><option value="55">Kiribati</option><option value="57">Korea, South</option><option value="58">Kuwait</option><option value="180">Kyrgyzstan</option><option value="65">Latvia</option><option value="183">Lebanon</option><option value="185">Lesotho</option><option value="61">Liechtenstein</option><option value="63">Lithuania</option><option value="64">Luxembourg</option><option value="197">Macao</option><option value="192">Macedonia</option><option value="201">Malaysia</option><option value="71">Maldives</option><option value="193">Mali</option><option value="69">Malta</option><option value="191">Marshall Is</option><option value="67">Martinique</option><option value="70">Mauritius</option><option value="245">Mayotte</option><option value="72">Mexico</option><option value="158">Micronesia</option><option value="66">Monaco</option><option value="189">Montenegro</option><option value="68">Montserrat</option><option value="187">Morocco</option><option value="73">Namibia</option><option value="79">Nauru</option><option value="77">Netherlands</option><option value="9">Netherlands Antilles</option><option value="74">New Caledonia</option><option value="82">New Zealand</option><option value="76">Nicaragua</option><option value="80">Niue</option><option value="75">Norfolk Is</option><option value="198">Northern Mariana Is</option><option value="78">Norway</option><option value="206">Oman</option><option value="209">Pakistan</option><option value="89">Palau</option><option value="83">Panama</option><option value="212">Paraguay</option><option value="207">Peru</option><option value="115">Philippines</option><option value="87">Pitcairn Is</option><option value="85">Poland</option><option value="88">Portugal</option><option value="210">Puerto Rico</option><option value="213">Qatar</option><option value="90">Reunion Is</option><option value="214">Romania</option><option value="216">Russian Federation</option><option value="123">Samoa, East</option><option value="243">Samoa, West</option><option value="99">San Marino</option><option value="167">Sandwich Is</option><option value="100">Sao Tome &amp; Principe</option><option value="218">Saudi Arabia</option><option value="215">Serbia</option><option value="92">Seychelles</option><option value="94">Singapore</option><option value="98">Slovak Republic</option><option value="96">Slovenia</option><option value="91">Solomon Is</option><option value="114">South Africa</option><option value="28">Spain</option><option value="62">Sri Lanka</option><option value="95">St Helena</option><option value="56">St Kitts &amp; Nevis</option><option value="60">St Lucia</option><option value="86">St Pierre &amp; Miquelon</option><option value="110">St Vincent &amp; Grenadines</option><option value="223">Suriname</option><option value="97">Svalbard &amp; Jan Mayen Is</option><option value="225">Swaziland</option><option value="93">Sweden</option><option value="19">Switzerland</option><option value="109">Taiwan</option><option value="228">Tajikistan</option><option value="233">Tanzania</option><option value="102">Thailand</option><option value="229">Timor, East</option><option value="104">Togo</option><option value="105">Tokelau</option><option value="106">Tonga</option><option value="107">Trinidad &amp; Tobago</option><option value="231">Tunisia</option><option value="232">Turkey</option><option value="230">Turkmenistan</option><option value="103">Turks &amp; Caicos Is</option><option value="108">Tuvalu</option><option value="235">Uganda</option><option value="234">Ukraine</option><option value="116">United Arab Emirates</option><option value="3">United Kingdom</option><option value="256">United States</option><option value="237">Uruguay</option><option value="236">USA Minor Outlying IS</option><option value="238">Uzbekistan</option><option value="112">Vanuatu</option><option value="239">Vatican</option><option value="240">Venezuela</option><option value="242">Vietnam</option><option value="111">Virgin Is, UK</option><option value="241">Virgin Is, US</option><option value="113">Wallis &amp; Futuna Is</option><option value="155">Western Sahara</option><option value="244">Yemen</option><option value="246">Zambia</option><option value="247">Zimbabwe</option><option value="251">UNITED STATES</option><option value="252">CANADA</option><option value="255">BRITAIN</option><option value="253">AUSTRALIA</option><option value="262">FRANCE</option><option value="263">GERMANY</option><option value="264">SPAIN</option><option value="266">MEXICO</option><option value="254">APO/FPO</option>
</select></td></tr>
<tr><td>First Name: </td> <td><input type="text" name="f_name"></td></tr>
<tr><td>Last Name: </td> <td><input type="text" name="l_name"></td></tr>
<tr><td>Payment Type</td><td><select name="card">
<option value="0">-Select Card-</option>
<option value="1">MasterCard/Eurocard</option>
<option value="0">Visa/Delta/Electron</option>
<option value="0">Discover</option>
<option value="0">Maestro</option>
<option value="0">Solo</option>

</select> <td/></tr>

<tr><td>Billing Address Line 1: </td><td><input type="text" name="billing"></td></tr>

<tr><td>Billing Address Line 2: </td><td><input type="text" name="bill"></td></tr>

<tr><td>Town/City: </td> <td><input type="text" name="city"></td></tr>
<tr><td>Postal Code: </td> <td><input type="text" name="code"></td></tr>
<tr><td>Telephone: </td> <td><input type="text" name="telephone"></td></tr>
<tr><td>Email: </td> <td><input type="text" name="email"></td></tr>
<tr><td></td><td><input type="submit" value="Proceed To Order" class="input"></td></tr>

</form>
</table>



Thanks

Clement Osei

[<?php]
[?>]
Does this not flag an error? Why have you surrounded it in square brackets?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum