Jump to content

Input form checks ??


mishuk

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.