How to submit a form with php without removing the previous data

I am creating an add student page in my website
i am using this code to do it

[php]<?php
ob_start();
include(‘connectdb.php’);
?>

Personal Information
*Student Number : Gender : Male Female
*Password : Religion :
*Last Name : Civil Status : Single Married Widowed Divorced
*First Name : Nationality :
<?php if(isset($_POST['submit'])) { $stud_no=$_POST['stud_no']; $password=$_POST['password']; $lname=$_POST['lname']; $fname=$_POST['fname']; $gender=$_POST['gender']; $religion=$_POST['religion']; $civilstatus=$_POST['civilstatus']; $nationality=$_POST['nationality']; if($stud_no =='' || $password== ' ' || $lname== ' ' || $fname== ' ') { echo "Please enter required data";} else if(mysql_num_rows($query) > 0) { echo "Student Number is already in use."; } else { $query = "insert into students (stud_no,password,lname,fname,gender,religion,civilstatus,nationality) values ('$stud_no','$password','$lname','$fname','$gender','$religion','$civilstatus','$nationality')"; mysql_query($query) or die (mysql_error()); echo "Student successfully added!"; header("location:addstudents.php"); }} ?>

[/php]

How can i make it so that if there are any errors detected (example:there is no value written for the student number box) the previously typed data in the form wont get deleted?

Hi,

To pre fill forms you would use the value=“somevalue” in the input tag ie

<input type="text" name="stud_no" id="stud_no" value="<? echo $stud_no; ?>">

However, you would need to process the POST data before outputting the form. With the gender, you would need a simple if statement to determine which should be pre selected.

Hi
what i want to do is that

when i click the submit button the page must not reload if it finds any error in the fill up form


In the code that i posted above once you click the submit button the page reloads and removes all the previous data the person typed so they have to retype everything again and press submit until it passes the checks and enter the database

What i want to happen is that once you press submit if it finds any errors like there are no values written in the name box or student number box it will just say “Please input the necessary fields” without refreshing the page and removing the previous information that the user just typed.

If you don’t want the page to reload, you’ll need javascript to validate the form before posting. Theres plenty of free javascript validation scripts on the net.

If you want a php based solution, you will need to write your page to first check POST for submit, if it finds it validate the data and if invalid, output the form with the post variables. If the data is valid, do whatever. And finally if there is no POST data display a blank form.

[php]<?php
include(‘connectdb.php’);
if(isset($_POST[‘submit’])) {
$stud_no=$_POST[‘stud_no’];
$password=$_POST[‘password’];
$lname=$_POST[‘lname’];
$fname=$_POST[‘fname’];
$gender=$_POST[‘gender’];
$religion=$_POST[‘religion’];
$civilstatus=$_POST[‘civilstatus’];
$nationality=$_POST[‘nationality’];

	if($stud_no =='' || $password== ' ' || $lname== ' ' || $fname== ' ') {
		echo "Please enter required data";}

?>

Personal Information
*Student Number : Gender : Male Female
*Password : Religion :
*Last Name : Civil Status : >Single Married Widowed Divorced
*First Name : Nationality :
<? else if(mysql_num_rows($query) > 0) { echo "Student Number is already in use."; } else {
	$query = "insert into students (stud_no,password,lname,fname,gender,religion,civilstatus,nationality) values ('$stud_no','$password','$lname','$fname','$gender','$religion','$civilstatus','$nationality')";
	mysql_query($query) or die (mysql_error());
		
	echo "Student successfully added!";

header(“location:addstudents.php”);
}} else {
?>

Personal Information
*Student Number : Gender : Male Female
*Password : Religion :
*Last Name : Civil Status : Single Married Widowed Divorced
*First Name : Nationality :
<? } ?> [/php]

This is just a quick response, with possible errors in it, but you should be able to get the idea. I am after all eating my lunch as I type.

Sponsor our Newsletter | Privacy Policy | Terms of Service