当表单在PHP中发布时,在文本字段中获取提交的值

时间:2011-08-17 09:58:19

标签: php forms textbox

在现有文本字段中发布表单时,我需要提交按钮的值。以下是代码。

我尝试了,但它正在创建一个新的文本字段。

<?php
$var1 = $_POST['hello'];
//below here i am trying out something but not getting in exiting textfield.
//i need the value of submit button ie some value in exiting textfield ie text1
echo "<input type='text' name='text1' value='$var1'>";

?>

<form method="post">
<input type="submit" name="hello" value="some value">
<input type="text" name="text1" value="dskfjls">
</form>

1 个答案:

答案 0 :(得分:0)

它会创建一个新的文本字段,因为这就是你echo正在做的事情。

只需改变它:

<?php
$submitbutton = $_POST['hello'];
$textbox = $_POST['text1'];
//below here i am trying out something but not getting in exiting textfield.
//i need the value of submit button ie some value in exiting textfield ie text1
echo $submitbutton;
echo $textbox;

?>

<form method="post">
<input type="submit" name="hello" value="some value">
<input type="text" name="text1" value="dskfjls">
</form>

编辑回复评论

使用此:

<form method="post">
<input type="submit" name="hello" value="some value">
<input type="text" name="text1" value="<?php (isset($_POST['hello'])?$_POST['hello']:null); ?>">
</form>