Script not recognising last variable

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
Puffmac
Forum Newbie
Posts: 4
Joined: Wed Oct 21, 2009 3:35 pm

Script not recognising last variable

Post by Puffmac »

Hello,

I've got a script which checks if any variables are unset in the URL:

Code: Select all

 
  function ProcessForm()
  {
    $InputFields = array( # All of the inputs we need
      'dep','arr','fltno','cruise','fuel','resfuel',
      'total','left','right','center','deptime','route',
      'remarks'
    );
    for($i = 0;$i < sizeof($InputFields);$i ++)
    {   
      if(!isset($_REQUEST[$InputFields[$i]]))
      {
        return false; # If any of the inputs aren't set, return false. This in turn creates the form via the Init function
      }
    }
    Output::BuildFile();
    return true; # By default return true, this line will be reached if all input fields are filled in.
  }
 
However, it doe not recognise when the last variable is unset, it seems to skip it. I tested this theory by adding an empty value after 'remarks' in the InputFields array. It then returned false when remarks was unset. It also returns false if you remove remarks from the URL.

Please Help,

Thanks,
Andrew
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Script not recognising last variable

Post by requinix »

Puffmac wrote:It then returned false when remarks was unset. It also returns false if you remove remarks from the URL.
This confuses me. What's the difference between "unset" and "not in the URL"?
Puffmac
Forum Newbie
Posts: 4
Joined: Wed Oct 21, 2009 3:35 pm

Re: Script not recognising last variable

Post by Puffmac »

tasairis wrote:
Puffmac wrote:It then returned false when remarks was unset. It also returns false if you remove remarks from the URL.
This confuses me. What's the difference between "unset" and "not in the URL"?
Hi,

When I remove '&remarks=' from the URL, it is working properly. So I think it's something to do with the last value in the array. To test this what I did was add an extra value to the array:

Code: Select all

 
    $InputFields = array( # All of the inputs we need
      'dep','arr','fltno','cruise','fuel','resfuel',
      'total','left','right','center','deptime','route',
      'remarks','' # <<< There
    );
 
It then worked properly, but that can't be nescessary, IMO. It must be something in my code.

Cheers,
Andrew
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Script not recognising last variable

Post by markusn00b »

Hi, Andrew.

It works fine for me. What is the URL that is causing the trouble?
Puffmac
Forum Newbie
Posts: 4
Joined: Wed Oct 21, 2009 3:35 pm

Re: Script not recognising last variable

Post by Puffmac »

markusn00b wrote:Hi, Andrew.

It works fine for me. What is the URL that is causing the trouble?
Do you want a link?
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Script not recognising last variable

Post by markusn00b »

Do you mean when $_GET['remarks'] has no value? If &remarks is present in the URL, it is 'set', so to speak. If you want to check whether they have a value, compare them against an empty string:

Code: Select all

 
function ProcessForm()
  {
    $InputFields = array( # All of the inputs we need
      'dep','arr','fltno','cruise','fuel','resfuel',
      'total','left','right','center','deptime','route',
      'remarks'
    );
    for($i = 0;$i < sizeof($InputFields);$i ++)
    {  
      if(!isset($_REQUEST[$InputFields[$i]]) || $_REQUEST[$InputFields[$i] == '')
      {
        return false; # If any of the inputs aren't set, return false. This in turn creates the form via the Init function
      }
    }
    return true; # By default return true, this line will be reached if all input fields are filled in.
  }
 
Mark.

P.S. If you know you're using GET, don't waste time by using REQUEST.
Puffmac
Forum Newbie
Posts: 4
Joined: Wed Oct 21, 2009 3:35 pm

Re: Script not recognising last variable

Post by Puffmac »

Mark,

Thanks very much. It works now :).
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Script not recognising last variable

Post by markusn00b »

Puffmac wrote:Mark,

Thanks very much. It works now :).
Welcome.

See ya around,
Mark.
Post Reply