I have a form that populates a database, and I'm having trouble with the error handling. I want the errors to show in a small pop up window on the same pg.
<form id="form1" name="form1" method="post" action="mailform.php" onsubmit="MM_validateForm('Name','','R','Business Name','','R','Email Address','','R','How selling product','','R','Where did you hear about us','','R');return document.MM_returnValue">
<div style="color:#FF0000; text-align:center;"><?php if(!empty($_GET['err_msg'])){echo $_GET['err_msg'];} ?></div>
<fieldset>
<legend>Contact form</legend>
<p class="first">
<label for开发者_如何学Go="name">First Name</label>
<input type="text" name="First Name" id="first_name" size="30" value="<?=htmlentities($profiledata['First_Name'])?>" />
</p>
<p class="first">
<label for="name">Last Name</label>
<input type="text" name="Last Name" id="last_name" size="30" value="<?=htmlentities($profiledata['Last_Name'])?>" />
</p>
<p>
<label for="name">Phone Number</label>
<input type="text" name="Phone Number" id="phone number" size="30" value="<?=$profiledata['Phone_Number']?>" />
</p>
<p>
<label for="email">Business Name</label>
<input type="text" name="Business Name" id="business name" size="30" value="<?=htmlentities($profiledata['Business_Name'])?>" />
</p>
<p>
<label for="email">Web Address</label>
<input type="text" name="Web Address" id="web address" size="30" value="<?=$profiledata['Web_Address']?>" />
</p>
<p>
<label for="email">Email</label>
<input type="text" name="Email Address" id="email address" size="30" value="<?=$profiledata['Email_Address']?>" />
</p>
<p>
<img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /><br />
<label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" />
</p>
</fieldset>
<fieldset>
<p>
<label for="message">Describe how you plan on selling this product</label>
<textarea name="How selling product" id="How selling product" cols="30" rows="4"><?=htmlentities($profiledata['How_selling_product'])?></textarea>
</p>
<p>
<label for="message">Where did you hear about us?</label>
<textarea name="Where did you hear about us" id="Where did you hear about us" cols="30" rows="4"><?=htmlentities($profiledata['Where_did_you_hear_about_us'])?></textarea>
</p>
</fieldset>
<p class="submit"><button type="submit">Send</button></p>
<input name="mailform_address" type="hidden" value="email@address.com" />
</form>
Error handling:
function valid_email($str)
{
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
function is_url($str)
{
return ( ! preg_match("/^((www)\.)?((\w+|[\d]?+)+(\.|\-)(\w+[\d]?+))+(\w)$/", $str)) ? FALSE : TRUE;
}
function valid_phone($str)
{
$Num = $str;
$Num = ereg_replace("([ ]+)","",$Num);;
$Num = eregi_replace("(\(|\)|\-|\+)","",$Num);
if(!is_numeric($Num))
{
return FALSE;
}
else
return TRUE;
}
$form_field = array(
'First_Name' => '',
'Last_Name' => '',
'Business_Name' => '',
'Phone_Number' => '',
'Web_Address' => '',
'Email_Address' => '',
'How_selling_product' => '',
'Where_did_you_hear_about_us' => '',
);
foreach($form_field as $key => $value){$profiledata[$key]=trim($_POST[$key]);}
$_SESSION['profiledata'] = $profiledata;
$emailto = NULL;
$emailmessage = "Dealer Contact Form\n";
$emailsubject = "Dealer Contact Form";
if(!empty($_POST)){
//echo "<pre>";print_r($_POST);die;
if( $_SESSION['security_code'] != $_POST['security_code'] || empty($_SESSION['security_code'] ) ) {
// Insert your code for showing an error message here
$err_msg = 'Sorry, you have provided an invalid security code';
header("Location: reseller.php?err_msg=".urlencode($err_msg));
return;
} else {
unset($_SESSION['security_code']);
}
$fname = html_entity_decode(trim($_POST['First_Name']));
$lname = html_entity_decode(trim($_POST['Last_Name']));
$company = html_entity_decode(trim($_POST['Business_Name']));
$phone = html_entity_decode(trim($_POST['Phone_Number']));
$website = html_entity_decode(trim($_POST['Web_Address']));
$email = html_entity_decode(trim($_POST['Email_Address']));
$notes = "Lead Source: ".html_entity_decode(trim($_POST['Where_did_you_hear_about_us']))."\n";
$notes .= "Selling Method: ".html_entity_decode(trim($_POST['How_selling_product']));
if(!valid_phone($phone)){
$err_msg = 'Please enter valid Phone Number.';
header("Location: reseller.php?err_msg=".urlencode($err_msg));
return;
}
if(!is_url($website)){
$err_msg = 'Please enter valid Web Address.';
header("Location: reseller.php?err_msg=".urlencode($err_msg));
return;
}
if(!valid_email($email)){
$err_msg = 'Please enter valid Email.';
header("Location: reseller.php?err_msg=".urlencode($err_msg));
return;
}
if(!stristr($website,"http://") && !stristr($website,"https://") && $website){
$website = "http://".$website;
}
$res = mysql_query("SELECT in_customer_id FROM tbl_customer WHERE st_company_name = '".addslashes($company)."'");
if(mysql_num_rows($res)){
$err_msg = 'Business Name already exists';
header("Location: reseller.php?err_msg=".urlencode($err_msg));
return;
}
$res = mysql_query("SELECT st_user_name,st_user_email_id FROM tbl_admin_user WHERE st_user_email_id='".addslashes($email)."' AND flg_is_delete=0");
if(mysql_num_rows($res)){
$err_msg = 'Email already exists';
header("Location: reseller.php?err_msg=".urlencode($err_msg));
return;
}
I'd appreciate some help or at least a nudge in the right direction. Thanks!
Depending on what kind of 'popup' you want, you will probably want to implement some client side javascript that displays a nicely formatted message that makes use of the message that you send back from the server.
I'd look at using JQuery if it was me.
Is this okay?
<html>
<head>
<script>
function Start(page) {
OpenWin = this.open(page, "CtrlWindow", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,width=600,height=200,left="
+((window.screen.width-600)/2)+",top="+((window.screen.height-200)/3)+"" );
}
</script>
</head>
<body onload="Start('child.htm')">
This is parent.
</body>
</html>
精彩评论