开发者

How would I go about making this error message appear when form field is not right?

I have an if statement and I already have it working so if certain fields are not filled in it will not send. I then have an else, and I put it like so:

if(isset($_POST['submit'])) {
    if (!empty($name) && (!empty($email) || !empty($phone))) {
    mail( "EMAIL@hotmail.com", "Monthly Specials Email",
    "Name: $name 
     Email: $email 
     Phone Number: $phone
     Comment: $comment", "From: $email" );

    $error = "";

   } else {
    $error = "Please fill in the required fields.";
   }
}

In the form, I have a span开发者_StackOverflow社区 class like so:

<span class="error">'.$error.'</span>

I have it so the action of the form is set to blank so it will stay on the same page when sent, and all of the functions are in the same page as the form. How would I go about updating the error span?

Thanks so much for any help or tips!


In order to process the form while staying on the page, you will need to incorporate some AJAX. The easiest way to do this is to use a framework of some sort (recommend jQuery). This should give you some insight into how to develop such functionality. If you get stuck, we're here to help. http://api.jquery.com/jQuery.post/


Following your current model, I am assuming you do not mean AJAX and that you merely mean the server side code and form cohabitate on the same script. You can set the action of the form to $_SERVER['PHP_SELF'] first to ensure the proper action attribute is set.

Are you echoing out the error message within the span, or is all that output being placed after an echo statement?

echo '<span class="error">'.$error.'</span>'

Or, if not in the PHP context outside of script

<span class="error"><? echo $error; ?></span>

Also, you may want to consider using a mature php mailing solution like PHP Mailer to help set headers and ensure more effective delivery.


You don't need any AJAX.

$error = '';
if (isset($_POST['submit'])) {
    if ( <<< insert check for required fields >>> ) {
        // handle form, send mail, etc
        // you may want to redirect on success to prevent double posting
    } else {
        $error = "Please fill in the required fields.";
    }
}


Well without the rest of the page I'm not sure why this isn't working already but you should post back to the same page not just an empty action. I would do it this way.

<?php 
$error = $name = $email = $phone = $comment = "";
if(isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $comment = $_POST['comment'];
    if (!empty($name) && (!empty($email) || !empty($phone))) {
    mail( "EMAIL@hotmail.com", "Monthly Specials Email",
    "Name: $name 
    Email: $email 
    Phone Number: $phone
    Comment: $comment", "From: $email" );
    } else {
        $error = "Please fill in the required fields.";
    }
}else{ ?>

<div id="specialsForm"><h3>Interested in this coupon? Email us! </h3>           
<form method="post" action="emailMonthlySpecials.php">    
    <span class="error><?php echo $error; ?></span>
    Name: <input name="name" type="text" value="<?php echo $name;?>"/><br />
    Email: <input name="email" type="text" value="<?php echo $email;?>"/><br />
    Phone Number: <input name="phone" type="text" <?php echo $phone;?>"/><br /><br /> 
    Comment: <br/>
    <textarea name="comment" rows="5" cols="30"><?php echo $comment;?></textarea><br /><br />
    <input type="submit" value="Submit Email"/>
</form></div>

<?php } ?>


When I handle form validations, I tend to create an array to hold the error messages, like so:

<?php

$error = array();
if( $POST ){
 # Form is Submitted
  if( /* TRUE if "email" is empty */ ){
    $error['email'] = 'Please provide an Email Address';
  }elseif( /* TRUE if "email" is not a valid address */ ){
    $error['email'] = 'Please provide a Valid Email Address';
  }elseif( /* TRUE if "email" is already associated with a User */ ){
    $error['email'] = 'The Provided Email Address is Already Taken';
  }

...

  if( count( $error )==0 ){
   # No Error has been Recorded
   # Do Successful Things
  }
} /* Closing if( $_POST ){ */

Then within the presentation/display section, I have something like:

<?php if( count( $error )>0 ){ ?>
<div id="error">
  The following Errors have occurred:
  <ul>
<?php foreach( $error as $k => $v ){ ?>
    <li><?php echo $k; ?>: <?php echo $v; ?></li>
<?php } ?>
  </ul>
</div>
<?php } ?>

And within the form, something like:

<input name="email"<?php echo ( $error['email'] ? ' class="error"' : '' ); ?> />

This means that:

  • Customised, multi-tiered error messages can be recorded.
  • A summary of the error messages can be shown.
  • Fields associated with the error messages can be marked.

Has worked well in my experience thusfar.


Yep, I think You have two methods to do that, as already explained above...

  1. When the form is submitted to the same page (itself) using *$_SERVER['PHP_SELF']*, you can check weather each posted field is empty using empty() function. Then if they are not filled then set the variable $error and then use echo $error; at the span of error... If no any error you can assign the default message at the $error instead of the error... It should do what you need...

  2. You can use AJAX and send a request to the page and set the error message. Then the page is not fully refreshed as it was before, but only the element you wanted to refresh. This is fast, but in most of the cases, first method is preferred, unless AJAX is a need..

What exactly you want to do? If you specify what's your actual need, it is possible to provide some sample code... (First method is already discussed)

Thank You. ADynaMic


My suggest is to use ajax call when submit,

according to the answer come back, you update the span of error.

you can find a lot of examples in web like

http://jqueryfordesigners.com/using-ajax-to-validate-forms/

http://www.the-art-of-web.com/javascript/ajax-validate/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