Page 1 of 1
First variable in array being skipped
Posted: Wed Mar 31, 2004 8:23 pm
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...
Posted: Wed Mar 31, 2004 8:26 pm
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).
Posted: Wed Mar 31, 2004 8:32 pm
by John Cartwright
got any suggestions?
*edit*
ahahah sorry i went back to this thread like 10 min after i read ur post.

Posted: Wed Mar 31, 2004 8:37 pm
by markl999
Apart from the one i already gave?

Posted: Wed Mar 31, 2004 8:46 pm
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
Posted: Wed Mar 31, 2004 9:11 pm
by John Cartwright
ty pozor

Posted: Thu Apr 01, 2004 6:35 am
by John Cartwright
Now if all of them are blank no error is issued.. :S
Posted: Thu Apr 01, 2004 8:19 am
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

Posted: Thu Apr 01, 2004 9:38 am
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
Posted: Thu Apr 01, 2004 11:48 am
by John Cartwright
but $_POST['submit']........... wouldnt that trigger the script?
Posted: Thu Apr 01, 2004 11:52 am
by magicrobotmonkey
Do a hidden form field:
<input type="hidden" name="checker" value="whatev">
and then check
if(!isset($_POST['checker']))
whatever