Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 checkboxes, which user can check and then submit and I am trying to print out the name of the checkbox, that was checked as first, but i keep getting these 2 errors -

Warning: Undefined array key "fruits" in path on line… (number)

Warning: Trying to access array offset on value of type null in path on line… (number)

Here is the code -
<body>

<form action="index.php" method="post">

Apples: <input type="checkbox" name"fruits[]" value="apples"><br>

Oranges: <input type="checkbox" name"fruits[]" value="oranges"><br>

Pears: <input type="checkbox" name"fruits[]" value="pears"><br>

<input type="submit">

</form>

<?php

//if(isset($_POST["fruits"])){

$fruits = $_POST["fruits"];

echo $fruits[0];

//}

?>

</body>

<pre>


You can see, that there is an if statement, which is commented out. When that if statement is not commented out, then these 2 errors disappear, however the result is still not being printed.

Can anybody please help me?

What I have tried:

a lot of things - especially with the isset part of the php code
Posted
Updated 26-Jan-23 21:12pm
v3
Comments
Richard MacCutchan 20-Mar-22 8:37am    
You have three checkboxes all with the same name but different values. So that is the first thing to correct. You then try to refer to the item named "fruits" in your if statement, but the names used in your html is "fruits[]". So that is the second thing you need to fix. You should also understand the difference between a simple variable and an array.
Member 15572410 20-Mar-22 8:40am    
This code is written according to this tutorial -
https://youtu.be/OK_JCtrrv-c?t=6957
Dave Kreskowiak 20-Mar-22 11:31am    
Mistake #1. You're using YouTube videos to learn PHP. YouTube videos are probably the worst way to learn as most people who make videos have no idea how to teach a language.

Get a book or two on PHP. That way you get a much better learning experience, a bunch of sample code, and references, all in one place.
Member 15572410 20-Mar-22 11:34am    
Thank You, I will consider doing so. Do You have any particular book or books on Your mind?
Dave Kreskowiak 20-Mar-22 11:43am    
I don't have any book recommendations because I don't do PHP.

You'll have to look into the books yourself. A book that I find good may be not so good for you.

You never brought an assignment operator.eg
You wrote name "fruits[]".
Instead of name = "fruits[]".
Hope it helps
 
Share this answer
 
Hey make a note of the name attribute in the input element and give it a try.
The below code is for your reference, it is working
<body>
    <form action="madLibs.php" method="post">
    Apples: <input type="checkbox" name="fruits[]" value = "apples"> <br>
    Oranges: <input type="checkbox" name="fruits[]" value = "oranges"> <br>
    Pears: <input type="checkbox" name="fruits[]" value = "pears"> <br>
    <input type="submit" value="submit">
    </form>
    <br> 

    <?php

    $fruit = $_POST['fruits'];
    echo $fruit[1];

    ?>
</body>
 
Share this answer
 
The first thing you have to do is remove that sqaure brackets inside the name attributes and here you are solved
 
Share this answer
 
Comments
Dave Kreskowiak 16-Mar-24 10:59am    
I seriously doubt the OP is still working on this problem two years later.

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