views:

238

answers:

4

I'm trying to make a div fade out with jquery after the form validates the user input after pushing submit. I'm trying to avoid the form from fading out before it validates in case the user didn't enter the correct information.

I would like to know if I can just add script tags in between my php tags, so that once the validation finishes, I just run the javascript real quick and then pick up with the rest of the php, like so:

<?php

    $name = trim($_POST['name']);
    $email = $_POST['email'];
    $comments = $_POST['comments'];

    $site_owners_email = '[email protected]'; // Replace this with your own email address
    $site_owners_name = 'Chris Seckler'; // replace with your name

    if (strlen($name) < 2) {
     $error['name'] = "Please enter your name"; 
    }

    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
     $error['email'] = "Please enter a valid email address"; 
    }

    if (strlen($comments) < 3) {
     $error['comments'] = "Please leave a comment.";
    }

    if (!$error) {

     require_once('phpMailer/class.phpmailer.php');
     $mail = new PHPMailer();

     $mail->From = $email;
     $mail->FromName = $name;
     $mail->Subject = "Website Contact Form";
     $mail->AddAddress($site_owners_email, $site_owners_name);
     $mail->AddAddress('[email protected]', 'Chris Seckler');
     $mail->Body = $comments;



     $mail->Send();
?>

<script type="text/javascript">
   $(function(){
      $('#container').fadeOut(1000);
   });
</script>



<?php 
     echo "<li class='success'> Congratulations, " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! </li>";
     echo nl2br("<b>Message Sent:</b>

      From: $name

      Email: $email

      Message: $comments

      <br/><a href='http://www.google.com'&gt;Link&lt;/a&gt;");




    } # end if no error
    else {

     $response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null;
     $response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null;
     $response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>" : null;

     echo $response;
    } # end if there was an error sending

?>
+3  A: 

Yes, but your result will not be what you intend.

PHP is all executed prior to the document being sent to the client (user). Javascript is executed after the document has been received by the client.

Less related comments: Your script is vulnerable to Cross Site Scripting (XSS) through POST. Do not use it on a real site before you address this issue.

One way you can accomplish what you may be intending to do is to have the second part of your php code render the html content within a div that is hidden <div id='content' style="display:none">...other content...</div>. Then, in javascript after the fade is complete, use javascript clear the display:none attribute from the div to make it appear.

Good luck!

Gdeglin
Thanks for the great comments! I googled Cross Site Scripting and it sounds scary. Are you saying I shouldn't use post? How do I address the XSS issue on my page?
zeckdude
Here is a complete explanation. (Anchor is to directions for prevention): http://www.cgisecurity.com/xss-faq.html#vendor. PHP has the following function for this purpose: http://us2.php.net/htmlspecialchars
Gdeglin
And an explanation of how this applies to POST: http://ha.ckers.org/blog/20060814/exploiting-cross-site-scripting-through-post/
Gdeglin
PHP doesn't all run before any output is printed; see e.g., the output buffer flush function
derobert
A: 

Why not try it? You already have the code written. From what I see in your code, you should be able to do this without a problem.

Joe Philllips
A: 

No that certainly won't work the way you want - your php script does not have that sort of intimate interaction with the browser and cannot come back and make an existing form do something else in this fashion. Once php starts producing output and sends the page header, it's a brand new web page you can't just make the old one go away.

you should probably consider looking at jquery forms plugin. you could then submit your form using ajax, and leave the active form visible. Once you've had a successful return from your ajax submit, then fade the form and move on to the next thing

Scott Evernden
I tried the Forms Plugin, but once it shows me the alert box, it keeps the form still filled with all the values. It isn't flashy at all. I would like to have the user click on submit, the from fades out, the message fades in, and after like 15 seconds, the empty form fades back in
zeckdude
A: 

I actually just tried the code that I showed you above and it works pretty well. At least it looks good. Here's it is live: Example Form

What I'm trying to do now is to get the Message that echo's in at the end to actually fade in instead of just pop in.

I think that George Deglin's answer,

have the second part of your php code render the html content within a div that is hidden.

<div id='content' style="display:none">
   ...other content...
</div>

Then, in javascript after the fade is complete, use javascript clear the display:none attribute from the div to make it appear.

would most likely work for that.

zeckdude