February 1, 201214 yr I need to show an alert box to the user in case a form gets successfully submitted. But it is not working. The alert box does'nt pop up. The form is on page contactus.php. I want the user to stay on that page only. So i have added header('Location: contactus.php'); Here is the code. $qry = "insert into contactus(name,email,subject,message) values('$name','$email','$subject','$message')"; $result=mysql_query($qry); if($result) { echo "<script type='text/javascript'>\n"; echo "alert('Your message was successfully sent.');\n"; echo "</script>"; header('Location: contactus.php'); } else { echo "<script type='text/javascript'>\n"; echo "alert('Could'nt send your message.');\n"; echo "</script>"; header('Location: contactus.php'); }
February 1, 201214 yr What do you expect to happen at if($result) ? To be literal, if result...... what? I think your trying to check if the query is a success, in which case try this instead: <?php $qry = "insert into contactus(name,email,subject,message) values('$name','$email','$subject','$message')"; $result=mysql_query($qry); // Query has failed, display message if(!$result) { ?> <script type="text/javascript"> <!-- alert('Message failed'); //--> </script> <?php // Redirect to page header('Location: contactus.php'); } else { ?> <script type="text/javascript"> <!-- alert('Message sent'); //--> </script> <?php // Query has succeeded, redirect header('Location: contactus.php'); } ?> You should also in this day and age be validating input, i.e. protecting against SQL Injection and also using mysqli* instead of mysql*. That script/your database could be compromised in seconds. Also check your original alert message for a failure and think about why that wouldn't work. I have not tested the above and it may not work, but mixing PHP (server-side) and JS (client side) is usually messy in this way. You're probably going to run into header errors as well I might add, I could solve them for you but where would be the fun in that
February 1, 201214 yr Author The code doesnt work.. If($result) comes out to true, i want it to display an alert box and just stay on the same page. It goes to contact_us_action.php which is the php handling the form. I want it to just stay on the same page.
February 1, 201214 yr Hi, I never said it would work, I'm not writing it for you! It's just for ideas as what you had was wrong also. If you want it fully diagnosed perhaps post your full code rather than just snippets. Cheers
February 1, 201214 yr Author I tried another method. It worked. $qry = "insert into contactus(name,email,subject,message) values('$name','$email','$subject','$message')"; $result=mysql_query($qry); if($result){ header('Location: contactus.php?status=sent'); }else{ header('Location: contactus.php?status=fail'); } And this code in contactus.php <?php if(!empty($_GET['status'])) { $status = $_GET['status']; if($status=="sent") { echo "<script type='text/javascript'>\n"; echo "alert('Your message was successfully sent.');\n"; echo "</script>"; } else { echo "<script type='text/javascript'>\n"; echo "alert('Could'nt send your message.');\n"; echo "</script>"; } } ?>
February 1, 201214 yr You still should be validating data especially when using a database. Look into mysqli_real_escape_string(); and seriously look into mysqli* instead of mysql*. Cheers
Create an account or sign in to comment