Jump to content

Form Error Checking - Showing the error?


peter_anderson

Recommended Posts

I've decided to rewrite one a couple of my forms, to make them more user friendly.

 

But I am having problems returning the error. I've created an array ($e) which has the field name ($e['field_name']) for the error, but it isn't displaying any errors.

 

Here's the code:

			case "contact":
			$e = array();
			if(isset($_POST['sb'])){
				$fullname = $sql->real_escape_string($_POST['fullname']);
				$email_a = $sql->real_escape_string($_POST['email']);
				$email_r = $sql->real_escape_string($_POST['email_r']);
				$orderid = $sql->real_escape_string($_POST['orderid']);
				$subject = $sql->real_escape_string($_POST['subject']);
				$problem = $sql->real_escape_string($_POST['problem']);
				# Error Message
				function show_error($error,$evar){
					$err = ' <span style="color: #ff0000"><strong>'.$error.'</strong></span>';
					return $err;
				}
				# Check for isset
				if(!isset($_POST['fullname'])){
					$e['fullname'] = show_error('Please enter your full name',$e['fullname']);
				}
				if(!isset($_POST['email_a'])){
					$e['email_a'] = show_error('Please enter your email address.',$e['email_a']);
				}
				if(!isset($_POST['email_r'])){
					$e['email_r'] = show_error('Please re-enter your email address.',$e['email_r']);
				}
				if(!isset($_POST['subject'])){
					$e['subject'] = show_error('Select a subject.',$e['subject']);
				}
				if(!isset($_POST['problem'])){
					$e['problem'] = show_error('Describe your problem.',$e['problem']);
				}
				if(isset($_POST['email']) AND isset($_POST['email_r']) AND $email_a != $email_r){
					$e['email_r'] = show_error('Your email addresses do not match.',$e['email_r']);
				}
			}
			$content = '<h2>
Customer Support - Contact</h2>
<p>
For technical support, sales and product related questions: <a href="'.$tech_support.'" target="_blank">Technical Support</a>.</p>
<p>
For downloading, ordering, refund or other store related questions, please fill out the form below:</p>
<form method="post" name="csupport">
<p>
	Please enter your full name:<br />
	<input name="fullname" type="text" value="'.$fullname.'" />'.$e['fullname'].'</p>
<p>
	Please enter your email address:<br />
	<input name="email_a" type="text" value="'.$email_a.'" />'.$e['email_a'].'<br />
	Please re-enter your email address:<br />
	<input name="email_r" type="text" value="'.$email_r.'" />'.$e['email_r'].'</p>
<p>
	Please enter your order number:<br />
	<input name="orderid" type="text" value="'.$orderid.'" /></p>
<p>
	Please select a subject for your enquiry:<br />
	<select name="subject"><option selected="selected" value="">--Select--</option><option value="Billing Question">Billing Question</option><option value="Cancel / Refund Order">Cancel / Refund Order</option><option value="Download Question">Download Question</option><option value="Password Issues">Password Issues</option><option value="Order Question">Order Question</option><option value="Shipping Question">Shipping Question</option><option value="Other">Other</option></select>'.$e['subject'].'</p>
<p>
	Describe your problem as best you can:<br />
	<textarea cols="45" name="problem" rows="4">'.$problem.'</textarea>'.$e['problem'].'</p>
<p>
	<input name="sb" type="hidden" value="sb" /><input name="submit" type="submit" value="Submit" /></p>
</form>
';
			$title = 'Contact Support';
		break;

 

Can anyone see what the problem is? Thanks

Link to comment
Share on other sites

I would guess it is because your second parameter passed to show_error() doesn't exist at the time it is being passed.

 

$e['whatever'] = show_error( 'sjdnflsndlg', $e['whatever'] );

 

See what I mean? If $e['whatever'] doesn't exist until you create it, then how can the value of $e['whatever'] include $e['whatever']?

Link to comment
Share on other sites

If I change the code so that the error shows if it is filled out, it shows the error:

 

