That's the best title I could come up with...
Here's the problem: I'm working on a php contact form for my website. Once the user clicks send, I want to display the text "Sending Message..." with the dots on the end animated. (First no dots, then one, then two, then three, then back to none, etc). Then once the email sends, I want the message to change to "Message Sent."
The following Javascript does that:
Calling the function startAn() starts the text animation. Calling the function stopAn() stops it. This works in every web browser I've tested.Code:var msg = "Sending Message..."; var L = msg.length - 3; var i=0; var msgSent = false; var msg_h; var newMsg; var posSet = false; // sets the position of the text so that it appears centered. function setPos() { posSet = true; var w = msg_h.offsetWidth; var l = Math.round((600 - w)/2); document.getElementById("msg_wrapper").style.left = l + "px"; } // recursive function to animate text. function animateMsg() { if (!msgSent) { newMsg = msg.substring(0, L + i); } else { newMsg = "Message Sent!"; i = -1; } if (document.body.firstChild.nodeValue) { msg_h.firstChild.nodeValue = newMsg; if (!posSet) setPos(); } else if (document.body.innerHTML) { msg_h.innerHTML = newMsg; if (!posSet) setPos(); } else if (msgSent) { document.write("<div style='font-weight: bold;'>Message Sent!</div>"); i = -1; } if (i != -1) { i = (i + 1) % 4; setTimeout("animateMsg()", 333); } else { setPos(); } } // start the animation function startAn() { msg_h = document.getElementById("msg_wrapper"); animateMsg(); } // stop the animation and display "Message Sent." function stopAn() { msgSent = true; }
Now the problem:
The stopAn() function is called by the body's onload event. The startAn() function is called just before the php code that sends the message is executed. Here's that section of the php code:
The problem is that in Opera, while the server is sending the message, the animation doesn't work. While the message is sending, the text "Sending" is displayed. Then once the message is sent the animation works for a split second before the stopAn() function is called.Code:<?php $showForm = true; $msg = ""; $sub = ""; $email = ""; if (isset($_POST['send'])) { $msg = $_REQUEST['message']; $sub = $_REQUEST['subject']; $email = $_REQUEST['email']; echo ' <div id="msg_wrapper"> </div> <script type="text/javascript">startAn();</script> '; $msg = stripslashes($msg); $msg .= "\r\n\r\nMessage sent via contact form."; $msg .= "\r\nIP address: $REMOTE_ADDR"; mail( "blake@blake-foster.com", $sub, $msg, "From: $email" ); } else { echo' <form name="contact" action="' . $_SERVER['PHP_SELF'] . '" method="post" onsubmit="return validate(this)"> Your Email Address:<br> <input name="email" size="30" type="text" value=""><br> Message Subject:<br> <input name="subject" size="30" type="text"><br> Message:<br> <textarea name="message" rows="15" cols="40"> </textarea><br> <input value="Send!" type="submit" name="send"> </form> '; } ?>
On top of that, if I call setTimeout('stopAn()', 3000) instead of stopAn() as the onload event, the animation will start working once the email is sent and continue to work for 3 seconds (until stopAn() is called).
I only have this problem in Opera. It works perfectly in every other web browser.
Any ideas?
Here's the url of the page:
http://www.blake-foster.com/contact.php



Reply With Quote






Bookmarks