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
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Wed Mar 31, 2004 8:23 pm
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...
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Wed Mar 31, 2004 8:26 pm
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).
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Wed Mar 31, 2004 8:32 pm
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.
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Wed Mar 31, 2004 8:37 pm
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 » Wed Mar 31, 2004 8:46 pm
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
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Thu Apr 01, 2004 6:35 am
Now if all of them are blank no error is issued.. :S
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Apr 01, 2004 8:19 am
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
Pozor
Forum Commoner
Posts: 74 Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland
Post
by Pozor » Thu Apr 01, 2004 9:38 am
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
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Thu Apr 01, 2004 11:48 am
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 » Thu Apr 01, 2004 11:52 am
Do a hidden form field:
<input type="hidden" name="checker" value="whatev">
and then check
if(!isset($_POST['checker']))
whatever