How to insert value into an HTML form using PHP? - php

I have a form in a file named signup.php like this,
<form action="includes/signup.inc.php" method="post">
<input type="text" name="uid" placeholder="Username..."><br><br>
<input type="text" name="mail" placeholder="Email..."><br><br>
<input type="password" name="pwd" placeholder="Password..."><br><br>
<input type="password" name="repeatPwd" placeholder="Repeat Password..."><br><br>
<input type="submit" name="btnSignup" value="Signup">
</form>
Then I have this if statement in the signup.inc.php file.
if (!preg_match("/^[a-zA-z0-9]*$/", $username)) {
header("Location: ../signup.php?error=invaliduid&mail=".$email);
exit();
}
That is if the user enter invalid username, then the user will be route back to the signup page, and I get an error message in the address bar, now I want to get the email from the address bar which the user entered, I make an if statement back in the signup.php file like this,
if ($_GET['error'] == "invaliduid") {
echo "Invalid username.";
$email = $_GET['mail'];
}
Now I want to fill the original form
<input type="text" name="mail" placeholder="Email...">
And put the email from $_GET['mail'] and put it in the value portion of the form, but I am stuck on how to do this.
The program should work like this, if the user entered proper mail and an invalid username then the user route back to the signup page with the message invalid username, and email field should be populated with the email which he already entered.

From what I understand, what you're looking for is simply this:
<input type="text" name="mail" placeholder="Email..." value="<?= htmlspecialchars($email) ?>">
(which is a less ugly way to write the following:)
<input type="text" name="mail" placeholder="Email..." value="<?php echo htmlspecialchars($email); ?>">

This here is working as intended:
<input type="text" name="mail" placeholder="Email..." value="<?php echo(isset($email))?$email:'';?>">

Related

checking users html form info with PHP on same page

I am trying to write a simple html form which requires the user to enter the correct email and password, using $_SERVER as the action for my form. I do not want to send the POST info to another php page, but I want to do it on the same page instead.
I have set two variables, $correct_email and $correct_password.
Here is the form;
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="eml">Email:</label>
<input type="email" name="eml" id="eml" required>
<br>
<label for="pwd">Password:</label>
<input type="password" name="pwd" id="pwd" required>
<br>
<input type="hidden" name="checkMe" value="12345">
<input type="submit" name="submit" value="Submit">
</form>
Here is what I am trying to get work in PHP
$correct_email = "(my personal email)";
$correct_password = "password"];
if ($_POST['eml']) == $correct_email && ($_POST['pwd']) == $correct_password {
echo "<h1>You are logged in</h1>";
} else {
echo "<h1>error</h1>";
}
It is not working, please help! I am also unclear on whether the PHP should come before or after the form.
Also, I would like to have the form be cleared from view, on the page, when the correct info is entered.
Thanks so much
Change the name of the submit button - never call anything "submit" in a form
Put the test at the top
You had issues with the ( and ) in the test
Also an issue with a ] after "password"
<?php
if ($_POST["subBut"] === "Submit") {
$correct_email = "(my personal email)";
$correct_password = "password";
if ($_POST['eml'] == $correct_email && $_POST['pwd'] == $correct_password) {
echo "<h1>You are logged in</h1>";
} else {
echo "<h1>error</h1>";
}
}
else {
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]); ?>">
<label for="eml">Email:</label>
<input type="email" name="eml" id="eml" required>
<br>
<label for="pwd">Password:</label>
<input type="password" name="pwd" id="pwd" required>
<br>
<input type="hidden" name="checkMe" value="12345">
<input type="submit" name="subBut" value="Submit">
</form>
<?php } ?>

Trying to check if the username is entered - PHP

-Checking if the username is entered.
-Basically I am trying to say if the user did not enter a username, echo "Please insert a username".
<form action="form_3.php" method="get">
Username: <input type="text" name="username" value="" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
$username=$_GET["username"];
if (!isset($username)) {echo "Please insert a USERNAME";}
else{echo "Hello: ".$username;}
?>
You can do this simply with HTML.
Username: <input type="text" name="username" value="" required>
You should add your php code on a different page to avoid any load errors.
You shouldn't use
isset();
but
empty();
The "empty" function is internally doing a "isset" on the value and checking that the variable is not an empty string.
http://php.net/manual/en/function.empty.php
The html5 validator can also be used to avoid your serveur to actually have to return the form saying "Please enter your username", but it can be easily disabled by a visitor.
<input type="text" name="username" value="" required>
When I load the page the first time it shows an error because the username is already empty. How can fix that error?

How to store html form data in a cookie so it's still there after the form is submitted

