Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

form/submit, how to check if array ISSET()

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

form/submit, how to check if array ISSET()

Postby Jason_C » Fri Jul 11, 2014 2:38 am

I have a form that has multiple check boxes and if the visitor does not select any of them the form is reloaded for them to make a manitory selection of at least one.

here is a snippet of the code... (yes i know i have inline css, these will be changed to classes once it is finished)

Code: Select all
<div style="float: left;<? if (isset($sectors) && empty($sectors)) { echo(' border: 1px solid #FF4040;'); } ?>"><input type="checkbox" name="sectors[]" value="select1"<?
if (isset($sectors) && in_array("select1", $sectors)) { echo(" checked"); } ?>></div><div style="float: left;"> select1
</div><br class="clear">

<div style="float: left;<? if (isset($sectors) && empty($sectors)) { echo(' border: 1px solid #FF4040;'); } ?>"><input type="checkbox" name="sectors[]" value="select2"<?
if (isset($sectors) && in_array("select2", $sectors)) { echo(" checked"); } ?>></div><div style="float: left;"> select2
</div><br class="clear">


When the page reloads if shows a border around each checkbox so they know to check at least one of them as the message above the form will tell them.

What I have a problem with is getting the script to know if the array is set or not. When the page loads first time around nothing is set so not red borders are shown, when the form is submitted then all strings are set and if any are empty then the border is shown to let them know that it much be filled in. But how do I find out if an array ISSET ?

The code above all of this creates the strings for each field, like this for the sectors...

Code: Select all
$sectors = $_POST['sectors'];


For some reason the ISSET part is not knowing if the array is set or not.

if i take out the... isset($sectors) && then the page shows the red border all the time even on the first load, which is not what I need.

Code: Select all
<div style="float: left;<? if (   empty($sectors)   ) { echo(' border: 1px solid #FF4040;'); } ?>"><input type="checkbox" name="sectors[]" value="select1"<?
if (isset($sectors) && in_array("select1", $sectors)) { echo(" checked"); } ?>></div><div style="float: left;"> select1
</div><br class="clear">
Jason_C
New php-forum User
New php-forum User
 
Posts: 29
Joined: Sat Nov 18, 2006 7:32 am

Re: form/submit, how to check if array ISSET()

Postby kladrian » Thu Aug 14, 2014 11:18 am

what about a simple check like

Code: Select all
if (!isset($_POST['sectors'])) {
  // show error message
}


---
kladrian
kladrian
New php-forum User
New php-forum User
 
Posts: 12
Joined: Thu Aug 14, 2014 1:13 am


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.