write a php code in order to create a form which submits to itself . Use a metho
ID: 3545332 • Letter: W
Question
write a php code in order to create a form which submits to itself . Use a method of POST. Make the required fields the same as on the example page:
Required:
Name
Username
Password & confirm Password (must match)
If the user does not enter a required field - this means that the validation has failed, so show the whole form again and remember what the user has already filled in
Tell the user if he/she has missed any field
Style the form in a proper way and also include comments, the results as if the form is not just an assignment, but one that you are actually placing on the web.
The error message should be shown on the same page on which the form is present.
use a function to redirect it on the same page displaying error message.
Explanation / Answer
// FUNCTION TO REDIRECT IT ON THE SAME PAGE
<?php
function redirect_to($location = NULL)
{
if($location!=NULL)
{
header("Location:{$location}");
exit; // will exit out from function
}
}
?>
<?php
if(isset($_REQUEST["msg"]))
{
echo $_REQUEST["msg"]; // DISPLAYING ERROR MESSAGE
}
if(isset($_POST['submit'])) // if submit button is pressed
{
$username=$_POST['username']; // storing in another variable
$password=$_POST['password']; // storing in another variable
$confirmpassword=$_POST['confirmpassword']; // checking if both the paaswords match or not
// if($username==""){echo "Please Enter username";}
$email=$_POST['email'];
if($username!=""&& $password!=""&&$confirmpassword!=""&&$email!="")
{
if( $password==$confirmpassword){
$query=("INSERT INTO fields VALUES(NULL,'".$username."','".$password."','".$email."')");
if(mysql_query($query))
echo ("<strong>'".$username."'</strong>congrats you have been signed ");
}
else
{
redirect_to("index.php? msg= <font style = color:red> <b>******passwords didnot match please re-enter </b></text>" );
}
}
else
{
redirect_to("index.php? msg= <font style = color:red><b>****please fill in the necessary details****</b> " );
}
}
else {
?>
<html>
function
<form action="index.php" method="POST"><table
<tr><th>USERNAME <br/><input type="text"name="username"style="text-align:center;" placeholder="enter name"></th></tr>
<br/>
<tr> <th> PASSWORD <br/><input type="password"name="password"placeholder="enter password"><br/></th></tr>
<tr><th> CONFIRM-PASS <br/> <input type="password"name="confirmpassword"placeholder="enter again"><br/></tr></th>
<tr><th> E-MAIL <br/><input type="email"name="email"placeholder="enter email"><br/><br/></th></tr>
<tr> <th> <input type="submit"name="submit"value="sign in"></th></tr>
</table> </form>
<strong>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.