please .. i have this php form <FORM action="add.php" method="post"> <TABLE width="100%" border=0> <TBODY> <TR> <TD align=center><FONT color=#ff0000>* </FONT>title:<BR><INPUT size=40 name=title><BR></TD> </TR> </TBODY> <TABLE width="100%" border=0> <TBODY> <TR> <TD align=center><FONT color=#ff0000>* </FONT>Url<BR><INPUT size=40 name=url value=http://><BR><TD></TR></TBODY></TABLE> </TD> </TR> <TABLE width="100%" border=0> <TBODY> <TR> <TD align=center>description<BR> <textarea cols="50" rows="4" name="description"></textarea><BR><TD></TR></TBODY></TABLE> </TD> </TBODY></TABLE> <P style="TEXT-ALIGN: center" align=center><INPUT class=submit type=submit value="add" name=send></TD> </FORM> Code (markup): and i want make the first 2 fields required ( title and url ) but i want the user don't leave the form if this 2 fields empty .. i need the user don't go the add.php if this fields empty or get Pop-up message .. like :: please fill the blank fields so can anyone help me ..
if(isset($_REQUEST['add']) { $error = 0; $error_msg = ''; if(!isset($_POST['title'] || (isset($_POST['title']) && strlen($_POST['title']) == 0)) { $error++; $error_msg .= 'You did not enter the title<br />'; } if(!isset($_POST['url'] || (isset($_POST['url']) && (strlen($_POST['url']) == 0 || $_POST['url'] == 'http://'))) { $error++; $error_msg .= 'You did not enter the url<br />'; } if($error > 0) { echo $error_msg; } else if($error == 0) { //insert form data into database. } } PHP:
If you want to use server side validation (means: user must submit the form first, and PHP analyze the form submitted), use tvoodoo solution. If you want clint site validation (means: when user click submit, the form still not submitted), you can check this page: w3schools.com/jS/js_form_validation.asp