[英]Radio Button INPUT not sending the input to the PHP code
下面給出的代碼顯示了錯誤:
<div class="field form-inline radio">
<form method="post" action="">
<div>
<label><input type="radio" name="eatable" value="fruit_in"/> Fruit</label>
</div>
<div>
<label><input type="radio" name="eatable" value="vegetable_in"/> Vegetable</label>
</div>
<div>
<label><input type="radio" name="eatable" value="bread_in"/> Bread</label>
</div>
<div>
<label><input type="radio" name="eatable" value="milk_in"/> Milk</label>
</div>
</form> <?php
$veg = $_POST['eatable']?>
任何人都可以告訴代碼中有什么問題嗎? 錯誤提示:注意:未定義索引:在第250行的C:\\ xampp \\ htdocs \\ k \\ upload.php中可食用
注意:由於$_POST
沒有索引eatable
,導致未定義索引 ,因為您的表單尚未發布。
您可以執行以下操作:
<?php if(isset($_POST['eatable'])){
$veg = $_POST['eatable'];
} ?>
修改旅游代碼,例如 ..
<form method="post" action="" name="items"><div><label><input type="radio" name="eatable" value="fruit_in" <?php if($_POST[eatable=="fruit_in"]) { ?> checked="checked" <?php } ?> /> Fruit</label>
</div>
<div>
<label><input type="radio" name="eatable" value="vegetable_in" <?php if($_POST[eatable=="vegetable_in"]) { ?> checked="checked" <?php } ?> /> Vegetable</label>
</div>
<div>
<label><input type="radio" name="eatable" value="bread_in" <?php if($_POST[eatable=="bread_in"]) { ?> checked="checked" <?php } ?> /> Bread</label>
</div>
<div>
<label><input type="radio" name="eatable" value="milk_in" <?php if($_POST[eatable=="milk_in"]) { ?> checked="checked" <?php } ?> /> Milk</label>
</div>
</form>
如果您正在尋找要以“提交”形式發送到php代碼的值,請使用
$ _POST ['eatable'];
否則您可以在點擊事件中使用“ ajax”
問題僅僅是因為打開文件時尚未定義“可食用”。 僅當您提交表單后才能定義“可食用”,因此將其更改為。
<?php
if(isset($_POST['eatable'])){
$veg = $_POST['eatable']
}
?>
同樣在表單上,您需要添加一個提交按鈕。
<form method="post" action="">
<div>
<label><input type="radio" name="eatable" value="fruit_in"/> Fruit</label>
</div>
<div>
<label><input type="radio" name="eatable" value="vegetable_in"/> Vegetable</label>
</div>
<div>
<label><input type="radio" name="eatable" value="bread_in"/> Bread</label>
</div>
<div>
<label><input type="radio" name="eatable" value="milk_in"/> Milk</label>
</div>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit']))
{
//php code goes here.
//this will only run when only submit button clicked.
$veg = $_POST['eatable'];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.