$e = array();
			if(isset($_POST['submit'])){
				$fullname = $sql->real_escape_string($_POST['fullname']);
				$email_a = $sql->real_escape_string($_POST['email_a']);
				$email_r = $sql->real_escape_string($_POST['email_r']);
				$orderid = $sql->real_escape_string($_POST['orderid']);
				$subject = $sql->real_escape_string($_POST['subject']);
				$problem = $sql->real_escape_string($_POST['problem']);
				# Error Message
				function show_error($error){
					$err = ' <span style="color: #ff0000"><strong>'.$error.'</strong></span>';
					return $err;
				}
				# Check for isset
				if(isset($_POST['fullname'])){
					$e['fullname'] = show_error('Please enter your full name');
					$continue = 0;
				}
				if(!isset($fullname)){
					$e['fullname'] = show_error('Please enter your full name');
					$continue = 0;
				}
				if(!isset($_POST['email_a'])){
					$e['email_a'] = show_error('Please enter your email address.');
					$continue = 0;
				}
				if(!isset($_POST['email_r'])){
					$e['email_r'] = show_error('Please re-enter your email address.');
					$continue = 0;
				}
				if(!isset($_POST['subject'])){
					$e['subject'] = show_error('Select a subject.');
					$continue = 0;
				}
				if(!isset($_POST['problem'])){
					$e['problem'] = show_error('Describe your problem.');
					$continue = 0;
				}
				if(isset($_POST['email']) AND isset($_POST['email_r']) AND $email_a != $email_r){
					$e['email_r'] = show_error('Your email addresses do not match.');
					$continue = 0;
				}
			}
			$content = '<h2>
Customer Support - Contact</h2>
<p>
For technical support, sales and product related questions: <a href="'.$tech_support.'" target="_blank">Technical Support</a>.</p>
<p>
For downloading, ordering, refund or other store related questions, please fill out the form below:</p>
<form method="post" name="csupport">
<p>
	Please enter your full name:<br />
	<input name="fullname" type="text" value="'.$fullname.'" />'.$e['fullname'].'</p>
<p>
	Please enter your email address:<br />
	<input name="email_a" type="text" value="'.$email_a.'" />'.$e['email_a'].'<br />
	Please re-enter your email address:<br />
	<input name="email_r" type="text" value="'.$email_r.'" />'.$e['email_r'].'</p>
<p>
	Please enter your order number:<br />
	<input name="orderid" type="text" value="'.$orderid.'" /></p>
<p>
	Please select a subject for your enquiry:<br />
	<select name="subject"><option selected="selected" value="">--Select--</option><option value="Billing Question">Billing Question</option><option value="Cancel / Refund Order">Cancel / Refund Order</option><option value="Download Question">Download Question</option><option value="Password Issues">Password Issues</option><option value="Order Question">Order Question</option><option value="Shipping Question">Shipping Question</option><option value="Other">Other</option></select>'.$e['subject'].'</p>
<p>
	Describe your problem as best you can:<br />
	<textarea cols="45" name="problem" rows="4">'.$problem.'</textarea>'.$e['problem'].'</p>
<p>
	<input name="sb" type="hidden" value="sb" /><input name="submit" type="submit" value="Submit" /></p>
</form>
';
			$title = 'Contact Support';
		break;

Link to comment
Share on other sites

<?php

case "contact":
$e = array();
if(isset($_POST['sb'])){
$fullname = $sql->real_escape_string($_POST['fullname']);
$email_a = $sql->real_escape_string($_POST['email']);
$email_r = $sql->real_escape_string($_POST['email_r']);
$orderid = $sql->real_escape_string($_POST['orderid']);
$subject = $sql->real_escape_string($_POST['subject']);
$problem = $sql->real_escape_string($_POST['problem']);
	# Error Message
function show_error($error){
	$err = ' <span style="color: #ff0000"><strong>'.$error.'</strong></span>';
		return $err;
}
				# Check for isset
if(!isset($_POST['fullname'])){
	$e['fullname'] = show_error('Please enter your full name');
				}
				if(!isset($_POST['email_a'])){
					$e['email_a'] = show_error('Please enter your email address.');
				}
				if(!isset($_POST['email_r'])){
					$e['email_r'] = show_error('Please re-enter your email address.');
				}
				if(!isset($_POST['subject'])){
					$e['subject'] = show_error('Select a subject.');
				}
				if(!isset($_POST['problem'])){
					$e['problem'] = show_error('Describe your problem.');
				}
				if(isset($_POST['email']) AND isset($_POST['email_r']) AND $email_a != $email_r){
					$e['email_r'] = show_error('Your email addresses do not match.');
				}
			}
			$content = '<h2>
Customer Support - Contact</h2>
<p>
For technical support, sales and product related questions: <a href="'.$tech_support.'" target="_blank">Technical Support</a>.</p>
<p>
For downloading, ordering, refund or other store related questions, please fill out the form below:</p>
<form method="post" name="csupport">
<p>
	Please enter your full name:<br />
	<input name="fullname" type="text" value="'.$fullname.'" />'.$e['fullname'].'</p>
<p>
	Please enter your email address:<br />
	<input name="email_a" type="text" value="'.$email_a.'" />'.$e['email_a'].'<br />
	Please re-enter your email address:<br />
	<input name="email_r" type="text" value="'.$email_r.'" />'.$e['email_r'].'</p>
