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.
Related
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.
I generate a form, which mostly consists of input fields that are already populated with values from the db.
So I do this currently like so:
<input id="misc" name="misc" value="<?php echo $workout['misc']; ?>" />
But when I try and do this:
<input id="misc" name="misc" value="<?php echo set_value($workout['misc']); ?>" />
along with a validation rule, the form does reload itself, the error message does display BUT the form is reset
What am I doing wrong?
As per the manual:
Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form. Example:
<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />
The above form will show "0" when loaded for the first time.
Hence in your case:
<input id="misc" name="misc" value="<?php echo set_value('misc', $workout['misc']); ?>" />
OR
<input id="misc" name="misc" value="<?php echo set_value('misc'); ?>" />
Documentation:
https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html
I have a link like this:
/index.php?textfieldvalue=test123
This link navigates to a site with a textfield. If this site is opened, the default-value should be "test123". To do this i used the PHP $_GET-parameter "textfieldvalue".
My textfield looks like this:
<input type="text" value="<?php $_GET('textfieldvalue') ?>" required />
But the textfield is still empty....
(And $_GET('target') is not empty! I printed it for testing seperate out)
Please remove '&' After '?' and in input field write:
<input type="text" value="<?php echo $_GET['textfieldvalue'] ?>" required />
How could I pass a query string value such as domain.com/register?invite=MR5OMxTyjYmTjcwNTyQjTZMyY5YY into a input box on a page?
For example say I had this: <input type="text" name="invite" value="" />
I'm using PHP
To clarify what I mean, if a person loaded that URL, then the value would be automatically filled in with the query string of invite.
Simple:
<?php $invite = (array_key_exists('invite', $_GET)) ? htmlspecialchars($_GET['invite']) : ''; ?>
<input type="text" name="invite" value="<?php echo $invite; ?>" />
Try this:
<input type="text" name="invite" value='<?php echo $_GET["invite"]; ?>' />
<input type="text" name="invite" value="<?php if(isset($_GET['register'])) echo $_GET['register']; ?>" />
This isn't secure at all, but it gives you a start.
Try fetching the invite key with $_GET['invite'] from the address bar (validate it first of course to prevent XSS attacks ;) ) and then place it in your input field within that value part as $invite for example so you end up with value="$invite"
Hope that helps!
<input type="text" name="invite" value="<?php echo $_REQUEST['invite']; ?>" />
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" />