Jump to content

Form Feature Change/Help


john.grisum

Recommended Posts

I have finally got my form working but wish to change one feature to make it perfect. I am not 100% sure on how to do it properly. When the form is submitted with an incorrect amount of digits in the "IMEI" field (it requires 15 characters), another page opens with "Sorry, but there was an error found with the form you submitted....Please press the back button on your browser and fix this error." This is expected. I wish for the error to be a prompt on the same page when the person does not enter exactly 15 numerical digits. Is there something that I can add to the "required.add" (this would be ideal as it would make the prompt occur on the same page)? The test form is located at the bottom of the page here: http://freemyblackberry.com/freecode2.htmlHTML

<script src="lite_validation.js"></script><script>required.add('Email_Address','EMAIL','Please enter a valid email address.');required.add('IMEI','NOT_EMPTY','Please enter 15 digits for the IMEI. Do not include periods, spaces, or dashes.');required.add('CARRIER','NOT_EMPTY','Please enter a carrier name.');required.add('MODEL','NOT_EMPTY','Please enter a Model Number.');required.add('COUNTRY','NOT_EMPTY','Please enter your country.') </script>
PHP FORM
<?phpif(isset($_POST['Email_Address'])) { include 'lite_settings.php'; function died($error) { echo "Sorry, but there was an error found with the form you submitted. "; echo "<br /><br />"; echo $error."<br /><br />"; echo "Please press the back button on your browser and fix this error.<br /><br />"; die(); } if(!isset($_POST['Email_Address']) || !isset($_POST['IMEI']) || !isset($_POST['CARRIER']) || !isset($_POST['MODEL']) || !isset($_POST['MEP']) || !isset($_POST['COUNTRY'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $email_from = $_POST['Email_Address']; $IMEI = $_POST['IMEI']; // required $CARRIER = $_POST['CARRIER']; // not required $MODEL = $_POST['MODEL']; // required $MEP = $_POST['MEP']; $COUNTRY = $_POST['COUNTRY']; $error_message = ""; $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"; if(!eregi($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; } if(strlen($IMEI) < 15) { $error_message .= 'Please enter exactly 15 digits for the IMEI. Do not include periods, spaces, or dashes.<br><br> Acceptable IMEI: 123456789012345 (Perfect!) <br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\r\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Email: ".clean_string($email_from)."\r\n"; $email_message .= "IMEI: ".clean_string($IMEI)."\r\n"; $email_message .= "CARRIER: ".clean_string($CARRIER)."\r\n"; $email_message .= "MODEL: ".clean_string($MODEL)."\r\n"; $email_message .= "MEP: ".clean_string($MEP)."\r\n"; $email_message .= "COUNTRY: ".clean_string($COUNTRY)."\r\n"; $headers = 'From: '.$email_from."\r\n".'Reply-To: '.$email_from."\r\n" .'X-Mailer: PHP/' . phpversion();@mail($email_to, $email_subject, $email_message, $headers);header("Location: $thankyou");?><script>location.replace('<?php echo $thankyou;?>')</script><?}?>
Link to comment
Share on other sites

the common convention for this type of functionality is to do the form validating with Javascript when the form is submitted, or have the form submit to itself (the same page) and the use PHP to check each piece of information and display the appropriate messages.

Link to comment
Share on other sites

This is actually the fourth form I have tried to recode to use on my website. All the previous ones did not have the functions I needed. I have already lost patience in trying to just get this specific form working (well except for this one issue). I am sure though that there are just two or three lines that need to be changed/added and my lack of specific experience dictates that I do not know where to start with this specific issue. A specific question needs a specific answer... Telling me to look for a validation script is not specific enough. I have already looked at such information and have not found anything applicable, otherwise I would not be here. Well, I guess I'm stuck with what I have.

Link to comment
Share on other sites

well, you know how to use control structures, right? Like

if(something){  //something}else{  //do something};

To make a form submit to itself, you just make the form code and the script that handles the form all on the same page. So form.php would look something likes this:

if($_POST['submit']){  //form was submitted  //do form processing stuff  //check for validation}else{  //display HTML form code};

now a very simple way to do add some validation would be to test if the forms being submitted, like we are doing, but instead check all the form values, in sucession, and if everything checks out, then you can display an extra message to the user that something went wrong. So, now we might have something like this:

if($_POST['submit']){  //form was submitted  //do form processing stuff  //check for validation, etc  if(empty($_POST['Email_Address']) || empty($_POST['IMEI']) || empty($_POST['CARRIER']) ||	 empty($_POST['MODEL']) || empty($_POST['MEP']) || empty($_POST['COUNTRY'])) {	 $isFormValid = false;  }else{	 $isFormValid = true;  };}else{  if(!$isFormValid){	echo '<h3 style="color:red;">Sorry there was a problem with your form.  Please check your inputs and try again.</h3>';  };  //display HTML form code};

Link to comment
Share on other sites

I already have a validation file (below)... The problem is that when the person does not enter 15 digits it forwards to a new webpage...

function has_id(id){try{var tmp=document.getElementById(id).value;}catch(e){return false;}return true;}function has_name(nm){try{var tmp=cfrm.nm.type;}catch(e){return false;}return true;}function $$(id){if(!has_id(id)&&!has_name(id)){alert("Field "+id+" does not exist!\n Form validation configuration error.");return false;}if(has_id(id)){return document.getElementById(id).value;}else{return;}}function $val(id){return document.getElementById(id);}function trim(id){$val(id).value=$val(id).value.replace(/^\s+/,'').replace(/\s+$/,'');}var required={field:[],add:function(name,type,mess){this.field[this.field.length]=[name,type,mess];},out:function(){return this.field;},clear:function(){this.field=[];}};var validate={check:function(cform){var error_message='Please fix the following errors:\n\n';var mess_part='';var to_focus='';var tmp=true;for(var i=0;i<required.field.length;i++){if(this.checkit(required.field[0],required.field[1],cform)){}else{error_message=error_message+required.field[2]+' \n';if(has_id(required.field[0])&&to_focus.length===0){to_focus=required.field[0];}tmp=false;}}if(!tmp){alert(error_message);}if(to_focus.length>0){document.getElementById(to_focus).focus();}return tmp;},checkit:function(cvalue,ctype,cform){if(ctype=="NOT_EMPTY"){if(this.trim($$(cvalue)).length<1){return false;}else{return true;}}else if(ctype=="EMAIL"){exp=/^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;if($$(cvalue).match(exp)==null){return false;}else{return true;}}},trim:function(s){if(s.length>0){return s.replace(/^\s+/,'').replace(/\s+$/,'');}else{return s;}}};
Link to comment
Share on other sites

a simple js/php validation would go similar to

<form method="post" action="mobile.php" onsubmit="return validateall()"><input id="IMEI" class="text" type="text" size="50" onfocus='this.value="";this.onfocus="";'  value="123456789012345" maxlength="15" name="IMEI" onblur="validnumber();"/><input type="submit" value="submit IMEI" />

<script type="text/javascript">/*<![CDATA[*//*---->*/function validnumber(){str=document.getElementById("IMEI").value;if(str.length >15 || str.length <15 || isNaN(str)){alert("no good! try again")return false;}}function validateall(){valid = true;valid = validnumber();//valid= validmep(); // run several validation functions for other inputs before posting values to other page.return valid;}/*--*//*]]>*/</script>

<?php$error_message="";if(isset($_POST['IMEI'])){$IMEI=$_POST['IMEI'];if(is_numeric($IMEI)){echo 'Is a number'."\n";}else{echo 'NOT a number'."\n";} if(strlen($IMEI) < 15) {$error_message .= 'Please enter exactly 15 digits for the IMEI. Do not include periods, spaces, or dashes.<br><br> Acceptable IMEI: 123456789012345 (Perfect!) <br />';}}?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...