<p>
	Please enter your order number:<br />
	<input name="orderid" type="text" value="'.$orderid.'" /></p>
<p>
	Please select a subject for your enquiry:<br />
	<select name="subject"><option selected="selected" value="">--Select--</option><option value="Billing Question">Billing Question</option><option value="Cancel / Refund Order">Cancel / Refund Order</option><option value="Download Question">Download Question</option><option value="Password Issues">Password Issues</option><option value="Order Question">Order Question</option><option value="Shipping Question">Shipping Question</option><option value="Other">Other</option></select>'.$e['subject'].'</p>
<p>
	Describe your problem as best you can:<br />
	<textarea cols="45" name="problem" rows="4">'.$problem.'</textarea>'.$e['problem'].'</p>
<p>
	<input name="sb" type="hidden" value="sb" /><input name="submit" type="submit" value="Submit" /></p>
</form>
';
			$title = 'Contact Support';
break;
?>

 

try that, let me know what happens.

Link to comment
Share on other sites

@radar, it prints:

 

Array
(
)

 

When no fields or some fields are filled in.

 

$e = array();
			if(isset($_POST['submit'])){
				$fullname = $sql->real_escape_string($_POST['fullname']);
				$email_a = $sql->real_escape_string($_POST['email_a']);
				$email_r = $sql->real_escape_string($_POST['email_r']);
				$orderid = $sql->real_escape_string($_POST['orderid']);
				$subject = $sql->real_escape_string($_POST['subject']);
				$problem = $sql->real_escape_string($_POST['problem']);
				# Error Message
				function show_error($error){
					$err = ' <span style="color: #ff0000"><strong>'.$error.'</strong></span>';
					return $err;
				}
				# Check for isset
				if(!isset($_POST['fullname'])){
					$e['fullname'] = show_error('Please enter your full name');
					$continue = 0;
				}
				if(!isset($fullname)){
					$e['fullname'] = show_error('Please enter your full name');
					$continue = 0;
				}
				if(!isset($_POST['email_a'])){
					$e['email_a'] = show_error('Please enter your email address.');
					$continue = 0;
				}
				if(!isset($_POST['email_r'])){
					$e['email_r'] = show_error('Please re-enter your email address.');
					$continue = 0;
				}
				if(!isset($_POST['subject'])){
					$e['subject'] = show_error('Select a subject.');
					$continue = 0;
				}
				if(!isset($_POST['problem'])){
					$e['problem'] = show_error('Describe your problem.');
					$continue = 0;
				}
				if(isset($_POST['email']) AND isset($_POST['email_r']) AND $email_a != $email_r){
					$e['email_r'] = show_error('Your email addresses do not match.');
					$continue = 0;
				}
				echo "<pre>";
					print_r($e);
				echo "</pre>";
			}
			$content = '<h2>
Customer Support - Contact</h2>
<p>
For technical support, sales and product related questions: <a href="'.$tech_support.'" target="_blank">Technical Support</a>.</p>
<p>
For downloading, ordering, refund or other store related questions, please fill out the form below:</p>
<form method="post" name="csupport">
<p>
	Please enter your full name:<br />
	<input name="fullname" type="text" value="'.$fullname.'" />'.$e['fullname'].'</p>
<p>
	Please enter your email address:<br />
	<input name="email_a" type="text" value="'.$email_a.'" />'.$e['email_a'].'<br />
	Please re-enter your email address:<br />
	<input name="email_r" type="text" value="'.$email_r.'" />'.$e['email_r'].'</p>
<p>
	Please enter your order number:<br />
	<input name="orderid" type="text" value="'.$orderid.'" /></p>
<p>
	Please select a subject for your enquiry:<br />
	<select name="subject"><option selected="selected" value="">--Select--</option><option value="Billing Question">Billing Question</option><option value="Cancel / Refund Order">Cancel / Refund Order</option><option value="Download Question">Download Question</option><option value="Password Issues">Password Issues</option><option value="Order Question">Order Question</option><option value="Shipping Question">Shipping Question</option><option value="Other">Other</option></select>'.$e['subject'].'</p>
<p>
	Describe your problem as best you can:<br />
	<textarea cols="45" name="problem" rows="4">'.$problem.'</textarea>'.$e['problem'].'</p>
<p>
	<input name="sb" type="hidden" value="sb" /><input name="submit" type="submit" value="Submit" /></p>
</form>
';
			$title = 'Contact Support';
		break;

Link to comment
Share on other sites

k now try the same thing with $_POST and make sure that is printing out correctly.

 

although it really shouldnt be inside the function..  it should be right before the ending ?> of the php file...  that way it always displays ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.