mishuk Posted February 17, 2007 Share Posted February 17, 2007 I have a problem with this script because when a user submits without filling out all the fields a message appears saying the field is missing however when you go back anything that has been inputted has gone and needs to be inputted again instead of just having to input the field that has been missed. How would i prevent this from happening. Thanks <?php /* Include Files *********************/ include("includes/db_connect.php"); /*************************************/ ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Add Student</title> </head> <body> <H1>Add Student Form</H1> <?php //Check status of submit button if($_POST['submit'] == 'Add Student') { if (!$_POST['forename'] || $_POST['forename'] == "") { $message = '<p> Please enter a forname <p>'; print $message; } else if (!$_POST['surname'] || $_POST['surname'] == "") { $message = '<p> Please enter a surname </p>'; print $message; } else { //assign fields to variables $forename =($_POST['forename']); $surname = ($_POST['surname']); // insert data into tbl_student $addstudent = "INSERT INTO tbl_student (forename, surname) VALUES ('$forename', '$surname')"; $result = mysql_query($addstudent) or die(mysql_error()); //Check that input has taken place if (mysql_affected_rows() == 1) { $message = 'Student Added'; print $message; } else { error_log(mysql_error()); $message = 'An error has occurred. Please try again!!'; print $message; } } } else { // Add Student Form ?><FORM method="post" action="student4.php"> <TABLE BORDER="0" ALIGN="center" cellspacing="5"> <TR> <TD>Forename:</TD> <TD><input type="text" size="20" name="forename"></TD> </TR> <TR> <TD>Surname:</TD> <TD><input type="text" size="20" name="surname"></TD> </TR> <TR> <TD><input name="submit" type="submit" value="Add Student"></TD> <TD><input name="reset" type="reset" value="Reset"></TD> </TR> </TABLE> </FORM> <? } ?> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/38896-input-form-checks/ Share on other sites More sharing options...
papaface Posted February 17, 2007 Share Posted February 17, 2007 You could use sessions. Link to comment https://forums.phpfreaks.com/topic/38896-input-form-checks/#findComment-187043 Share on other sites More sharing options...
mishuk Posted February 17, 2007 Author Share Posted February 17, 2007 How would that work? Link to comment https://forums.phpfreaks.com/topic/38896-input-form-checks/#findComment-187057 Share on other sites More sharing options...
mishuk Posted February 17, 2007 Author Share Posted February 17, 2007 I have just found that by removing the print $message commands and just using echo 'error message' seems to have worked Link to comment https://forums.phpfreaks.com/topic/38896-input-form-checks/#findComment-187060 Share on other sites More sharing options...
papaface Posted February 17, 2007 Share Posted February 17, 2007 You could try sessions with this code (may be an error or two since i did it quickly): <?php session_start(); /* Include Files *********************/ include("includes/db_connect.php"); /*************************************/ ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Add Student</title> </head> <body> <H1>Add Student Form</H1> <?php //Check status of submit button if($_POST['submit'] == 'Add Student') { $_SESSION['forename'] = $_POST['forename']; $_SESSION['surname'] = $_POST['surname']; if (!$_POST['forename'] || $_POST['forename'] == "") { $message = '<p> Please enter a forname <p><br><a href="javascript:history.back()">Click here to go back</a>'; print $message; } else if (!$_POST['surname'] || $_POST['surname'] == "") { $message = '<p> Please enter a surname </p><br><a href="javascript:history.back()">Click here to go back</a>'; print $message; } else { //assign fields to variables $forename =($_POST['forename']); $surname = ($_POST['surname']); // insert data into tbl_student $addstudent = "INSERT INTO tbl_student (forename, surname) VALUES ('$forename', '$surname')"; $result = mysql_query($addstudent) or die(mysql_error()); //Check that input has taken place if (mysql_affected_rows() == 1) { $message = 'Student Added'; print $message; } else { error_log(mysql_error()); $message = 'An error has occurred. Please try again!!'; print $message; } } } else { // Add Student Form ?><FORM method="post" action="student4.php"> <TABLE BORDER="0" ALIGN="center" cellspacing="5"> <TR> <TD>Forename:</TD> <TD><input type="text" size="20" name="forename" value="<?php if (isset($_SESSION['forename'])) { echo $_SESSION['forename'];} ?>"></TD> </TR> <TR> <TD>Surname:</TD> <TD><input type="text" size="20" name="surname" value="<?php if (isset($_SESSION['surname'])) { echo $_SESSION['surname'];} ?>"></TD> </TR> <TR> <TD><input name="submit" type="submit" value="Add Student"></TD> <TD><input name="reset" type="reset" value="Reset"></TD> </TR> </TABLE> </FORM> <? } ?> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/38896-input-form-checks/#findComment-187064 Share on other sites More sharing options...
mishuk Posted February 17, 2007 Author Share Posted February 17, 2007 How would you then reset the form to be blank to add another ? Link to comment https://forums.phpfreaks.com/topic/38896-input-form-checks/#findComment-187077 Share on other sites More sharing options...
redarrow Posted February 17, 2007 Share Posted February 17, 2007 <?php session_start(); echo="<a href='page.php?cmd=del'>add another one!</a>"; if($_GET['cmd']=="del"){ unset($forename); unset($surname); } ?> Link to comment https://forums.phpfreaks.com/topic/38896-input-form-checks/#findComment-187079 Share on other sites More sharing options...
mishuk Posted February 17, 2007 Author Share Posted February 17, 2007 Is there a specific place that the if statement goes in the code because it doesnt seem to work I placed the echo command in the code where it checks a row has been added and prints student added. But no matter where i place the if statement that follows it doesnt work? The variables don't seem to unset Link to comment https://forums.phpfreaks.com/topic/38896-input-form-checks/#findComment-187089 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.