Can i pass a name + a var through POST PHP

Viewed 37

Im writing a web page that updates scores on a database and i was wondering if i could pass a name + a variable through the form POST method e.g. (homeScore $i) where $i is the for loop index. the user can input a number to change the score and i will update the score in that database. heres what i got in the first for loop

<input type="number" name="awayScore <?php echo $i; ?>" value="<?php echo $arrayScores[$i]['awayScore']; ?>">

and when i want to pull the POST in another for loop

$arrayScores[$j]['homeScore'] = $_POST["homeScore $j"];

so far i keep getting an error saying "Notice: Undefined index: homeScore 0" all the way to homeScore 6

1 Answers

Replace

<input type="number" name="awayScore <?php echo $i; ?>" value="<?php echo $arrayScores[$i]['awayScore']; ?>">

With

$val = isset($_POST['awayScore'][$i]) ? $_POST['awayScore'][$i] : "";
<input type="number" name="awayScore[<?php echo $i; ?>]" value="<?php echo val; ?>">

Don't use space character within the field name, you may used [] notation, the isset line is to prevent you from getting error when there's no value in exist in the $_POST['awayScore'] array.

Related