我是PHP的新手,目前正致力于开发我的第一个表单。 使用下面的代码示例,我试图实现两件事。
如果表格中填写了“姓名”或“电子邮件”的字段,则会发布“您的姓名是:”名称“和”您的电子邮件是:“电子邮件”。 如果没有填写字段,那么它将根本不显示任何内容。我认为这需要用IF语句来完成,但是如果字段为空,我很难找到显示协议的任何内容。 任何帮助将不胜感激。
<form action="Test.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> <?php $name = $_POST["name"]; if($name="name") { echo "Your name is: $_POST["name"]" ; } else { echo ""; } ?> <br> <?php $email = $_POST["email"]; if($email = "email") { echo "Your email is: $_POST["email"]" ; } else { echo ""; } ?>I am very new to PHP and currently working on developing my first form. With the below code sample I am trying to achieve two things.
If the fields for "name" or "email" are filled out in the form, then it will post "Your name is: "name" and "Your email is:"email". If a field is not filled out, then it will display nothing at all.I believe this needs to be done with an IF statement but I am having a hard time finding anything showing the protocol for if the field is empty. Any help would be greatly appreciated.
<form action="Test.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> <?php $name = $_POST["name"]; if($name="name") { echo "Your name is: $_POST["name"]" ; } else { echo ""; } ?> <br> <?php $email = $_POST["email"]; if($email = "email") { echo "Your email is: $_POST["email"]" ; } else { echo ""; } ?>
最满意答案
您的条件语句不正确。
您现在正在使用= with ($name="name")和($email = "email")进行分配,当您应该使用==检查某些内容是否“等于”时。
另外,正如Marc B stated in his comment : “你不能在"引用字符串中使用"引号" ,也不允许在"引用字符串......”中使用引用的数组键。
但是,如果你要保持这种策略; ie: if($name=="name")那么只有在表格的输入中输入了name和email ,您的表单才有效。
有几种方法可以实现这一点,使用if(!empty检查某个字段是否为空if(!empty或如果设置为if(isset 。
在这种情况下,我使用if(!empty 。意思是,如果字段不为空,那么之后回显结果。
这是一个基本的例子:(还有其他方法可以达到相同的效果)
<form action="Test.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> <?php $name = $_POST['name']; if(!empty($_POST['name'])) { echo "Your name is: " . $name; } else { echo ""; } ?> <br> <?php $email = $_POST['email']; if(!empty($_POST['email'])) { echo "Your email is: " . $email; } else { echo ""; } ?>Your conditional statements are incorrect.
You're presently making an assignment using = with ($name="name") and ($email = "email") when you should be checking if something is "equal to" using ==.
Also, as Marc B stated in his comment: "you can NOT use " quotes within a " quoted string, nor are you allowed to use quoted array keys within " quoted strings..."
However, if you were to keep that strategy; i.e.: if($name=="name") then your form would only work if the words name and email were entered in the form's inputs.
There are a few ways to achieve this, checking if a field is (not) empty using if(!empty or if it is set if(isset.
In this case, I used if(!empty. Meaning, that if the fields are not empty, then echo the results afterwards.
This is a basic example: (There are other ways to achieve the same result)
<form action="Test.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> <?php $name = $_POST['name']; if(!empty($_POST['name'])) { echo "Your name is: " . $name; } else { echo ""; } ?> <br> <?php $email = $_POST['email']; if(!empty($_POST['email'])) { echo "Your email is: " . $email; } else { echo ""; } ?>更多推荐
email,字段,php,电脑培训,计算机培训,IT培训"/> <meta name="description"
发布评论