How to get the value from a text field and pass it into a variable?
I have the following form.
<form name="cookieform" id="login" method="POST">
<input type="text" NAME="username" id="username" class="text" maxlength="30" />
</form>
What I would like to do is to grab the value from the text field and place it into a PHP variable. I have the following code.
开发者_运维技巧<?php
$get_username = $_POST['username'];
print($get_username);
?>
thanks
Every time you hit the submit button, it will post the value of the input. This will set your PHP variable every time.
Update: Patrioticcow said his variable isn't returning anything. Something that should have been included in the question, but now we have that information.
echo $yourvariable;
Instead of print. This will work.
Unless you have stored your previous value in the $_SESSION
array, you will lose access to it each time the form is submitted again. Your $get_username
will be overwritten each time.
I don't quite understand what you mean by "enter another value in the text field" but with HTTP, every request is handled separate from any other request. So if you submit again, the new value will show.
If you are wandering how to pass multiple values with the same key and how to retrieve those value sin php, this is what you do:
$usernames = array($_POST['username']); $username_one = $usernames[0]; $username_two = $usernames[1]; ...
精彩评论