tags:

views:

27

answers:

2

I have a form with several input fields, i would like to go through and check to make sure they are all present before continuing.

This is the code i am using

if(isset($_POST['url'])){ $url = $_POST['url']; } else { echo "<error>no url</error></data></xml>"; exit(); }
if(isset($_POST['username'])){ $username = $_POST['username']; } else { echo "<error>no username</error></data></xml>"; exit(); }
if(isset($_POST['password'])){ $password = $_POST['password']; } else { echo "<error>no password</error></data></xml>"; exit(); }
if(isset($_POST['cachename'])){ $cachename = $_POST['cachename']; } else { echo "<error>no cachename</error></data></xml>"; exit(); }
if(isset($_POST['lat'])){ $lat = $_POST['lat']; } else { echo "<error>no lat</error></data></xml>"; exit(); }
if(isset($_POST['long'])){ $long = $_POST['long']; } else { echo "<error>no long</error></data></xml>"; exit(); }
if(isset($_POST['message'])){ $message = $_POST['message']; } else { echo "<error>no message</error></data></xml>"; exit(); }
if(isset($_POST['notes'])){ $notes = $_POST['notes']; } else { echo "<error>no notes</error></data></xml>"; exit(); }
if(isset($_POST['tags'])){ $tags = $_POST['tags']; } else { echo "<error>no tags</error></data></xml>"; exit(); }

The problem im getting, is even when i JUST enter a URL, it returns "no lat". Even when i fill in everything down to notes, it still returns "no lat"

Any ideas?

+1  A: 

Check values in $_POST

echo "<pre>";
print_r($_POST);
echo "</pre>";

Make sure every post variable is set and the names match.

Ólafur Waage
thanks, i had an input named incorrectly.
Patrick
A: 

Not that this will fix your problem (see Ólafur's comment), but, here's a more automated way of performing validation on all of your fields:

$required = array('url', 'username', 'password', 'cachename', 
                  'lat', 'long', 'message', 'notes', 'tags');

while (list($i, $require)=each($required)){
    if(empty($_POST[$require])){
     die('<error>no ' . $require . '</error></data></xml>');
    }else{
     $$require = $_POST[$require];
    }
}


PS: empty() is often better better to use than isset(). An empty string will return true with the isset() function.

brianreavis