How to get directed to main page..if validation is good.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TheDumbNerd
Forum Newbie
Posts: 13
Joined: Sun Nov 13, 2011 4:01 pm

How to get directed to main page..if validation is good.

Post by TheDumbNerd »

Ok, so if the user enters an good value intothe form, how would they get directed to the main page with the value that the user enteres in the table?
Here's my first document containing the table with whatever the user hs enetered in the form thats contained in a table. I put in values already (projectmain.php):

Code: Select all

<html>
<h1> Database </h1>
</html>

 
<?php
@$fnameadd=$_POST["fnameadd"];
@$addrecord=$_POST["addrecord"];
@$editrecord=$_POST["editrecord"];
@$deleterecord=$_POST["deleterecord"];
$firstname=array("Sandy, Bhogal");
array_push($firstname, $fnameadd);
if (empty($firstname)==False)

{
echo "<table border=1>";
echo "<th>First Name</th>";
echo "<tr>";
For ($i=0; $i<count($firstname);$i++)
echo "<td>".$firstname[$i]."</td>";
echo "</tr>"; 
echo "</table>";
echo "<form name=\"myform\" value=\"\" action=\"projectform.php\" method=\"POST\"/>\n";
echo "<input type=\"submit\" name=\"Add Record\" value=\"Add\"/>\n";
echo "<input type=\"hidden\" name=\"addrecord\" value=\"<?php echo htmlentities(serialize($addrecord));?>\"> \n";
echo "</form>";
echo "<form name=\"myform\" value=\"\" action=\"projectrecord.php\" method=\"POST\" />\n";
echo "<input type=\"submit\" name=\"Edit Record\" value=\"Edit\"/>\n";
echo "<input type=\"hidden\" name=\"editrecord\" value=\"<?php echo htmlentities(serialize($editrecord));?>\"> \n";
echo "</form>";
echo "<form name=\"myform\" value=\"\" action=\"projectrecord.php\" method=\"POST\" />\n";
echo "<input type=\"submit\" name=\"Delete Record\" value=\"Delete\"/>\n";
echo "<input type=\"hidden\" name=\"deleterecord\" value=\"<?php echo htmlentities(serialize($deleterecord));?>\"> \n";
echo "</form>";
}
else
{
echo "There are no records in the database.";
echo "<form name=\"myform\" value=\"\" action=\"projectform.php\" method=\"POST\" />\n";
echo "<input type=\"submit\" name=\"Add Record\" value=\"Add Record\"/>\n";
echo "</form>";
}
?>
Heres my second document containing the form, so when the user has pressed add, they eneter what they want to add into the form on this page(projectform.php):

Code: Select all

<?php 
@$fnameadd=$_POST["fnameadd"];
@$firstname = unserialize($_POST['firstname']);
@array_push($firstname);


?>
<html>
<h1> Add Record </h1>
</html>
<form name="forms" value="" action="projectrecord.php" method="POST" />
<p> First Name: <input type="text" value="" name="fnameadd" value="<?php echo $fnameadd; ?>" /></p>
<input type="hidden" name="firstname" value="<?php echo htmlentities(serialize($fname));?>"> 
<p> <input type="submit" name="Submit" value="Submit" /> </p>
</form>
<form name="cancelform" value="" action="projectmain.php" method="POST" />
<p> <input type="submit" name="Cancel" value="Cancel" /> </p>
</form>
</html>
Heres my third document containing the report, so if the user entered a bad value into the form, its validated and an error message pops up, and if its good, I want the user to get redirected to the main page withthe values in the table in the main page. I don't know how to do this.(projectrecord.php)

Code: Select all

<?php 
@$fnameadd=$_POST["fnameadd"];
@$firstname = unserialize($_POST['firstname']);
@$addrecord=$_POST["addrecord"];
@$editrecord=$_POST["editrecord"];
@$deleterecord=$_POST["deleterecord"];

if ($editrecord==true )
{
echo "<html>" ;
echo "<h1>" ;
echo "Edit Record" ;
echo "<h1>" ;
echo "</h1>" ;	
echo "</html>" ;
echo "<form name=\"myform\" value=\"\" action=\"p04_form.php\" method=\"POST\"/>\n";
echo "<select name=\"Edit Record\" value=\"Edit Record\" />\n";
echo "<option value=\"blank\" >---Select Record---</option> \n";
For ($i=0; $i<count($firstname);$i++)
echo "<option value=\".$i.\">\".$firstname[$i].\"</option>\n";
echo "</select>" ;

echo "</form>";
}


if ( $addrecord==true and $fnameadd=="" 
	or ctype_alpha($fnameadd)== False ) 
	{echo "Please return to previous page and fix the errors." ;}

if (ctype_alpha($fnameadd)== true ) 
	
	{echo "Correct." ;}
	
if ($deleterecord==true )
{
echo "<html>" ;
echo "<h1>" ;
echo "Delete Record" ;
echo "<h1>" ;
echo "</h1>" ;	
echo "</html>" ;
echo "<form name=\"myform\" value=\"\" action=\"projectmain.php\" method=\"POST\" />\n";
echo "<select name=\"Delete Record\" value=\"Delete Record\" />\n";
echo "<option value=\"blank\" >---Select a Record---</option> \n";
For ($i=0; $i<count($firstname);$i++)
echo "<option value=\".$firstname[$i].\">\".$firstname[$i].\"</option>\n";
echo "</select>" ;
echo "<input type=\"submit\" name=\" Delete Record\" value=\"Edit\"/>\n";
}


?>
<form name="forms" value="" action="projectform.php" method="POST" />
<p> <input type="submit" name="Back" value="Back" /> </p>
</form>
Also, how would you know what button the user has pushed? Edit, delete or add?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to get directed to main page..if validation is good.

Post by social_experiment »

Re-directing is done by using the header() function

Code: Select all

<?php
 header('location: page.php');
 exit();
?>
TheDumbNerd wrote:Also, how would you know what button the user has pushed? Edit, delete or add?
You can test which button which clicked by using isset()

Code: Select all

<?php 
 if (isset($_POST['Edit'])) {
  // edit button clicked
 }
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply