Forum Moderators: coopster
If an error is made (i.e. userid already in use. The form remembers the text that has been input but not the radio buttons that were ticked nor the selected input from a drop down box?
Is there a way for it to save all the input fields even if the screen refreshes?
thanks
Yarni
When using a form one should not let the input being posted until it match the programmer requirements.
For instance, check for bogus email addresses, check for unauthorized signs in user name and of course check for missing field or for example if an user name has already been chosen by a previous poster etc....
It can also I presume be done in JS but I am not a JS person!
FIRST - JAVASCRIPT
******************
You should check that everything is filled correctly using Javascript (empty fields, wrong characters, etc...).
To check if username has already been used, you could use AJAX method. Simple and efficient, though a bit slow.
If everything is true, than the user can finally submit the form
SECOND - PHP
************
Even if I used Javascript to check that everything is correct, I still verify all variables using PHP after the form has been submitted AND before running any query (INSERT, UPDATE, etc.). Just to be on the safe side, especially for user having Javascript disable.
e.g. yesterday, I managed to register entering fake email address by disabling my Javascript. The webmaster forgot to write a solution for non javascript users! And I am sure there are many other websites with similar error.
I have a form that asks for (sample of some of the inputs) :
username : <varchar 20>
password : <password>
contact address : <var char ....>
property type : <drop down box>
beach : <radio buttons - y or n>
Now when a user fills in the whole form the php scripts checks various things on the form.
If the user has missed a field or tapped in an incorrect field the form refreshes and the text based input still shows BUT
the user loses the input they have chosen in the drop down box (reverts back to "select") and the radio button fileds (y/n) are both empty.
How do I keep the drop down boxes and radio fields showing what the user has already input.
Cheers
Yarni
The “varchar” indicates that you are indeed using a DB
It shows that a specific length of the input is predefined and that it will be inputted in a particular field (up to 256)
When using a backbrowser move most form retains most of the data but never a PW.
if the user is sent back to the login then it will not retain any info.
Without looking at the code we cannot help.
Do change by whatever real url etc....
Ive took out someof the requested fields as it was massive!
<?php
session_start();
include *************
switch (@$_GET['do'])
{
case "login":
$connection=mysql_connect($host,$user,$password)
or die ("Couldn't connect to server.");
$db=mysql_select_db($database,$connection)
or die ("Couldn't select a database.");
$sql="SELECT loginname FROM ********
WHERE loginname='$_POST[fusername]'";
$result=mysql_query($sql)
or die ("Couldn't execute query.");
$num=mysql_num_rows($result);
if ($num == 1) //login name was found
{
$sql="SELECT loginname FROM ***********
WHERE loginname='$_POST[fusername]'
AND password=password('$_POST[fpassword]')";
$result2 = mysql_query($sql)
or die("Couldn't execute query 2.");
$num2=mysql_num_rows($result2);
if ($num2 > 0) //password is correct
{
$_SESSION['auth']="yes";
$logname=$_POST['fusername'];
$_SESSION['logname'] = $logname;
$today=date("Y-m-d h:m:s");
$sql = "INSERT INTO **** (loginname,logintime)
VALUES ('$logname','$today')";
mysql_query($sql) or die("Can't execute query.");
header("Location: *******************");
}
else // password is not correct
{
unset($do);
$message="The login name, '$_POST[fusername]'
exists, but you have not entered the correct password!
Please try again.<br>";
include("form.php");
}
}
elseif ($num == 0) // login name not found
{
unset($do);
$message="The login name you entered does not exist!
Please try again.<br>";
include("form.php");
}
break;
case "new":
foreach($_POST as $field => $value)
{
if (ereg("(Name)",$field))
{
if (!ereg("^[A-Za-z' -]{1,50}$",$value))
{
unset($_GET['do']);
$message_new="$field is not a valid name.
Please try again.";
include("form.php");
exit();
}
}
$$field=strip_tags(trim($value));
} //end foreach
if (!ereg("^[A-Za-z' -]{1,50}$",$first_name))
{
unset($_GET['do']);
$message_new="$first_name is not a valid first name.
Please try again.";
include("form.php");
exit();
}
if (!ereg("^[A-Za-z' -]{1,50}$",$last_name))
{
unset($_GET['do']);
$message_new="$last_name is not a valid last name.
Please try again.";
include("form.php");
exit();
}
..........................................................
$connection=mysql_connect($host,$user,$password)
or die ("couldn't connect to server.");
$db=mysql_select_db($database,$connection)
or die ("Couldn't select database.");
$sql="SELECT loginname from *********
WHERE loginname='$newname'";
$result=mysql_query($sql)
or die("Couldn't execute query.");
$num=mysql_numrows($result);
if ($num > 0)
{
unset($_GET['do']);
$message_new="$newname already used. Select another member ID.";
include("form.php");
exit();
}
else
{
$today = date("y-m-d");
$rent="rent";
$sql="INSERT INTO ********* (advertise,loginname,createdate,password,first_name,last_name ............) VALUES
('$rent','$newname','$today',password('$newpass'),'$first_name','$last_name' ..............)";
mysql_query($sql);
$_SESSION['auth']="yes";
$_SESSION['logname'] = $newname;
/* send email to new member */
$homeemail="************************";
$query="SELECT * from ********** WHERE loginname='$newname'";
$result=mysql_query($query)
or die("Couldn't execute query.");
$row=mysql_fetch_array($result,MYSQL_ASSOC);
extract($row);
header("Location: new.php");
}
break;
default:
include("form.php");
}
?>
Another way around: split the script in two parts
A)Only PW and Username, which will make later easier the retrieving or resetting of PW
B)The rest of info
But if you did not participate in the making of your script you might face a tough task.