Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to send data to my db where if a radio button is checked on one of the rows displayed on my table, it will send the value of "Primary" but the code will not execute completely and shows an error of

Warning: Undefined array key 1


Here is my html:
<form method="POST" action="">
<table>
  <?php while($row = mysqli_fetch_assoc($result)) {  ?>
  <tr>
    <td><input type="text" name="lang[]" value="<?php echo $row['lang'] ?>">
    <td><input type="radio" name="pri[]" value="Primary" 
        <?php if($row['pri']=="Primary"){echo "checked"} ?>>
    </td>
  </tr>
  <?php } ?>
</table>
<button type="submit" name="submit">Send</button>
</form>


My PHP:
<?php

if (isset($_POST["submit"])){
   $lang= isset($_POST["lang"]) ? $_POST["lang"]: '';
   $name= isset($_POST["pri"]) ? $_POST["pri"]: '';
   for($i=0;$i<count($name);$i++){
      echo $name[$i];
      //Update whole DB Table Code
   }
?>


What I have tried:

The echo part works, I am able to get value the value of only the checked radio button but for some reason an error still occurs about the undefined array key of the $name[$i].

I want to be able to update all the data in my db. Lets say they are two rows of data. I still want to update both incase the second data in the text field is changed. So I want to read both radio button and the one that is not checked will be updated as null but i don't know where the error lies
Posted
Updated 11-Jun-23 21:27pm

1 solution

You are using square brackets(array) [ ] in your HTML code to define the 'name' attribute as an array 'name="pri[]"', but in your PHP code, you are treating it as a single value '$name= isset($_POST["pri"]) ? $_POST["pri"]: '';'. I am not sure why you are declaring it as an array in your radio element though, as a single value instead of an array will work fine as you only use the string "Primary" at each row...

You need to change your code to handle the array when you submit your form -
<?php
if (isset($_POST["submit"])) {
   $lang = isset($_POST["lang"]) ? $_POST["lang"] : array();
   $name = isset($_POST["pri"]) ? $_POST["pri"] : array();
   for ($i = 0; $i < count($name); $i++) {
      echo $name[$i];
      // Update whole DB Table Code
   }
}
?>


If you should change your radio and text element name values to not be an array, your code will look like the following, which ran fine in my test -
<form method="POST" action="">
<table>
  <?php while($row = mysqli_fetch_assoc($result)) {  ?>
  <tr>
    <td><input type="text" name="lang" value="<?php echo $row['lang'] ?>">
    <td><input type="radio" name="pri" value="Primary" 
        <?php if($row['pri'] == "Primary") { echo "checked"; } ?>>
    </td>
  </tr>
  <?php } ?>
</table>
<button type="submit" name="submit">Send</button>
</form>

<?php
if (isset($_POST["submit"])) {
   $lang = isset($_POST["lang"]) ? $_POST["lang"] : '';
   $name = isset($_POST["pri"]) ? $_POST["pri"] : '';
   echo $name;
   // Update whole DB Table Code
}
?>


To return only the rows where your user clicked on radio element and it's value was set to "Primary", you can use the following code when your form submit. In my code I will only echo the values, you need to add your code for database table insert accordingly -
<?php
if (isset($_POST["submit"])) {
   $lang = isset($_POST["lang"]) ? $_POST["lang"] : array();
   $name = isset($_POST["pri"]) ? $_POST["pri"] : array();
   
   for ($i = 0; $i < count($name); $i++) {
      if ($name[$i] == "Primary") {
         echo "Language: " . $lang[$i] . ", Priority: " . $name[$i] . "</br>";
         // Add your data handling here
      }
   }
}
?>
 
Share this answer
 
v4
Comments
Richard Deeming 12-Jun-23 4:21am    
The square brackets after the name is the standard way for PHP to handle posted fields with multiple values.
Andre Oosthuizen 12-Jun-23 6:44am    
I understand, in this case there is no multiple values sent, only 1 value 'Primary', which is why I could not understand why OP will have an array name element.
Allysha April 12-Jun-23 11:36am    
There will be multiple values because I am looping through the rows of data i have within my db and viewing it into the table in my html. I want to be able to read all the radio buttons but only the checked radio button will return a value while the rest will just be null. I only now understand that it is only returning 1 value and that index 1 does not exist because only 1 radio button will be chosen at a time but how to i over come that?
Andre Oosthuizen 13-Jun-23 4:45am    
I have updated my solution with code to loop through each row and find only the rows where your radio element has been clicked (value of true)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900