Reputation: 1
I am just trying to learn PHP and want to get the value of the textbox using $_post function, but its not working. I am using wamp 2.1 and the code is simple as below
<form method="POST" action="c:/wamp/www/test/try.php">
<input type="text" name="nco" size="1" maxlength="1" tabindex="1" value="2">
<input
tabindex="2" name="submitnoofcompanies" value="GO"
type="submit">
</form>
<?php
if (!isset($_POST['nco']))
{
$_POST['nco'] = "undefine";
}
$no=$_POST['nco'];
print($no);
However in no way I get the value of the textbox printed, it just prints undefined, please help me out.
Upvotes: 0
Views: 416
Reputation: 871
what for you are using this line $_POST['nco'] = "undefine"; } ..?
and please cross check whether you are using form method as post and make sure that your text name is nco ... or else use the below code it will work.
<?php
$no = $_POST['nco'];
echo $no;
?>
<form name='na' method='post' action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type='text' name='nco'>
</form>
thanks
Upvotes: 0
Reputation: 52372
You first assigned the word "undefine" to the variable $_POST['nco']
.
You then assigned the value of the variable $_POST['nco']
(still "undefine" as you stored there) to the variable $no
.
You then printed the value stored in the variable $no
.
It should be clear that this will always print the word "undefine".
If you want to print the value of the textbox with the name nco
, fill out the form with that textbox, and in the page that process the form,
echo $_POST['nco'];
...is all you do.
Upvotes: 1
Reputation: 2731
You need to setup a form or something similar in order to set the $_POST variables. See this short tutorial to see how it works. If you click the submit button, your $_POST variables will be set.
Upvotes: 0