[Solved] Finding form field count

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

[Solved] Finding form field count

Post by Addos »

Hi,
I have a form with 3 fields and I want to go to a specific page if only one of the fields contains a value or to another page if two or more fields contain a value.
I know I need to loop through the form fields to see if only one is populated but I’m still learning and not sure how to approach this.

I was trying to use the following however I’m sure there must be an easier and simpler way as this is not giving me the required results. To some extent this works but only if the first of the fields in the form is populated but if I enter something into the second field only it will send me to the wrong page.

Thanks for any help

Code: Select all

//if (isset($_POST['v_number_1'], $_POST['v_number_2'], $_POST['v_number_3'])){
$array = array($_POST['v_number_1'], $_POST['v_number_2'], $_POST['v_number_3']);
$results=(array_count_values($array)); 
 
foreach ($results as $value=>$occurrences){
    if ($occurrences == 1) {
        $goTo = 'voucher_merge_update.php <br>';
        
        } else { 
        $goTo =  'voucher_update_4.php <br>';
        
        } // END if ($occurrences > 1) {
    }// END foreach ($results as $value=>$occurrences){

Code: Select all

<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
 
Voucher 1:<input type="text" name="v_number_1" value="<?php if (isset($_POST['v_number_1'])) echo $_POST['v_number_1'];?>" size="32">
    
 Voucher 2:  <input type="text" name="v_number_2" value="<?php if (isset($_POST['v_number_2'])) echo $_POST['v_number_2'];?>" size="32">
 
Voucher 3:<input type="text" name="v_number_3" value="<?php if (isset($_POST['v_number_3'])) echo $_POST['v_number_3'];?>" size="32<input type="submit" value="Apply Vouchers">
 
  <input type="hidden" name="MM_update" value="form1">
 
</form>
Last edited by Addos on Sun Aug 10, 2008 4:27 am, edited 1 time in total.
filippo.toso
Forum Commoner
Posts: 30
Joined: Thu Aug 07, 2008 7:18 am
Location: Italy
Contact:

Re: Finding form field count

Post by filippo.toso »

Code: Select all

<?php
 
$_POST['v_number_1'] = isset($_POST['v_number_1']) ? $_POST['v_number_1'] : '';
$_POST['v_number_2'] = isset($_POST['v_number_2']) ? $_POST['v_number_2'] : '';
$_POST['v_number_3'] = isset($_POST['v_number_3']) ? $_POST['v_number_3'] : '';
 
if (($_POST['v_number_1'] != '') || ($_POST['v_number_2'] != '') || ($_POST['v_number_3'] != '')) {
  if ((($_POST['v_number_1'] != '') && ($_POST['v_number_2'] != '')) ||
     (($_POST['v_number_2'] != '') && ($_POST['v_number_3'] != '')) ||
     (($_POST['v_number_1'] != '') && ($_POST['v_number_3'] != ''))) {
    echo('goto page 2');
  } else {
    echo('goto page 1');
  } 
}
 
?>
 
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Re: Finding form field count

Post by Addos »

Thank you that was really helpful and appreciated.
thanks
Post Reply