How to check if value isset then echo value property [PHP] - php

I have input with name of "username" and what I need is when I send the form to check whether the value was sent with the form I want to print the property inside input as:
<input type="text" name="username"
<?php if(isset($uname)){echo 'value="' . $uname . '"';} ?>
>
So I don't want to print value property unless there's a value already sent by POST method.
The code I have for now is:
<input type="text" name="username" id="username" class="form-control"
value="<?php if(isset($uname)){echo $uname;}?>"
placeholder="Username">
It's working fine but its printing value property even before I send the form, I have tried to modify the code, but the closing tag of input [ > ] seems to have problem with php closing tag ?>
How I can solve this issue?
Thanks a lot :)
Thanks, I solved it like this:
<?php
echo '<input type="text" name="username" id="username" class="form-control"';
echo (isset($uname)) ? 'value="'.$uname.'"' : '';
echo ' placeholder="Username">';
?>
Note:
I already had declared $uname = $_POST['username']; in another place, so that's why I have a valid variable ($uname).

You have to check for the post variables (which you don't show here)
<?php if (isset($_POST['uname']): ?>
value="<?php echo $_POST['uname'];?>"
<?php endif; ?>
Change the contente of post by what you have and it should solve your problem

<input type="text" name="username" id="username" class="form-control" value="<?php echo (isset($uname) && isset($_POST['uname'])) ? $uname : '';?>" placeholder="Username">
Don't forget to use filter_input(http://php.net/manual/en/function.filter-input.php) on your $uname variable before using.

Related

why the input tag prints user input in the 'value' atttribute

I have the following code from somewhere and I don't know the reason why there is a need to have the 'value' attribute in the input tag. I deleted it and the code worked perfectly. Is is for security reasons ? or am I missing something ?
<input type="text" name="username" placeholder="Enter Username" maxlength = "30" value= "<?php if (isset($_POST['username'])) echo $_POST['username']; ?>" required>
Thanks for your time

PHP HTML form session

<?php
session_start();
?>
<html>
<body>
<form action="process.php" method="post">
Name: <input type="text" name="username" value="<?php echo $_SESSION['u']; ?>" />
<input type="submit" value="Submit"/>
</form>
<form action="register.php" method="post">
<input type="submit" value="Register a new user" />
</form>
</body>
</html>
So guys I got this and my question is: I want to use sessions to assign the last registered user as the value of the login form. Everything I got runs ok the thing is if I log in for the first time and I have not started a session i get nothing for the U variable and i get an error message. How can I ... avoid that or something. I tried if statement but in the value section it does not accept {} brackets or something. Any suggestions?
Use isset and assign the session value to a variable instead of using it directly:
if(!isset($_SESSION['u'])){
$u = '';
} else {
$u = $_SESSION['u'];
}
<input type="text" name="username" value="<?php echo $u; ?>" />
An alternative solution - though less cleaner and maintainable - is to utilize PHP's ternary operator:
<input type="text" name="username" value="<?php echo (isset($_SESSION['u']) ? $_SESSION['u'] : ''); ?>" />
Simple explanation of the ternary operator
($statement ? [if $statement == true] : [$statement == false])
using ternary operator..
try this
Name: <input type="text" name="username" value="<?php echo ($_SESSION['u'])?$_SESSION['u']:''; ?>" />
value="<?php echo isset($_SESSION['u'])?$_SESSION['u']:''; ?>"

PHP: keeping filled form field values after form errors

I ve read a few question on the site similar to this but couldn't really take anything from them. I have a form that gets submitted, if there are errors, I notify the user of the errors above the form and display the form below that for them to correct it. This is all good and dandy except that they have to re enter all of their information again. I want the information to stay, and them to only have to fix / reenter the field that the mistake was in.
Here is my handling file:
<?php
include_once("Header.php");
if(!empty($_POST['formsubmit'])){require_once ("Form_Handle.php");}
include("Form.php");
include_once("Footer.php");
?>
is there something that i have to do when I include the form.php after I handle it?
I felt the answers provided are somewhat vague and also incorrect in the sense they tell you to set the contents of the fields inside the value attribute based on whether or not values exist for those fields.
Here's why. When you put if statement logic inside the value attribute, it either sets the value attribute to 'YOURVALUE' or '""'. <- That is the problem. The value of the field gets set to empty string when $_POST["field_name"] is empty. If you had form validation and were hoping to throw an 'empty field' error, this would pass your validation logic, which would be completely incorrect (Your form field will appear empty, but there would be an empty string inside it).
Instead, just echo the variable without any checks. If it's not set, nothing will happen. If it is, you will be able to retain the value. For example,
<input type="text" name="email" value="<?php echo $_POST["email"]; ?>" >
Here's another example of code where I forgo the whole above situation and only echo the value attribute if the variables are not empty.
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name" <?php if (!empty($_POST['name'])) {echo "value=\"" . $_POST["name"] . "\"";} ?> >
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" <?php if (!empty($_POST['email'])) {echo "value=\"" . $_POST["email"] . "\"";} ?> >
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website" <?php if (!empty($_POST['website'])) {echo "value=\"" . $_POST["website"] . "\"";} ?> >
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" <?php if (!empty($_POST['comment'])) {echo "value=\"" . $_POST["comment"] . "\"";} ?> rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female" <?php if ($_POST['gender']=="female") {echo "checked";} ?> >Female
<input type="radio" name="gender" value="male" <?php if ($_POST['gender']=="male") {echo "checked";} ?> >Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
In relation to the actual question that the user asked, you would need to add similar code into your form.php file.
You just need to write some logic in your input field value for keeping entered value. e.g
<input type="text" name="login" value="<?php if(isset($_POST['login'])){ echo $_POST['login'];}?>">
In your Form.php you should check the presence of various $_POST vars which you use and put their contents as value of input fields
In your form assign default values.
For example:
<?php
$answer = (isset($_POST['answer'])) ? $_POST['answer'] : '';
?>
And the form field:
<input type="text" name="answer" value="<?php print $answer;?>"/>
Why not store the posted data in a session variable.
Then when you redirect the user back to the form page, you can check to see if the form values are in the session variable, if so fill in the contents as the value in the input field.

