[英]PHP: Undefined index in $_POST
我正在使用表格選擇。 我只想通過在同一頁面上回顯結果來檢查用戶選擇了什么,所以我保留了action =“”。 但其顯示錯誤未定義索引slct。 誰能幫幫我嗎
<form action="" method="post">
<select name="slct">
<option value="yes" selected="selected"> yes </option>
<option value="no"> no </option>
</select>
<input type="button" value="Submit" />
</form>
<?php
$tofd = $_POST["slct"];
echo $tofd;
?>
為什么顯示錯誤
Notice: Undefined index: slct in C:\wamp\www\Univ Assignment\Untitled-4.php on line 21
您應該使用按鈕類型提交NOT按鈕
<input type="submit" value="submit" />
然后像測試它一樣
echo (isset($_POST['slct']))? $_POST['slct'] : 'Variable undefined..';
使用PHP isset檢查它是否首先存在
范例:
$tofd = isset($_POST["slct"]) ? $_POST["slct"] : null ;
示例2:使用函數
function __POST($var)
{
return isset($_POST[$var]) ? $_POST[$var] : null ;
}
$tofd = __POST("slct");
如果它們在同一頁上,則$_POST
最初為空,因為用戶未發布任何內容。 所以你必須處理。
if(isset($_POST["slct"]))
$tofd = $_POST["slct"];
<?php
if (isset($_POST["slct"])){
$tofd = $_POST["slct"];
echo $tofd; }
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.