First variable in array being skipped

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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

First variable in array being skipped

Post by John Cartwright »

First variable in array being skipped:

Code: Select all

<?

$missing = array();

if ($submit=="Continue"){
if(!empty($_POST)) { 
  $required = array('hosting','timelicense');
  foreach($required as $req){ 
    if(empty($_POST[$req])){ 
      $missing[] = $req; 
      $error="1";
    }else{
	  $error="0";
	  $status="0";
	} } } 
?>
If I rotate them it is always the first in array to get skipped...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try not use $error to see if there's an error. Just check if(!empty($missing)){ .. there was an error .. } after the foreach loop.

Using $error as you are there won't work as expected, as if the first is missing then error gets set to 1, if the next element is there, then error gets set to 0 (overwriting the previous 1 value).
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

got any suggestions?


*edit*
ahahah sorry i went back to this thread like 10 min after i read ur post.
:)
Last edited by John Cartwright on Wed Mar 31, 2004 8:46 pm, edited 2 times in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Apart from the one i already gave? ;)
Pozor
Forum Commoner
Posts: 74
Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland

Post by Pozor »

Hello,

with minor changes will your skript run as you except.

Code: Select all

<?php
$missing = array(); 
if ($submit == 'Continue'){ 
  if(!empty($_POST)) { 
    $required = array('hosting','timelicense'); 
    foreach($required as $req){ 
      if(empty($_POST[$req])){ 
        $missing[] = $req; 
        $error += 1; //change
      }else{ 
        //change  (delete line)
        $status=0; //why status = 0;
      } 
    } 
  } 
?>
greez Pozor
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

ty pozor :P
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Now if all of them are blank no error is issued.. :S
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Did you try ...
Try not use $error to see if there's an error. Just check if(!empty($missing)){ .. there was an error .. } after the foreach loop.
That's \the easiest solution and is saves creating temporary vars like $error. If $missing isn't empty then there was an 'error', no need for any other vars :o
Pozor
Forum Commoner
Posts: 74
Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland

Post by Pozor »

Hello,

if $_POST is empty, the code below isn't processed!

Code: Select all

<?php
 if(!empty($_POST)) {
    //code to process
 }else{
   echo 'ERROR: Nothing is sent!';
 }
?>

greez Pozor
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

but $_POST['submit']........... wouldnt that trigger the script?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Do a hidden form field:
<input type="hidden" name="checker" value="whatev">

and then check
if(!isset($_POST['checker']))
whatever
Post Reply