I have made this html form, it submits all the information to a MySQL database. Pretty simple stuff.
How do I store the username in a cookie so that once it's entered? I would like to ensure that once the user enters their username once, it pre-populates the username field in the future.
<!-- Message board submissionform -->
<form id="frmMB" name="frmMB" action="insert.php" method="post" enctype="multipart/form-data">
<label class="name"><input name="name" placeholder="Enter your name" type="text" id="name" onFocus="if(this.value=='Enter your name'){this.value=''};" onBlur="if (this.value==''){this.value='Enter your name'};" value="Enter your name" size="80" maxlength="10" ></label>
<br />
<label class="message">
<input name="post" placeholder="Enter a message" type="text" id="post" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;" value="Enter a message" size="80" maxlength="140" data-maxsize="3">
</label>
<br />
<label>
<input name="Submit" class="large button" type="submit" value="submit">
</label>
I think something like this would just do this trick.
setcookie($_POST['name'], $value, time()+3600);
if(isset($_COOKIE['name']) && !empty($_COOKIE['name']))
{
// do some stuff here because the name is set in the cookie.
}
First you need to start a session -> session_start()
Once done so, you have created a public $_SESSION array and can add elements to it. Easiest way to do so is:
<?php
if(isset($_POST['username'])){
sesion_start();
$_SESSION['username'] = $_POST['username'];
}
...
Cheers!

Passing a value IF set

I am trying to set up an application form on my site where there is a main large application form on one page (parent.php)and a smaller form on another page (child2.php) that when users fill out some of their details and submit that smaller form, they are taken to the larger form and the details they have entered already appear in the corresponding textbox on the larger form along side some extra boxes for them to fill out(if that makes sense!)
I can get it to work in that the textboxes on the 2nd page display the values of the matching textboxes on the first page, but only when a value is set. As users can either access the main application form through the smaller form OR by directly accessing it, I need to have it so that if the value is set, the set value is displayed and will also be the value entered into the database OR if the value is not pre-set from the smaller form, the user can enter in their info to the main form and this is what's sent to the DB. I think I might need to use ifisset and have tried to do so but am getting nowhere.
Apologies for messy code and the set up of the textboxes as they are just for testing this out and I am still getting to grips with all this and would be grateful if anyone could help me/let me know if I'm on the right track/totally off. Thanks in advance!
Page 1 (parent.php)
<form action="child2.php" method="post" class="validate">
<div>
<input class="tb" type="text" name="fName" placeholder="first name" id="fName" value="<?php $fName ?>" required/><br/>
<br/>
<input class="tb" type="text" name="sName" placeholder="surname" id="sName" value="<?php $sName ?>" required/><br/>
<br/>
<input class="tb" type="email" name="email" required placeholder="email address" id="email" value="<?php $email ?>" required/>
<br/>
<input class="tb" type="address" name="address" placeholder="address" value="<?php $address ?>" id="address" />
<br/>
<input id="submit" name="submit" type="submit" value="Submit">
</div>
</form>
Page 2 (child2.php)
<?php
function renderForm($fName, $sName, $email, $address){
?>
<form action="" method="post" class="validate">
<label class="label">first name</label><input class="tb" type="text" id="fName" name="fName" value="<?php if (isset($fName)) { echo $fName = $_REQUEST['fName'];} else { echo "first name"; }?>"/>
</br>
<label class="label">surname</label><input class="tb" type="text" id="sName" name="sName" value="<?php if (isset($sName)) { echo $sName = $_REQUEST['sName'];} else { echo "surname"; }?>"/>
</br>
<label class="label">email</label><input class="tb" type="email" id="email" name="email" value="<?php if (isset($email)) { echo $email = $_REQUEST['email'];} else { echo "email"; }?>"/>
</br>
<label class="label">address</label><input class="tb" type="text" id="address" name="address" value="<?php if (isset($address)) { echo $address = $_REQUEST['address'];} else { echo "address"; }?>"/>
</br>
What you're looking for are $_SESSION variables, which stay active until the user closes the browser or until the Session expires (I think the default PHP config time is 24 minutes..?). This is a lot more efficient than storing them in a database for temporal purposes.
So you can set the variables with $_SESSION['fName'] = $_POST['fName']; etc. and then call them later with echo $_SESSION['fName']; etc.
To utilize session variables you will need <?php session_start(); ?> at the beginning of your pages (before any HTML is used)
As suggested by khanahk, using SESSION variables will do your job. The problem with the solution you are thinking is that its fine only as long as you are passing values from one page to some other page, that too after so much mess.
So, you should instead, you should use something like this
session_start();
$email = $_SESSION['email'];
<label class="label">email</label><input class="tb" type="email" id="email" name="email" value="<?php echo $email; ?>"/>

Form fields stability php

How can I make fields still in the form fields after submitting the form and return with invalid input data?
<form action="" method="post">
<label for="cellPhoneNo">البريد الالكتروني</label>
<input type="text" name="emailAddress" class="textField"/>
<span>*</span>
<span><?php
if($emailValidator != CORRECT_VALUE)
echo $errorMessage[$emailValidator];?>
</span>
<br>
</form>
and here's where I check the input
$emailValidator=checkInput($_POST['emailAddress'],INVALID_EMAIL_ADDRESS,'/^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$/');
when user submit invalid email address, the validation result shows the error message to user. How can I make the invalid error email address still in the input?
add to input
<input type="text" name="emailAddress" class="textField" value="<? echo (isset($_POST) ? $_POST['emailAddress'] : "") ?>"/>
instead of "" you can insert your default value or just leave it like this :)
Try,
<input type="text" name="emailAddress" value="<?php echo $_POST['emailAddress']; ?>" class="textField"/>
Try to use javascript to validate the email before posting it to your server-side code :)
Check this link: http://www.zparacha.com/validate-email-address-using-javascript-regular-expression/

Categories