Error Checking for at least one value selected in multi-value list before submission

5 posts by 3 authors in: Forums > CMS Builder
Last Post: January 14, 2016   (RSS)

By gkornbluth - January 12, 2016

Hi All,

I need to do an error check to make sure that at least one search criteria value has been selected in a multi-value pull down list (called provider_state (as in NY, CT, VT etc.)) before form submission.

I've tried using the functions in_array and also isset, and I can’t seem to get it right.

Any ideas? (Or maybe there’s a much simpler approach...)

Thanks,

Jerry Kornbluth

Here’s what I’m using at the top of my page.

<?php $errorsAndAlerts = ""; ?>
<?php if (@$_REQUEST['save'] == 1) {
    $string = '[practice_state_keyword]';
$array = $_REQUEST;
if ( !in_array( $string, $array ) )
{
   $errorsAndAlerts .= "You'll need to choose at least a State and re-submit your search to continue<br/>\n";

 // on success
 
    if (!$errorsAndAlerts) {
 
      $errorsAndAlerts = "Thanks, your search can continue!<br/>\n";
    }
 }    
    
}?>



I also tried:

<?php $errorsAndAlerts = ""; ?>
<?php if (@$_REQUEST['save'] == 1) {
   
$var = '$_REQUEST[practice_state_keyword]';

if ( !isset($var ) )
{
   $errorsAndAlerts .= "You'll need to choose at least a State and re-submit your search to continue<br/>\n";

 // on success
 
    if (!$errorsAndAlerts) {
 
      $errorsAndAlerts = "Thanks, we've updated your profile!<br/>\n";
    }
 }    
    
}?>


And here is the trimmed version of my form

<table>
    <form method="post" action="">
      <input type="hidden" name="save" value="1" />
      <tr>
        <td >State:</td>
        <td>
<select name = "practice_state_keyword" multiple>
           
            <?php foreach (getListOptions('accounts', 'practice_state') as $value => $label1): ?>
            <option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['practice_state']);?>> <?php echo $label1; ?></option>
            <?php endforeach ?>
          </select></td>
      </tr>
</table>


When a practice_state_keyword is selected showme( $_REQUEST ) displays:

Array
( [save] => 1
[practice_state_keyword] => 7
)


and when it’s not (or before submission) it displays:

Array
(
)

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Damon - January 13, 2016

Hi Jerry,

Just wanted to check, is this what you are working with Ross on through consulting?

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By gregThomas - January 14, 2016

Hi Jerry,

I've taken a look over the code, I noticed a couple of things that need changing to get it running.

The HTML look good, you just need to add a couple of square brackets to the end of the name to get it to send multiple values:

<table>
  <?php echo $errorsAndAlerts; ?>
  <form method="post" action="">
    <input type="hidden" name="save" value="1" />
    <tr>
      <td >State:</td>
      <td>
        <select name="practice_state_keyword[]" multiple>
          <?php foreach (getListOptions('blog', 'category') as $value => $label): ?>
            <option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['practice_state']);?>> <?php echo $label; ?></option>
          <?php endforeach ?>
        </select>
      </td>
    </tr>
    <input type="submit" name="go" value="go">
  </form>
</table>

Then you can check that they've selected at least one item by checking that something is set in the value practice_state_keyword:

  $errorsAndAlerts = "";
  if(@$_REQUEST['save']){

    if(!@$_REQUEST['practice_state_keyword']) { $errorsAndAlerts .= "You must select at least one state!<br>\r\n"; }

    if(!$errorsAndAlerts){
      //NO ERRORS
    }
  }

So the above code checks that we can find the save variable, and if we can, we then check that something is set in the practice_state_keyword variable. 

Let me know if you have any questions!

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gkornbluth - January 14, 2016

Thanks Greg,

I'm out of town for a few days bit i'll make the changes as soon as I get back to the office.

Appreciate your help

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php