write a code in order to create a form which submits to itself . Use a method of
ID: 3543832 • Letter: W
Question
write a 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:
If the user does not enter a required field or has not met all the other rules of the form (password at least four characters, both password fields matching) - this means that the validation has failed, so show the whole form again and remember what the user has already filled in (hint: value=""".$_POST['fieldname']."").
Tell the user if he/she has missed any field (generally this is done in order, so if the first field is username and they do not fill it in, tell them they missed that before you tell them they missed the email field).
Style the form and 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.
please hurry!!
Explanation / Answer
// FUNCTION TO REDIRECT IT ON THE SAME PAGE
<?php
function redirect_to($location = NULL)
{
if($location!=NULL)
{
header("Location:{$location}");
exit;
}
}
?>
<?php
if(isset($_REQUEST["msg"]))
{
echo $_REQUEST["msg"]; // DISPLAYING ERROR MESSAGE
}
if(isset($_POST['submit']))
{
$username=$_POST['username'];
$password=$_POST['password'];
$confirmpassword=$_POST['confirmpassword'];
// 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.