I'm trying to do a simple form that has two textareas, and two drop-downs in 1 form. I'm trying to save the values of the POST but it's not working, I keep getting:
Notice: Undefined index: team2
Notice: Undefined index: players2
My POST page is this:
$team1 = $_POST["team1"];
$team2 = $_POST["team2"];
$str1 = $_POST["players1"];
$str2 = $_POST["players2"];
And my form is this:
<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>
<?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>
Since you have four variables, if you check isset($team1) inside if
condition statement, it will throw error if any of other variables are empty.
Instead you can use ternary operators. Example as below,
<?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"]:'';
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.