嗨,我有这个并且工作正常,除了以下问题。
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1' value=".$_SESSION['txt1'].">";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
<A submit button here to send the form data>
问题是,如果我在提交表单时在John
文本框中有值,则会在文本框中保留原始用户输入的单词Jonn
。但是,如果用户键入John Carter
,则在提交表单时,它仅保留John
。换句话说,文本只能到文本之间的第一个空格。我如何保留整篇文章?
修改
<?php
if(isset($_POST['sendone']))
{
echo "Hello";
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
}
if(isset($_POST['sendtwo']))
{
if($_POST['txt1']=='')
{
echo "Hello";
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
echo "Empty";
}
else
{
$_SESSION['txt1'] = $_POST['txt1'];
echo "Hello";
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1' value=".$_SESSION['txt1'].">";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
echo "Hit success!";
}
}
?>
答案 0 :(得分:1)
value=
echo
_SESSION["txt1"]
时,您需要在<?php
if(isset($_POST['sendone']))
{
echo "Hello";
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
}
if(isset($_POST['sendtwo']))
{
if($_POST['txt1']=='')
{
echo "Hello";
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
echo "Empty";
}
else
{
$_SESSION['txt1'] = $_POST['txt1'];
echo "Hello";
echo "<form method='post' action=''>";
// check out this line here:
echo " <input type='text' name='txt1' id='txt1' value='" . htmlspecialchars($_SESSION['txt1']) . "'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
echo "Hit success!";
}
}
?>
attribue的值周围添加单引号,否则生成的HTML无效..就像这样:
echo
尽管如此,在可能的情况下使用普通HTML并且只使用{{1}}来表示您希望包含的变量更为高效(并且可读!)是值得的。
答案 1 :(得分:0)
检查此代码。
<form name="" method="post">
<input type="text" name="txt1">
<input type="submit" name="sendtwo">
<input type="submit" name="sendone">
</form>
<?php
if(isset($_POST['sendone']))
{
echo "Hello";
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
}
if(isset($_POST['sendtwo']))
{
if($_POST['txt1']=='')
{
echo "Hello";
echo "<form method='post' action=''>";
echo " <input type='text' name='txt1' id='txt1'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
echo "Empty";
}
else
{
$_SESSION['txt1'] = $_POST['txt1'];
echo "Hello";
echo "<form method='post' action=''>";
// check out this line here:
echo " <input type='text' name='txt1' id='txt1' value='" . $_SESSION['txt1'] . "'>";
echo " <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
echo "</form>";
echo "Hit success!";
}
}
?>