Jons Posted August 9, 2020 Share Posted August 9, 2020 Hi, I have form that populates an array, but when I try and retrieve the contents of the array, they return as blank. Form code is $count="0"; While ($count<$num_rows){ ?> <div> <label><?echo"$Part1[$count]"?></label> <input type="checkbox" name="<?echo"$Part1[$count]"?>" > </div> <? $count++; } ?> <div> <button type="submit" class="btn" name="submit">Submit</button> </div> When I try and access the array with $name = $_POST[Part1[0]]; echo"1 $name<br>"; $name = $_POST[Part1[1]]; echo"2 $name<br>"; $name = $_POST[Part1[2]]; echo"3 $name<br>"; $name = $_POST[Part1[3]]; echo"4 $name<br>"; $name = $_POST[[Part1[4]]; echo"5 $name<br>"; I get the exact results I entered in the form, but if I try and use ; $num_rows="5"; $count="0"; While ($count<$num_rows){ $name = $_POST[Part1[$count]]; echo"1 $name<br>"; $count++; } I get blanks. Any assistance for a new coder appreciated. Jon Quote Link to comment https://forums.phpfreaks.com/topic/311302-form-help/ Share on other sites More sharing options...
gw1500se Posted August 9, 2020 Share Posted August 9, 2020 You don't have your checkbox array specified correctly. Don't confuse the name with the value. <input type="checkbox" name="Part1[]" value="<?echo"$Part1[$count]"?>" > Quote Link to comment https://forums.phpfreaks.com/topic/311302-form-help/#findComment-1580500 Share on other sites More sharing options...
Jons Posted August 10, 2020 Author Share Posted August 10, 2020 Thanks, that makes sense. The way I had it, the name would be changing each time it cycled rather than putting them into an array? Not sure why I am still struggling with this.... <?PHP> $count="0"; While ($count<$num_rows){ ?> <div> <label><?echo"$Part1[$count]"?></label> <input type="checkbox" name="Part1[]" > </div> <? $count++; } <?PHP> $num_common_parts = "5"; $count="0"; While ($count<$num_common_parts){ $name = $_POST['Part1[$count]']; echo"Count $count Name $name<br>"; $count++; } Still gives no output. Quote Count 0 Name Count 1 Name Count 2 Name Count 3 Name Count 4 Name Quote Link to comment https://forums.phpfreaks.com/topic/311302-form-help/#findComment-1580503 Share on other sites More sharing options...
gw1500se Posted August 10, 2020 Share Posted August 10, 2020 (edited) The returned value of the checkbox will exist only if checked. You should use 'isset' to see it if was checked then take appropriate action if it was. You also left off the value parameter. You need both as I showed you. Edited August 10, 2020 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/311302-form-help/#findComment-1580526 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.