php - How to validate if user inserts http:// anywhere in form, display a error message? -
in form user can add urls of social sites. cannot add url http://
or https://
.
if user adds http://
or https://
in form submit; form had not submitted , redirect error page.
but want if user add http://
or https://
in form submit, display error message , not redirect on error page.
here form process. (mysql deprecated transfer later)
if(isset($_post['update_ac'])){ $web = mysql_real_escape_string($_post['web']); $fb = mysql_real_escape_string($_post['fb']); $tw = mysql_real_escape_string($_post['tw']); $gg = mysql_real_escape_string($_post['gg']); $fk = mysql_real_escape_string($_post['fk']); $rn = mysql_real_escape_string($_post['rn']); $yt = mysql_real_escape_string($_post['yt']); $ig = mysql_real_escape_string($_post['ig']); $it = mysql_real_escape_string($_post['it']); $ms = mysql_real_escape_string($_post['ms']); $pt = mysql_real_escape_string($_post['pt']); $sc = mysql_real_escape_string($_post['sc']); $tm = mysql_real_escape_string($_post['tm']); $vv = mysql_real_escape_string($_post['vv']); $ws = mysql_real_escape_string($_post['ws']); # array of keys check... $keys = array('web','fb','tw','gg','fk','rn','yt','ig','it','ms','pt','sc','tm','vv','ws'); # array of invalid strings to check for... $invalid_strings = array("http://","https://"); # array hold errors found... $errors = array(); foreach($keys $key) { # iterate through each key check foreach($invalid_strings $invalid) { # iterate through each invalid string if (strpos($_post[$key],$invlid) > -1) { $errors[] = "$key cannot contain '$invalid'"; } } } # if errors found... if (count($errors) > 0) { $error_msg = implode($errors,", "); echo $error_msg; } else { // update data in mysql database $sql = mysql_query("update social set web='$web', fb='$fb', tw='$tw', gg='$gg', fk='$fk', rn='$rn', yt='$yt', ig='$ig', it='$it', ms='$ms', pt='$pt', sc='$sc', tm='$tm', vv='$vv', ws='$ws' username = '".$_session['username']."'"); $result=mysql_query($sql); // if updated. if($result){ echo '<strong>updated successful</strong>'; echo '<meta http-equiv=refresh //content="0">'; } else { echo '<strong>sorry !</strong> not update, try again later'; } } } <form class="form-horizontal" role="form" name="form1" method="post" action=""> <input name="username" type="text" id="username" value="<? echo $session->username; ?> (cannot change)" disabled/> <input name="web" type="text" id="web" placeholder="enter url without http:// or https://" value="<? echo $web; ?>" size="15"> <input name="fb" type="text" id="fb" placeholder="enter url without http:// or https://" value="<? echo $fb; ?>" size="15"> <input name="tw" type="text" id="tw" placeholder="enter url without http:// or https://" value="<? echo $tw; ?>" size="15"> <input name="gg" type="text" id="gg" placeholder="enter url without http:// or https://" value="<? echo $gg; ?>" size="15"> <input name="fk" type="text" id="fk" placeholder="enter url without http:// or https://" value="<? echo $fk; ?>" size="15"> <input name="rn" type="text" id="rn" placeholder="enter url without http:// or https://" value="<? echo $rn; ?>" size="15"> <input name="yt" type="text" id="yt" placeholder="enter url without http:// or https://" value="<? echo $yt; ?>" size="15"> <input name="ig" type="text" id="ig" placeholder="enter url without http:// or https://" value="<? echo $ig; ?>" size="15"> <input name="it" type="text" id="it" placeholder="enter url without http:// or https://" value="<? echo $it; ?>" size="15"> <input name="ms" type="text" id="ms" placeholder="enter url without http:// or https://" value="<? echo $ms; ?>" size="15"> <input name="pt" type="text" id="pt" placeholder="enter url without http:// or https://" value="<? echo $pt; ?>" size="15"> <input name="sc" type="text" id="sc" placeholder="enter url without http:// or https://" value="<? echo $sc; ?>" size="15"> <input name="tm" type="text" id="tm" placeholder="enter url without http:// or https://" value="<? echo $tm; ?>" size="15"> <input name="vv" type="text" id="vv" placeholder="enter url without http:// or https://" value="<? echo $vv; ?>" size="15"> <input name="ws" type="text" id="ws" placeholder="enter url without http:// or https://" value="<? echo $ws; ?>" size="15"> <input name="userid" type="hidden" id="userid" value="<? echo $session->userid; ?>"> <input type="submit" name="update_ac" value="submit"> </form>
this way i'd handle it...
<?php # array of keys check... $keys = array('web','fb','tw','gg','fk','rn','yt','ig','it','ms','pt','sc','tm','vv','ws'); # array of invalid strings to check for... $invalid_strings = array("http://","https://"); # array hold errors found... $errors = array(); foreach($keys $key) { # iterate through each key check foreach($invalid_strings $invalid) { # iterate through each invalid string if (strpos($_post[$key],$invlid) > -1) { $errors[] = "$key cannot contain '$invalid'"; } } } # if errors found... if (count($errors) > 0) { $error_msg = implode($errors,", "); echo $error_msg; } else { # code run if no errors found } ?>
Comments
Post a Comment