PHP: keeping the username in field

How do i do if i want to keep the username in the field if the users entered incorrect password, so the person doesnt need to retype the username? Should i use sessions for this?
Just pass the value to the field:
<input name="uid" value="<?php echo (isset($_POST['uid'])) ? $_POST['uid'] : ''?>" />
Never forget to sanitize user input first! (not like in my example but it should give you the right idea).
But be careful with error messages. Don't say that the password is wrong. Say that the password or username is wrong. You don't want to let anyone know that a certain username is register in your system (at least not by trying to login).
Make sure $_POST['username'] data is not harmful first.
<input name="username" type="text" value="<?php echo $_POST['username'] ?>" />
try this one
use the session variable
$_SESSION['username'] = $_POST['username'];
<input type="text" name="username" value="<?php echo $_SESSION['username']; ?>" />
Just print it:
<input name="username" type="text" value="<?php echo htmlspecialchars($_POST['username']) ?>">
Yes if you are not posting to the same page but to a php handeling script you would need to use a session variable like $_SESSION['sticky']['username'] = $_POST['username'], then on the page that you return to
<input type="text" value="<?php if isset($_SESSION['sticky']['username']) echo $_SESSION['sticky']['username'] ?>" name="username" />

How can I set the value of a textbox through PHP?

So I have this empty textboxes in a registrationg page. The user enters some data, hits continue and then there's a confirmation page. If the data is incorrect, the user hits go back to go correct whatever was wrong. However, when he goes back, all the textboxes are empty. So the first thing that comes to my mind is to store the user data in a Session (I have a User class that holds all this data so I store the class in the session). When the user goes back I am able to retrieve the data.
I do something like this:
if($_SESSION['UserInfo'])
{
$user = $_SESSION['UserInfo'];
$firstName = $user->FirstName;
$lastName = $user->LastName;
}
How would I put these variables in a textbox?
To set the value, you can just echo out the content inside the value attribute:
<input type="text" name="firstname" value="<?php echo htmlentities($firstName); ?>" />
<input type="text" name="lastname" value="<?php echo htmlentities($lastName); ?>" />
Of course you will want to escape it but...
<input type="text" value="<?php echo $firstName ?>" />
or if the form is posted, it would be easier to do:
<input type="text" name="firstName" value="<?php echo $_POST['firstName'] ?>" />
fine... even though it was out of the scope of the question here is the escaped version:
<input type="text" name="firstName" value="<?php echo htmlentities($_POST['firstName']) ?>" />
smth like
<input type="text" value="<?php echo $first_name;?>">
Don't forget to escape with htmlentities() or smth similar. If you don't know why - google XSS.

Categories