how to separate html from php with working form validation -
i new php. working on simple contact form practise.it worked fine,exactly way want when tried separate html form.php file , paste in process.html , pressed submit button,without filling in of fields, form submits without doing form validation , empty data not inserted in db.
all want keep html , php both separate files , want submit form using form validation did.any please????
here code
form.html
<?php require_once "process.php";?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>untitled document</title> <link href="style.css" rel="stylesheet" type="text/css" media="all"> </head> <body> <div id="form_container"> <?php $arrmessage = "" ?> <div style="color:#f00"><?php if($arrmessage !=""){ echo $arrmessage; }? ></div> <div id="form_heading">new user registration</div> <form name="form1" id="form1" action="process.php?value=submit" method="post"> <div class="row_a"> <div class="cell cell_left">student name</div> <div class="cell cell_right"> <input type="text" id="student_name" name="student_name" value="" placeholder="student name"> </div> <div class="clear_box"></div> </div> <div class="row_b"> <div class="cell cell_left">email</div> <div class="cell cell_right"> <input type="email" id="email" name="email" value="" placeholder="email"> </div> <div class="clear_box"></div> </div> <div class="row_a"> <div class="cell cell_left">contact number</div> <div class="cell cell_right"> <input type="text" id="contact_number" name="contact_number" value="" placeholder="contact number"> </div> <div class="clear_box"></div> </div> <div class="row_b"> <div class="cell cell_left">gender:</div> <div class="cell cell_right">male <input type="radio" name="gender" value="male" / > female <input type="radio" name="gender" value="female" / > </div> <div class="clear_box"></div> </div> <div class="row_a"> <div class="cell cell_left">interest:</div> <div class="cell cell_right"> laptop<input type="checkbox" id="" name="interest[]" value="laptop" > i-pad<input type="checkbox" id="" name="interest[]" value="ipad" > mobile<input type="checkbox" id="" name="interest[]" value="mobile" > </div> <div class="clear_box"></div> </div> <div class="row_b"> <div class="cell cell_left">date of birth</div> <div class="cell cell_right"> <select name="day"> <option value="">day</option> <option>1</option> <option>2</option> <option>3</option> </select>- <select name="month"> <option value="">month</option> <option>january</option> <option>february</option> <option>march</option> </select>- <select name="year"> <option value="">year</option> <option>2001</option> <option>2002</option> <option>2003</option> </select> </div> <div class="clear_box"></div> </div> <div class="row_a"> <div class="cell cell_left">street adress</div> <div class="cell cell_right"> <textarea id="street_adress" name="street_adress" placeholder="street adress"></textarea> </div> <div class="clear_box"></div> </div> <div class="row_b"> <div class="cell cell_left">city</div> <div class="cell cell_right"> <input type="text" id="city" name="city" value="" placeholder="city" > </div> <div class="clear_box"></div> </div> <div class="row_a"> <div class="cell cell_left">state</div> <div class="cell cell_right"> <input type="text" id="state" name="state" value="" placeholder="state" > </div> <div class="clear_box"></div> </div> <div class="row_b"> <div class="cell cell_left">country</div> <div class="cell cell_right"> <select name="country"> <?php foreach($countries $list) echo "<option>". $list ."<br/>" . "</option>"; ?> </select> </div> <div class="clear_box"></div> </div> <div class="row_a"> <div class="cell cell_right"> <input name="submit" id="submit" type="submit" value="submit"> <br/> </div> <div class="clear_box"></div> </div> </form> <br/><a href="logout.php">log out</a> </div> </body> </html>
process.php
<?php session_start(); require_once("database.php"); if(!isset($_session['user_id']) && empty($_session['user_id'])){ header("location:login.php"); exit; } if(isset($_request['value'])){ //exit; if (isset($_post['submit'])) { if(empty($_post['student_name'] )){ $arrmessage = "please enter student name"; }elseif(empty($_post['email'] )){ $arrmessage = "please enter email"; }elseif(empty($_post['contact_number'] )){ $arrmessage = "please enter contact number"; }elseif(empty($_post['gender'] )){ $arrmessage = "please select gender"; }elseif(empty($_post['interest'] )){ $arrmessage = "please select interest"; }elseif(empty($_post['day'])){ $arrmessage = "please enter day"; }elseif(empty($_post['month'])){ $arrmessage = "please enter month"; }elseif(empty($_post['year'])){ $arrmessage = "please enter year"; }elseif(empty($_post['street_adress'] )){ $arrmessage = "please enter street_adress"; }elseif(empty($_post['city'])){ $arrmessage = "please enter city"; }elseif(empty($_post['state'] )){ $arrmessage = "please enter state"; }elseif(empty($_post['country'] )){ $arrmessage = "please enter country"; }else{ $arrmessage =""; } if($arrmessage =="") { $student_name = $_post['student_name']; $email = $_post['email']; $contact_number = $_post['contact_number']; $gender = $_post['gender']; $interest = $_post['interest']; $checked =""; foreach($interest $list){$checked .= $list .",";} $dob = $_post['day'].'-'.$_post['month'].'-'.$_post['year']; $street_adress = $_post['street_adress']; $city = $_post['city']; $state = $_post['state']; $country = $_post['country']; $sql = "insert form (student_name,email,contact_number,gender,interest,dob,street_adress,city,state,country)values('{$student_name}','{$email}','{$contact_number}','{$gender}','{$checked}','{$dob}','{$street_adress}','{$city}','{$state}','{$country}')"; mysql_query($sql); if(mysql_insert_id() != ""){ echo "records inserted successfully."; } header("location:views.php"); exit; } } } $countries = array("afghanistan", "albania", "algeria", "american samoa", "andorra", "angola", "anguilla", "antarctica", "antigua , barbuda", "argentina", "armenia", "aruba", "australia", "austria", "azerbaijan", "bahamas", "bahrain", "bangladesh", "barbados", "belarus", "belgium", "belize", "benin", "bermuda", "bhutan", "bolivia", "bosnia , herzegowina", "botswana", "bouvet island", "brazil", "british indian ocean territory", "brunei darussalam", "bulgaria", "burkina faso", "burundi", "cambodia", "cameroon", "canada", "cape verde", "cayman islands", "central african republic", "chad", "chile", "china", "christmas island", "cocos (keeling) islands", "colombia", "comoros", "congo", "congo, democratic republic of the", "cook islands", "costa rica", "cote d'ivoire", "croatia (hrvatska)", "cuba", "cyprus", "czech republic", "denmark", "djibouti", "dominica", "dominican republic", "east timor", "ecuador", "egypt", "el salvador", "equatorial guinea", "eritrea", "estonia", "ethiopia", "falkland islands (malvinas)", "faroe islands", "fiji", "finland", "france", "france metropolitan", "french guiana", "french polynesia", "french southern territories", "gabon", "gambia", "georgia", "germany", "ghana", "gibraltar", "greece", "greenland", "grenada", "guadeloupe", "guam", "guatemala", "guinea", "guinea-bissau", "guyana", "haiti", "heard , mc donald islands", "holy see (vatican city state)", "honduras", "hong kong", "hungary", "iceland", "india", "indonesia", "iran (islamic republic of)", "iraq", "ireland", "israel", "italy", "jamaica", "japan", "jordan", "kazakhstan", "kenya", "kiribati", "korea, democratic people's republic of", "korea, republic of", "kuwait", "kyrgyzstan", "lao, people's democratic republic", "latvia", "lebanon", "lesotho", "liberia", "libyan arab jamahiriya", "liechtenstein", "lithuania", "luxembourg", "macau", "macedonia, former yugoslav republic of", "madagascar", "malawi", "malaysia", "maldives", "mali", "malta", "marshall islands", "martinique", "mauritania", "mauritius", "mayotte", "mexico", "micronesia, federated states of", "moldova, republic of", "monaco", "mongolia", "montserrat", "morocco", "mozambique", "myanmar", "namibia", "nauru", "nepal", "netherlands", "netherlands antilles", "new caledonia", "new zealand", "nicaragua", "niger", "nigeria", "niue", "norfolk island", "northern mariana islands", "norway", "oman", "pakistan", "palau", "panama", "papua new guinea", "paraguay", "peru", "philippines", "pitcairn", "poland", "portugal", "puerto rico", "qatar", "reunion", "romania", "russian federation", "rwanda", "saint kitts , nevis", "saint lucia", "saint vincent , grenadines", "samoa", "san marino", "sao tome , principe", "saudi arabia", "senegal", "seychelles", "sierra leone", "singapore", "slovakia (slovak republic)", "slovenia", "solomon islands", "somalia", "south africa", "south georgia , south sandwich islands", "spain", "sri lanka", "st. helena", "st. pierre , miquelon", "sudan", "suriname", "svalbard , jan mayen islands", "swaziland", "sweden", "switzerland", "syrian arab republic", "taiwan, province of china", "tajikistan", "tanzania, united republic of", "thailand", "togo", "tokelau", "tonga", "trinidad , tobago", "tunisia", "turkey", "turkmenistan", "turks , caicos islands", "tuvalu", "uganda", "ukraine", "united arab emirates", "united kingdom", "united states", "united states minor outlying islands", "uruguay", "uzbekistan", "vanuatu", "venezuela", "vietnam", "virgin islands (british)", "virgin islands (u.s.)", "wallis , futuna islands", "western sahara", "yemen", "yugoslavia", "zambia", "zimbabwe"); ?>
you can not execute php code in html file.so, make form.html file extension form.php , pass parameter while redirecting form.php process.php if error occurs.
check if parameter available in form.php render message.
if($arrmessage =="") { $student_name = $_post['student_name']; $email = $_post['email']; $contact_number = $_post['contact_number']; $gender = $_post['gender']; $interest = $_post['interest']; $checked =""; foreach($interest $list){$checked .= $list .",";} $dob = $_post['day'].'-'.$_post['month'].'-'.$_post['year']; $street_adress = $_post['street_adress']; $city = $_post['city']; $state = $_post['state']; $country = $_post['country']; $sql = "insert form (student_name,email,contact_number,gender,interest,dob,street_adress,city,state,country)values('{$student_name}','{$email}','{$contact_number}','{$gender}','{$checked}','{$dob}','{$street_adress}','{$city}','{$state}','{$country}')"; mysql_query($sql); if(mysql_insert_id() != ""){ echo "records inserted successfully."; } header("location:views.php"); exit; } else { header("location:form.php?error=1&msg=$arrmessage"); }
at form.php file need check if $_get['error'] display message in $_get['msg']
Comments
Post a Comment