多个选择/输入框以在PHP中POST

时间:2015-08-09 04:58:09

标签: php html forms post

我试图做一个有两个textareas的简单表单,以及一个表单中的两个drop-down。我试图保存POST的值,但它不起作用,我一直在:

Notice: Undefined index: team2
Notice: Undefined index: players2

我的POST页面是:

$team1 = $_POST["team1"];
$team2 = $_POST["team2"];
$str1 = $_POST["players1"];  
$str2 = $_POST["players2"];

我的表格就是这样:

<form action="cap_update.php" method="post">
To:
<select name="team1">
<option value="Anaheim">Anaheim</option>
<option value="Arizona">Arizona</option>
<option value="Boston">Boston</option>
</select><br><br>
<textarea rows="5" cols="80" name="players1"></textarea>
<br><br>
To:
<select name="team2">
<option value="Anaheim">Anaheim</option>
<option value="Arizona">Arizona</option>
<option value="Boston">Boston</option>
</select><br><br>
<textarea rows="5" cols="80" name="players2"></textarea>
<br><br>
<input type="submit" value="Submit trade!">
</form>

2 个答案:

答案 0 :(得分:2)

<?php
if(isset($team1))
{
$team1 = $_POST["team1"];
$team2 = $_POST["team2"];
$str1 = $_POST["players1"];  
$str2 = $_POST["players2"];
}
print_r($_POST);

?>
<form action="index.php" method="post">
To:
<select name="team1">
<option value="Anaheim">Anaheim</option>
<option value="Arizona">Arizona</option>
<option value="Boston">Boston</option>
</select><br><br>
<textarea rows="5" cols="80" name="players1"></textarea>
<br><br>
To:
<select name="team2">
<option value="Anaheim">Anaheim</option>
<option value="Arizona">Arizona</option>
<option value="Boston">Boston</option>
</select><br><br>
<textarea rows="5" cols="80" name="players2"></textarea>
<br><br>
<input type="submit" value="Submit trade!">
</form>



use isset to check

otherwise you can use 
error_reporting(E_ERROR | E_PARSE);
E_ERROR


    <?php
error_reporting(E_ERROR | E_PARSE);
 $team1 = $_POST["team1"];
 $team2 = $_POST["team2"];
 $str1 = $_POST["players1"];  
 $str2 = $_POST["players2"];
?>
<form action="index.php" method="post">
To:
<select name="team1">
<option value="Anaheim">Anaheim</option>
<option value="Arizona">Arizona</option>
<option value="Boston">Boston</option>
</select><br><br>
<textarea rows="5" cols="80" name="players1"></textarea>
<br><br>
To:
<select name="team2">
<option value="Anaheim">Anaheim</option>
<option value="Arizona">Arizona</option>
<option value="Boston">Boston</option>
</select><br><br>
<textarea rows="5" cols="80" name="players2"></textarea>
<br><br>
<input type="submit" value="Submit trade!">
</form>

答案 1 :(得分:0)

由于你有四个变量,如果在<?php $team1 = (isset($_POST["team1"]))?$_POST["team1"]:''; $team2 = (isset($_POST["team2"]))?$_POST["team2"]:''; $players1 = (isset($_POST["players1"]))?$_POST["players1"]:''; $players2 = (isset($_POST["players2"]))?$_POST["players2"]:''; ?> 条件语句中检查isset($ team1),如果其他任何变量为空,它将抛出错误。

相反,您可以使用三元运算符。示例如下,

{{1}}