EMAW and Cougs
EMAW and Cougs

Reputation: 23

Validate PHP GET form to an array?

I am writing a code that I am sure is very simple, but for some reason I am having trouble. It seems like it would be logical to write it out as it is. If the $_GET != $arr then return false with an error.

The situation is we give two different groups a code, there are three codes in all. The third code is a catch all that takes them to a form that is un-modified, so it is an initial value or will be. The other two take them to two different forms that is assigned to each code.

What I am trying to get to do is if they enter the code correct then fine it redirects them, if they do not enter anything they get an error that says sorry you must enter your code. If they enter it wrong an error that says check your code and try again or call this number for help.

The error is where I am having trouble creating. If you would help me figure out how to validate the $_GET form to the arrays and if it comes out false to pop out the error.

   <?php

$arr = array(001,002,003);
    if ($_GET['code'] === "001") {
        header("Location: http://stackoverflow.com/");      
    }
    if ($_GET['code'] === "002") {
        header("Location: http://gaming.stackexchange.com/");
    }
    if ($_GET['code'] === "003") {
        header("Location: http://gamedev.stackexchange.com/");
    }
if ($_GET ['code'] != $arr); 
else {
echo("Uh oh, you did not enter the correct code error message");
}
?>
</head>

<body>
<form id="galaonly" name="galaonly" method="get" action="">
  <input name="code" type="text" value="001" size="50" maxlength="150" />
  <input type="submit" value="Submit" />
</form>

Upvotes: 1

Views: 228

Answers (7)

arunkumar
arunkumar

Reputation: 34153

Try using the in_array function. Like this:

<?php 

if (!in_array($_GET['code'], $arr)) {
    echo("Uh oh, you did not enter the correct code error message");
}

You can also use the php switch/case statement instead of the multiple if statements. In which case you don't need the error check separately, you can use switch/case's default to catch the error. Like this -

<?php 

switch($_GET['code']) {
    case '001':
        header("http://location1");
        break;
    case '002':
        header("http://location2");
        break;
    case '003': 
        header("http://location3");
        break;
    default:
        echo("Uh oh, you did not enter the correct code error message");
} 

Upvotes: 1

uadnal
uadnal

Reputation: 11445

I think this might be what you want

if ($_GET['code'] == "001") {
        header("Location: http://stackoverflow.com/");      
    }
    else if ($_GET['code'] == "002") {
        header("Location: http://gaming.stackexchange.com/");
    }
    else if ($_GET['code'] == "003") {
        header("Location: http://gamedev.stackexchange.com/");
    }
    else {
       echo "Uh oh, you did not enter the correct code error message";
    } 

Otherwise you're looking for in_array

if (!in_array($_GET['code'], $arr))
    // error stuff here

Upvotes: 0

Sascha Galley
Sascha Galley

Reputation: 16101

i think you mean the following:

if (! in_array($_GET ['code'], $arr))
    echo('error');

but you could also do it like so:

$arr = array(
    '001' => 'http://stackoverflow.com/',
    '002' => 'http://gaming.stackexchange.com/',
    '003' => 'http://gamedev.stackexchange.com/'
);

if (isset($_GET['code']) {
    if (isset($arr[$_GET['code']])
        header("Location: " . $arr[$_GET['code']);
    else
        echo 'error';
}

Upvotes: 1

Troy
Troy

Reputation: 1639

Something like this, perhaps?

$pages = array(
    '001' => 'http://stackoverflow.com/',
    '002' => 'http://gaming.stackexchange.com/',
    '003' => 'http://gamedev.stackexchange.com/'
);
$code = $_GET['code'];
if (in_array($code, $pages, true)) { // The true here makes in_array check for type as well.
    header("Location: " . $pages[$code]);
} else {
    echo("Uh oh, you did not enter the correct code error message");
}

I think you were just looking for the in_array() function.

EDIT: The advantage of this approach (as opposed to using a switch or if/else construct) is that it's quicker and easier to maintain, and properly expresses the idea that you're mapping codes to redirect URLs. If you end up with a lot of these codes, you can load the array from a CSV file or something like that, meaning that you won't even have to edit your source code.

Upvotes: 1

Londeren
Londeren

Reputation: 3341

You can use switch construction

switch $_GET['code']
{
case "001":
    // your code
  break;
  case "002":
    // your code
  break;
  case "003":
    // your code
  break;
  default:
    echo("Uh oh, you did not enter the correct code error message");
  break;
}

Upvotes: 1

Shef
Shef

Reputation: 45599

<?php
if(isset($_GET['code'])){
    $arr = array('001', '002', '003');

    if(in_array($_GET['code'], $arr, true)){
        if($_GET['code'] === '001'){
            header("Location: http://stackoverflow.com/");
        } elseif($_GET['code'] === '002') {
            header("Location: http://gaming.stackexchange.com/");
        } elseif($_GET['code'] === '003'){
            header("Location: http://gamedev.stackexchange.com/");
        }
    } else{
        echo 'Uh oh, you did not enter the correct code error message';
    }   
}
?>

</head>

<body>
<form id="galaonly" name="galaonly" method="get" action="">
  <input name="code" type="text" value="001" size="50" maxlength="150" />
  <input type="submit" value="Submit" />
</form>

Upvotes: 1

Chris Laplante
Chris Laplante

Reputation: 29668

Try:

if (!in_array($_GET['code'], $arr)) {

to catch an invalid code.

However, I think that this section of code would be better written using a switch statement, provided that the codes won't change too often.

Upvotes: 1

Related Questions