Forum Moderators: coopster
however, it checks to see if fields are empty when the button is pressed, if they are empty ive got it to bring up an error to say for example name, postcode empty.
but the form comes back with blank fields i want it so if fields are empty form comes back with the fields that have already been filled in.
how do i do this?
Thanks in advance
You just put the $_POST variables in the value attribute of the form elements like this:
<?php extract($_POST);?>
<input type="text" name="name" value="<?=$name?>" />
<input type="text" name="postcode" value="<?=$postcode?>" />
etc...
Now, depending on your PHP configuration, you may get errors by just doing that. The possible error will be "undefined index". This will happen when the form page is loaded without the $_POST present(ie. the first visit to the form).
To avoid these errors you need to check for the existence of the post vars, and if they are not there you have to create empty ones.
<?php
if(!$_POST)
{
$name = "";
$postcode = "";
etc...
} else {
extract($_POST);
}
?>
<input type="text" name="name" value="<?=$name?>" />
<input type="text" name="postcode" value="<?=$postcode?>" />
etc...
if error...
header("Location: /myform.php?name=" . $_POST["name"] . "&postcode=" . $_POST["postcode"]);
And on the form page, change all $_POST references to $_GET
So for example if i type nothing in to shopName, but type everthing else into the other fields, it brings up page saying error and $shopWebSite in each field instead of what i typed.
here is code
<html>
<body>
<?php
$showAdd=true;
if (isset($_POST['submit']))
{
if (empty($_POST['shopName']))
{
echo 'required field is empty';
if(!$_POST)
{
$shopName = "";
} else {
extract($_POST);
extract($_POST);
echo ( '<form action="shoppingDatabaseAdd.php" method="post">' );
echo ( 'Shop Name : <input type="text" name="shopName" value="$shopName" size="20" length="40"><BR>' );
echo ( 'Shop Address Line One : <input type="text" name="shopAddressLineOne" value="$shopAddressLineOne" size="20" length="40"><BR>' );
echo ( 'Shop Address Line Two : <input type="text" name="shopAddressLineTwo" value="$shopAddressLineTwo" size="20" length="40"><BR>' );
echo ( 'Shop County : <input type="text" name="shopCounty" value="$shopCounty" size="25" length="40"><BR>' );
echo ( 'Shop PostCode : <input type="text" name="shopPostCode" value="$shopPostCode" size="7" length="40"><BR>' );
echo ( 'Shop Telephone : <input type="text" name="shopTelephone" value="$Telephone" size="13" length="40"><BR>' );
echo ( 'Shop Fax : <input type="text" name="shopFax" value="$shopFax" size="13" length="40"><BR>' );
echo ( 'Shop Email : <input type="text" name="shopEmail" value="$shopEmail" size="40" length="40"><BR>' );
echo ( 'Shop WebSite : <input type="text" name="shopWebSite" value="$shopWebSite" size="100" length="40"><BR>' );
echo ( 'Shop Type : <input type="text" name="shopType" value="$shopType" size="15" length="40"><BR>' );
echo ( '<input type=button value="Back" onClick="history.go(-1)">' );
echo ( '<input type="submit" name="submit" value="Submit">' );
echo ( '<input type="reset" name="reset" value="Clear It">' );
echo ( '</form>' );
}
}
else
{
run();
}
}
function run()
{
$connection = pg_connect("host=jhjhn port= dbname=ug75ixc user=u password=p);
$query = "INSERT INTO shop (shopname, shopaddresslineone, shopaddresslinetwo, shopcounty, shoppostcode, shoptelephone, shopfax, shopemail, shopwebsite, shoptype) values ('$_POST[shopname]', '$_POST[shopAddressLineOne]', '$_POST[shopAddressLineTwo]', '$_POST[shopCounty]', '$_POST[shopPostCode]', '$_POST[shopTelephone]', '$_POST[shopFax]', '$_POST[shopEmail]', '$_POST[shopWebSite]', '$_POST[shopType]')";
$result = pg_exec($connection, $query);
$nrows = pg_numrows($result);
printf ("These values were inserted into the database - %s", $_POST[shopName]);
pg_close();
}
if($showAdd)
{
echo ( '<form action="shoppingDatabaseAdd.php" method="post">' );
echo ( 'Shop Name : <input type="text" name="shopName" size="20" length="40"><BR>' );
echo ( 'Shop Address Line One : <input type="text" name="shopAddressLineOne" size="20" length="40"><BR>' );
echo ( 'Shop Address Line Two : <input type="text" name="shopAddressLineTwo" size="20" length="40"><BR>' );
echo ( 'Shop County : <input type="text" name="shopCounty" size="25" length="40"><BR>' );
echo ( 'Shop PostCode : <input type="text" name="shopPostCode" size="7" length="40"><BR>' );
echo ( 'Shop Telephone : <input type="text" name="shopTelephone" size="13" length="40"><BR>' );
echo ( 'Shop Fax : <input type="text" name="shopFax" size="13" length="40"><BR>' );
echo ( 'Shop Email : <input type="text" name="shopEmail" size="40" length="40"><BR>' );
echo ( 'Shop WebSite : <input type="text" name="shopWebSite" size="100" length="40"><BR>' );
echo ( 'Shop Type : <input type="text" name="shopType" size="15" length="40"><BR>' );
echo ( '<input type=button value="Back" onClick="history.go(-1)">' );
echo ( '<input type="submit" name="submit" value="Submit">' );
echo ( '<input type="reset" name="reset" value="Clear It">' );
echo ( '</form>' );
}
?>
</body>
</html>
- you have extract twice, which doesn't really affect anything
- I don't see why you need to repeat the form output with the only difference being the value="$varname" it will just be blank if they are not set
- you don't need to echo everything, just drop out of php
- you have all of your single and double quotes mixed up which, I think, is causing your vars not to echo
try this
<html>
<body>
<?
if (isset($_POST['submit'])) {
if (empty($_POST['shopName'])) {
echo 'required field is empty';
if(!$_POST) {
$shopName = "";
} else {
extract($_POST);
}
} else {
run();
}
}
function run() {
$connection = pg_connect("host=jhjhn port= dbname=u user=u password=p);
$query = "INSERT INTO shop (shopname, shopaddresslineone, shopaddresslinetwo, shopcounty, shoppostcode, shoptelephone, shopfax, shopemail, shopwebsite, shoptype) values ('$_POST[shopname]', '$_POST[shopAddressLineOne]', '$_POST[shopAddressLineTwo]', '$_POST[shopCounty]', '$_POST[shopPostCode]', '$_POST[shopTelephone]', '$_POST[shopFax]', '$_POST[shopEmail]', '$_POST[shopWebSite]', '$_POST[shopType]')";
$result = pg_exec($connection, $query);
$nrows = pg_numrows($result);
printf ("These values were inserted into the database - %s", $_POST[shopName]);
pg_close();
}
?>
<form action="shoppingDatabaseAdd.php" method="post">
<br>Shop Name : <input type="text" name="shopName" value="<? echo $shopName?>" size="20" length="40">
<br>Shop Address Line One : <input type="text" name="shopAddressLineOne" value="<? echo $shopAddressLineOne?>" size="20" length="40">
<br>Shop Address Line Two : <input type="text" name="shopAddressLineTwo" value="<? echo $shopAddressLineTwov" size="20" length="40">
<br>Shop County : <input type="text" name="shopCounty" value="<? echo $shopCounty?>" size="25" length="40">
<br>Shop PostCode : <input type="text" name="shopPostCode" value="<? echo $shopPostCode?>" size="7" length="40">
<br>Shop Telephone : <input type="text" name="shopTelephone" value="<? echo $Telephone?>" size="13" length="40">
<br>Shop Fax : <input type="text" name="shopFax" value="<? echo $shopFax?>" size="13" length="40">
<br>Shop Email : <input type="text" name="shopEmail" value="<? echo $shopEmail?>" size="40" length="40">
<br>Shop WebSite : <input type="text" name="shopWebSite" value="<? echo $shopWebSite?>" size="100" length="40">
<br>Shop Type : <input type="text" name="shopType" value="<? echo $shopType?>" size="15" length="40">
<br><input type=button value="Back" onClick="history.go(-1)">
<br><input type="submit" name="submit" value="Submit">
<br><input type="reset" name="reset" value="Clear It">
</form>
</body>
</html>
One quick note though. PHP could be configured to throw errors when referencing non-existent variables in a script. You never know when your host will upgrade and inadvertantly change some settings. It happened to me :(
So, to be safe, you should use always initialize a variable(or check that it exists) that may not be present. I know the server I'm working on now would throw errors on your script when I load the form for the first time because the variables being echoed do not exist.
In your case you could just do:
<input type="text" name="shopTelephone" value="<? if(isset($Telephone))echo $Telephone;?>" size="13" length="40">