Handling forms

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
jlimbo
Forum Newbie
Posts: 10
Joined: Thu Nov 19, 2009 9:33 pm

Handling forms

Post by jlimbo »

Right, this is a slightly odd one but here we go. I've got a form that posts its values to a script which starts with the line

Code: Select all

if($_POST['form'] == 'login') { 


That should be as straightforward as it gets. But php tells me the index 'form' is undefined. But here's the kicker, when I look at the contents of the post array using firebug, they are as they should be with the 'form' variable present. I've checked to see if its just 'form' thats missing, but no, the entire array is empty according to php.

Anyone got any ideas what might be going on

Thanks
John
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Handling forms

Post by shawngoldw »

jlimbo wrote:

Code: Select all

if($_POST['form'] == 'login') { 
Do this:

Code: Select all

if(isset($_POST['form'] && $_POST['form'] == 'login') { 
Shawn
jlimbo
Forum Newbie
Posts: 10
Joined: Thu Nov 19, 2009 9:33 pm

Re: Handling forms

Post by jlimbo »

Tried that. but the test still fails and runs the else clause.
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: Handling forms

Post by timWebUK »

Post the code for your HTML form.
jlimbo
Forum Newbie
Posts: 10
Joined: Thu Nov 19, 2009 9:33 pm

Re: Handling forms

Post by jlimbo »

Here you go

Code: Select all

<form name='loginForm' id='loginForm' class='login' action='formProcessor.php' method='post'><input type='hidden' name='form' value='login' />
	<div class = 'formRow'><p>
		<span class='formLeftCol'>
		<label for='uname'>Username: </label>
        </span>
        <input type='text' name='uname' id='uname' class='formRightCol' value=''  /></p>
    </div>
    <div class='formRow'><p>
    	<span class='formLeftCol'><label for='pwd'>Password: </label>
        </span>
     <input type='password' name='pwd' class='formRightCol' value='' /></p>
    </div>
    <input type='submit' value='Login' name='submit' id='submit' />
</form>   
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: Handling forms

Post by timWebUK »

You should be checking $_POST of the submit button, not the form name.

Code: Select all

if(isset($_POST['submit']))
{
  //... code here

}
jlimbo
Forum Newbie
Posts: 10
Joined: Thu Nov 19, 2009 9:33 pm

Re: Handling forms

Post by jlimbo »

Thanks Tim,

I've just changed that. I was actually about to mark this is resolved as I finally worked out the problem this morning. It turns the problem was purely that I hadn't added 'session_start()' in the form handler and so the query was running ok but the reponse was being lost

Thanks again
John
Post Reply