<?php
if (isset($_POST['submit']))
{
if (isset($_POST['a']) and $_POST["a"]!=='')
{
$misamarti = $_POST["a"];
echo $misamarti;
}
}
?>
<form method="post" action="">
<input type="text" name"a" value="<?php echo $misamarti; ?>" />
<input type="submit" name="submit" />
</form>
之后我得到
注意:未定义变量:misamarti in C: ' xampp '根模板' ' admin_panel ' php 1. 21行
因为变量$misamarti
将保持未定义,直到您的表单得到提交。所以要经常练习定义/初始化变量。
$misamarti = ''
if(isset($_POST['submit'])){
if(isset($_POST['a']) and $_POST["a"]!==''){
$misamarti = $_POST["a"];
echo $misamarti;
}
}
应该是<input type=text
中的name="a"
首先,您在输入名称处忘记了一个=。
第二,你应该把$misamarti放在双引号里,让它工作。
那么输入应该是这样的:
<input type="text" name="a" value="<?php echo "$misamarti" ?>" />
$misamarti
仅在if()
条件成功时才被定义。
当if()
条件失败时,没有定义变量。因此,当您在代码中进一步使用它时,它将抛出您报告的错误。
您可以通过在代码顶部为该变量定义一个默认值来修复此问题:
<?php
$misamarti = ''; //add this line at the top of the program.
if(isset($_POST['submit'])) {
//.... etc....
不要使用关键字and
,而是使用&&
同时,在顶部定义misamarti,如下所示
<?php
$misamarti;
if(isset($_POST['submit'])){
if(isset($_POST['a']) && $_POST["a"]!==''){
$misamarti = $_POST["a"];
echo $misamarti;
}
}
?>
在HTML中,a是未定义的,因为Name是未定义的
<input type="text" name="a" value="<?php echo $misamarti; ?>"/>
我不是PHP专家,但我怀疑$misamarti
的作用域仅在if
块内部,因此当您稍后在页面中尝试echo
时,它不存在。(至少在其他更静态类型的语言中是这样工作的。)如果是这种情况,请首先在if
块之外定义变量,并将其设置为某个默认值。像这样:
$misamarti = "";
if(isset($_POST['submit'])) {
if(isset($_POST['a']) and $_POST["a"]!=='') {
$misamarti = $_POST["a"];
echo $misamarti;
}
}
你可以试试这段代码,也许会对你有帮助。
<?php
if (isset($_POST['submit']))
{
if (isset($_POST['a']) and $_POST["a"]!=='')
{
$misamarti = $_POST["a"];
echo $misamarti;
}
}
?>
<form method="post" action="">
<input type="text" name="a" />
<input type="submit" name="submit" />
</form>
,也试试这个代码
<?php
$misamarti='';
if (isset($_POST['submit']))
{
if (isset($_POST['a']) and $_POST["a"]!=='')
{
$misamarti = $_POST["a"];
echo $misamarti;
}
}
?>
<form method="post" action="">
<input type="text" name="a" value="<?php echo $misamarti ?>" />
<input type="submit" name="submit" />
</form>
变化
<input type="text" name"a" value="<?php echo $misamarti; ?>" />
<input type="text" name="a" value="<?php echo $misamarti; ?>" />