...

View Full Version : How to reload a form after submission



viddz
04-19-2012, 07:39 PM
Iam doing a simple high school project. Finally i understood the importance of reload the after form submission. I have tried several ways but failed. Can u guys pls help me here. This is my adduser form.




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add user</title>



<script type="text/javascript">

function validateForm(){
with (document.adduserForm)

{
if(login.value == "") {
alert("Error: login cannot be blank!");
login.focus();
return false;
}
if(login.value.length < 4) {
alert("Error:Login must contain at least four characters!");
login.focus();
return false;

}

re = /^w+$/;
if(!re.test(login.value)) {
alert("Error: login must contain only letters, numbers and underscores!");
login.focus();
return false;
}

if(password.value == "" ) {
alert("Error: Password field cannot be blank !");
password.focus();
return false;
}
if(password.value.length < 4) {
alert("Error: Password must contain at least four characters!");
password.focus();
return false;
}

if(!re.test(password.value)) {
alert("Error: Password must contain only letters, numbers and underscores!");
password.focus();
return false;

}



else

// alert("You have added a new user : " + login.value);
return true;

}}
</script>

<link href="table.css" rel="stylesheet" type="text/css" media="screen" />

</head>

<body>


<form ACTION="add_user_save.php" name="adduserForm" id="adduserForm" METHOD="POST" align="center" onsubmit="return validateForm()" >

<div align="center"><center>
<table width="436" id="table">
<tr>
<td width="214">username</td>
<td width="254"><input name="login" maxlength="10" id="login"/></td>
</tr>
<tr>
<td>password</td>
<td><input type="password" id="password" name="password" maxlength="10"/></td>
</tr>
<tr>
<td>Select user type :</td>
<td><select name="type" id="type">
<option value="admin"> Admin </option>
<option value="labassistant"> Lab Assistant </option>
<option value="storekeeper"> Store Keeper </option>
</select></td>
</tr>

<tr>
<td></td>
<td><input type="submit" name="button" id="button" value="save"/></td>
</tr>
</table>
</center></div>

</form>
</body>
</html>

viddz
04-19-2012, 07:42 PM
this is my addusersave.php file




<?php
session_start();


if(!isset($_SESSION['SESS_LOGIN']) || $_SESSION['SESS_TYPE'] !='admin')// if session variable "login" does not exist.
{
echo '<script language="javascript">';
echo 'alert("Please login as ADMINISTRATOR to add a user");';
echo ' window.location.replace("index.html");';
echo '</script>';

//header("location:login-form.php"); // Re-direct to login-form.php

}
else
{
include("config.php");

$login = mysql_real_escape_string($_POST['login']);
$password = mysql_real_escape_string($_POST['password']);
$type = mysql_real_escape_string($_POST['type']);


$checkformembers = mysql_query("SELECT * FROM members WHERE login='$login'");
if(mysql_num_rows($checkformembers) != 0)
{

echo '<script language="javascript">';
echo 'alert("Username already in use. Please try again.!" );';
echo ' window.location.replace("adduser.php");';
echo '</script>';
}
else
{

$qry_add = " INSERT INTO members
(login, password,type )
VALUES ('$login', '$password', '$type') ";

$count = mysql_query("SELECT COUNT(login) FROM members WHERE login='$login'");
if($count==1)
{
echo "<font color=red> Duplicate Entry. Please Verify login</font>";
}
else
{

if($result=mysql_query($qry_add))
{
// echo '<script language="javascript">';
//echo 'alert("you have successfully added one user !" );';
// echo 'window.setTimeout("window.location.replace('adduser.php');",20);';
// echo 'window.setTimeout("window.location.replace('adduser.php'),20");';

echo "<br><font color=green size=+1 >you have successfully added one user ! <br>[ username = $login ] </font>" ;
//echo ' window.location.reload("adduser.php");';
// echo '/script>';


}


else
{
echo "<br><font color=red size=+1 >Problem in Adding !</font>" ;
echo "ERROR - unable to save new username and password!<br>";
$SQLError = "SQL ERROR: ".mysql_errno().". ".mysql_error()."<BR><BR>";
echo "$SQLError";
mysql_close();

}

}
}

}


?>

angst
04-19-2012, 07:46 PM
HEADER("location: SomePage.php");

viddz
04-19-2012, 07:51 PM
@angst

where shud i put this ? when i put header there is a cannot modify error. :confused::confused:

stevehungry
04-19-2012, 08:05 PM
wouldn't you add

HEADER("location: differentPage.php");
at the end of your "add_user_save.php" script? after its finished adding your users to your db?

viddz
04-19-2012, 08:28 PM
@stevehungry

I did it. but error occured.

error--->> Warning: Cannot modify header information - headers already sent by (output started at F:\xampplite\htdocs\metal-feel\add_user_save.php:45) in F:\xampplite\htdocs\metal-feel\add_user_save.php on line 74

this is how i hv done it.



<?php
session_start();


if(!isset($_SESSION['SESS_LOGIN']) || $_SESSION['SESS_TYPE'] !='admin')// if session variable "login" does not exist.
{
echo '<script language="javascript">';
echo 'alert("Please login as ADMINISTRATOR to add a user");';
echo ' window.location.replace("index.html");';
echo '</script>';

//header("location:login-form.php"); // Re-direct to login-form.php

}
else
{
include("config.php");

$login = mysql_real_escape_string($_POST['login']);
$password = mysql_real_escape_string($_POST['password']);
$type = mysql_real_escape_string($_POST['type']);


$checkformembers = mysql_query("SELECT * FROM members WHERE login='$login'");
if(mysql_num_rows($checkformembers) != 0)
{

echo '<script language="javascript">';
echo 'alert("Username already in use. Please try again.!" );';
echo ' window.location.replace("adduser.php");';
echo '</script>';
}
else
{

$qry_add = " INSERT INTO members
(login, password,type )
VALUES ('$login', '$password', '$type') ";

$count = mysql_query("SELECT COUNT(login) FROM members WHERE login='$login'");
if($count==1)
{
echo "<font color=red> Duplicate Entry. Please Verify login</font>";
}
else
{

if($result=mysql_query($qry_add))
{
// echo '<script language="javascript">';
//echo 'alert("you have successfully added one user !" );';
// echo 'window.setTimeout("window.location.replace('adduser.php');",20);';
// echo 'window.setTimeout("window.location.replace('adduser.php'),20");';

echo "<br><font color=green size=+1 >you have successfully added one user ! <br>[ username = $login ] </font>" ;
//echo ' window.location.reload("adduser.php");';
// echo '/script>';

//header("Location: view_all_user.php");
}


else
{
echo "<br><font color=red size=+1 >Problem in Adding !</font>" ;
echo "ERROR - unable to save new username and password!<br>";
$SQLError = "SQL ERROR: ".mysql_errno().". ".mysql_error()."<BR><BR>";
echo "$SQLError";
mysql_close();
// echo " <a href= " adduser.php " > click here to add new user </a>" ;
}
//include("adduser.php");
header("location:adduser.php"); // this is line no 74
//header("Location: view_all_user.php");
//echo " <a href= ' adduser.php ' > click here to add new user </a>" ;

}
}

}


?>

tangoforce
04-19-2012, 08:40 PM
Any use of header() MUST be before ANY output to the browser. That means html output.

viddz
04-19-2012, 09:00 PM
Any use of header() MUST be before ANY output to the browser. That means html output.

my problem isnt with headers. I just wanna reload my form after submission.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum