[英]index undefined php with $_POST
我試圖擺脫未定義的索引。 每次我點擊提交時都沒有勾選方框,我得到一個未定義的索引錯誤。
<html>
<head>
<title>Order</Title>
<style>
</style>
<body>
<form action = "order.php" method = "post">
Coffee:<p>
<input type = "checkbox" value = "coffee" name = "cappuccino"/>Capuccino<br>
</form>
</body>
</head>
</Html>
<?php
$capuccino = 3.75;
if(isset($_POST["submit"]))
{
if($_POST['cappuccino'] <> 'coffee')
{
$capuccino = 0;
}
}
?>
嘗試使用isset
類的
<?php
if(isset($_POST["submit"]))
{
if(isset($_POST['cappuccino']) && $_POST['cappuccino'] <> 'coffee')
{
$capuccino = 0;
}
}
?>
您也可以使用!=
而不是<>
$_POST['cappuccino'] != 'coffee'
在HTML表單中,如果不進行檢查,則不會發布值。 你應該測試它是否是第一次發布,所以你的PHP代碼將是這樣的:
<?php
if(isset($_POST["submit"]))
{
if(isset($_POST['cappuccino']) && $_POST['cappuccino'] <> 'coffee')
{
$capuccino = 0;
}
}
?>
你的條件應該是:
if(array_key_exists('cappuccino', $_POST) && isset($_POST['cappuccino']) && $_POST['cappuccino'] <> 'coffee')
<html>
<head>
<title>Order</Title>
<style>
</style>
<body>
<form action = "order.php" method = "post">
Coffee:<p>
<input type = "checkbox" value = "coffee" name = "cappuccino"/>Capuccino<br>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</head>
</Html>
<?php
$capuccino = 3.75;
if(isset($_POST["submit"]))
{
if(isset($_POST['cappuccino']) && !empty($_POST['cappuccino']) <> 'coffee')
{
$capuccino = 0;
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.