ngreenwood6 Posted August 1, 2008 Share Posted August 1, 2008 I am trying to get my errors to post to the same page. there is an email, firstname, and lastname field. ex. if the user doesnt enter their email on that same page put the error. my code looks like this: if (!$get_email) { echo ("You are missing some information!"); } How can I incorporate what I am trying to do into my code? Any help is appreciated as always. Quote Link to comment Share on other sites More sharing options...
MaaSTaaR Posted August 1, 2008 Share Posted August 1, 2008 Hello .. I don't know if I understood you correctly or not, but you may want to do something like that : if (!$get_email) { die("You are missing some information!"); } Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 1, 2008 Author Share Posted August 1, 2008 That works but it didnt solve my issue. I am trying to get the error to post to the same page where you are putting in the data. (ex. if you forget to put in your email address, it displays "you are missing some information" next to the form input on the same page) When i submit the form and forget an email address now it just opens a new page and says "you are missing some information" on a new blank page. I dont want a new blank page i want it displayed on the form page where you are trying to submit it. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 1, 2008 Share Posted August 1, 2008 Well you need to handle the form on the same page that you display it then. At the top of the page do something like: <?php if (isset($_POST['submit'])) { //process form } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted August 1, 2008 Share Posted August 1, 2008 You mean you want the error messages displayed on the page your form is on? Example.... form.php <?php session_start(); ?> <form action="process.php" method="post"> <input type="text" name="name"><?php echo (isset($_SESSION['name_error'])) ? "You must supply a name" : ""; ?><br /> <input type="text" name="email"><?php echo (isset($_SESSION['email_error'])) ? "You must supply an email address" : ""; ?><br /> <input type="submit" name="submit"> </form> process.php <?php $err = false; session_start(); if (isset($_POST['submit'])) { if (empty($_POST['name'])) { $_SESSION['name_error'] = true; $err = true; } if (empty($_POST['email'])) { $_SESSION['email_error'] = true; $err = true; } if ($err) { header("location: form.php"); } else { unset($_SESSION); // process form } } ?> Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 1, 2008 Author Share Posted August 1, 2008 darkwater im not sure that i know what you mean. can you elaborate some or possibly give me an example? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 1, 2008 Share Posted August 1, 2008 Yeah, sure. You need to attempt to handle the form on the same page that it is displayed. For example: <?php if (isset($_POST['submit'])) { $last_name = $_POST['lname']; $first_name = $_POST['fname']; if (empty($last_name) || empty($first_name)) { $errors[] = "You forgot some information!"; } } ?> <h2>Just a test form!</h2> <?php if (is_array($errors)) { foreach ($errors as $error) { echo "- $error<br />"; } } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> First Name: <input type="text" name="fname" value="<?php echo htmlentities($first_name, ENT_QUOTES); ?>" /> Last Name: <input type="text" name="lname" value="<?php echo htmlentities($last_name, ENT_QUOTES); ?>" /> <input type="submit" name="submit" value="Submit Form" /> </form> Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 1, 2008 Author Share Posted August 1, 2008 I got it now. sorry it is working thanks for all of your help! Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 1, 2008 Author Share Posted August 1, 2008 Darkwater you definitely hit the nail on the head. thank you for the help! Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 1, 2008 Share Posted August 1, 2008 Any time. =) Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted August 1, 2008 Share Posted August 1, 2008 there is a common myth out there that forms have to process to a page other then themselves that is not the case Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 1, 2008 Author Share Posted August 1, 2008 I tried your code and it worked fine. I tried to change my file from 2 pages to 1 but i kept getting different errors and stuff. Could you help me try to combine it so that my errors come up correctly? The first page is the page with the form on it: <html> <head> <title>Sign up for newsletter!</title> </head> <!--beginning statement--> If you would like to sign up for the newsletter please use this form! <br><br> <form name="input" method="post" action="news.php"> Enter your email address: <input name="get_email" type="text" id="get_email"> <br> Enter your first name: <input name="get_firstname" type="text" id="get_firstname"> <br> Enter your last name: <input name="get_lastname" type="text" id="get_lastname"> <br> <input type="submit" name="submit" value="Submit"> </form> <br><br> To view a list of registered emails <a href="list.php">click here</a>. <br><br> <body> </body> </html> The second page is the page where it processes it: <?php //include our variables include ("variables.php"); include ("db.php"); //connect to database $mysqli_connect = mysqli_connect($host,$db_user,$db_pass,$db_name) or die ("Could not connect to database"); if (empty($get_email) || empty($get_firstname) || empty($get_lastname)) { include ("index.php"); echo "You are missing some information"; } else { //check for valid format of email address if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $get_email)) { echo "The email you have entered is not a valid email format."; } else { //check to see if data is in database $query_string = "SELECT email FROM users WHERE email = '$get_email' LIMIT 1"; $query = mysqli_query($mysqli_connect,$query_string)or die(mysqli_error($mysqli_connect)); $num = mysqli_num_rows($query); } //display this error if data is already in database if ($num != 0) { include ("index.php"); echo "That email is in use by another account."; } else { //variable to send the data if all data is correct $result = mysqli_query($mysqli_connect,$putinto_db) or die ("Error: ".mysqli_error($mysqli_connect)); ?> Congratulations! To view a list of registered emails <a href="list.php">click here</a>. <?php } } ?> If you need any additional info please let me know. Quote Link to comment Share on other sites More sharing options...
trq Posted August 1, 2008 Share Posted August 1, 2008 Have you even read the replies previous replies? Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 1, 2008 Author Share Posted August 1, 2008 yes, i did and like i said the form that he gave me worked when i tried using it how he coded it. however when i tried to embed it into my script it didn't work. i even tried to make my script one page but it wasn't working which is why i asked if he could help me create a new single page with this embedded into it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.