ngreenwood6 Posted July 31, 2008 Share Posted July 31, 2008 Im sure this is an easy answer for some of you guru's out there but I just cannot figure it out with google searches and everything else. I have a page where a user inputs data (first name, last name, etc.). When they dont put in that information my codes is: echo ("please input data"); However, this takes them to a new page that is blank and just says "please input data". I would like it to put "please input data on the same page as where they enter the dat. How can I do this? Link to comment https://forums.phpfreaks.com/topic/117572-display-errors/ Share on other sites More sharing options...
phpSensei Posted July 31, 2008 Share Posted July 31, 2008 Set your form action to nothing <form name="formName" method="POST" action="">// Your fiels go here</form> Put your PHP code on top of the form, and do a simple echo. Example <?php $form = (isset($_POST['Submit'])) ? TRUE : FALSE; $name = trim(strip_tags(htmlentities($_POST['name']))); if($form){ if($name == ''){ echo 'Please Enter Your Name'; } } ?> <form id="form1" name="form1" method="post" action=""> <label> <input type="text" name="name" value = "<?php echo $name; ?>"/> </label> <label> <input type="submit" name="Submit" value="Submit" /> </label> </form> Link to comment https://forums.phpfreaks.com/topic/117572-display-errors/#findComment-604723 Share on other sites More sharing options...
ngreenwood6 Posted July 31, 2008 Author Share Posted July 31, 2008 There has got to be an easier way or if you can show me what to add to mine or how to fix it. I have this code that includes index.php everytime and it works, but the problem is that if I have two boxes it displays the boxes twice because it calls index.php to each of them if the "if" statement is true. I am a noob so any help is greatly appreciated. This is get.php: <?php //include variables include ("variables.php"); //include the database variables include ("db.php"); $mysql_connect = mysqli_connect($host,$db_username,$db_password,$db_name) or die ("Could not connect to database"); //variable to send data to the database $result = mysqli_query($mysql_connect,$getfrom_db) or die ("Error: ".mysqli_error($cxn)); $row = mysqli_fetch_array($result); $firstname = $row['firstname']; $lastname = $row['lastname']; $id = $row['id']; if (!$get_lastname) { include ("index.php"); echo ("Please enter a valid last name!"); } elseif (strtolower($get_lastname) != strtolower($lastname)) { include ("index.php"); echo ("There are no results, please enter a different last name!"); } else { include ("index.php"); echo ("$firstname $lastname your user id is $id!"); } ?> The code for index.php: <html> <head> <title>Get the Data!</title> </head> <body> <!--This is the beginning statement--> This script will allow a user to pull up the data that they previously entered! <br><br> <!--This is the form that we will be using to enter the users data--> <form name="input" method="post" action="get.php"> Enter your last name: <input name="get_lastname" type="text" id="get_lastname"> <br> <input type="submit" name="submit" value="Submit"> </form> </body> </html> This is the code for variables.php: <?php //define the variables $get_lastname = Trim(stripslashes($_POST['get_lastname'])); ?> Link to comment https://forums.phpfreaks.com/topic/117572-display-errors/#findComment-604741 Share on other sites More sharing options...
phpSensei Posted July 31, 2008 Share Posted July 31, 2008 What I showed you was the easiest way, or you can use javascript to validate the form. Link to comment https://forums.phpfreaks.com/topic/117572-display-errors/#findComment-604786 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.