Click to See Complete Forum and Search --> : $_POST['submit'] problem!!!!!


j-dearden
02-09-2007, 08:41 AM
Hey there

im havin a slight problem whith the $_POST['submit'] variable to check whether or not my form has been posted.

all my proccessign is doe on the same page, therefore i obviosuly need to check whether the form hass been submitted befire anything can be done with the data. i have used this in the past with no problems, but for some reason it doesnt seem to want to work ne more.

this is the simple code i have got just to check it........

if(isset($_POST['submit']))
{
do something with the einformation
}

and the correspopnding form...........

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
form elements
</form>

how ever this does not work do any of you have any ideas?

and by the way i have just set up a new php/mysql environment on my computer, could it have some thing to do with the php config files? i am using apache at the mo on local host.

cheers

john

apg88
02-09-2007, 09:19 AM
You need to have name="submit" for your submit button in order for this to work.
<input type="submit" name="submit" />

j-dearden
02-09-2007, 09:22 AM
Sry didnt notice id missed that off, still doesnt work though ne other ideas? im lost.

apg88
02-09-2007, 09:28 AM
Put this at the top of your PHP script:
echo "<pre>";
print_r($_REQUEST);
echo "</pre>";
die();

See what data is been sent to the page.
Post it here if you don't understand it.

j-dearden
02-09-2007, 09:32 AM
Cheers for that, the vale set is 'Submit', not submitted, so does the $_POST global variable use an associative array?

apg88
02-09-2007, 09:34 AM
Yeah, $_POST, $_GET, etc... are arrays.

j-dearden
02-09-2007, 09:47 AM
Once the form hass been posted is there a way of unsetting the entire array like unset($_POST)?

apg88
02-09-2007, 09:58 AM
Unset should work.
I don't see why you would have to do that though.

j-dearden
02-09-2007, 10:02 AM
I was just thinking becaus correct me if im wrong.

once i submit the form, $_POST['Submit'] will be set therefore the code within the if will be executed, hence parse information which as not been supplied, however if i unset the $_POST['Submit'], the if statement cannot be exucted until the user resends the form containing new info.

am i think correctly here?

apg88
02-09-2007, 10:10 AM
I'm not exactly sure what you are trying to do.
If the user submits the form, then you process it right? So why wouldn't you want to process it?

j-dearden
02-09-2007, 12:04 PM
Sry if it sounds a little vague..

let me try and explain a little more..

1. User fills in details

2. submits form

3 details proccessed

Since i am using PHP_SELF, the user is displayed with the form yet again. but because the form as already been submitted once the $_POST variable is now set, so wen the page is loaded again the if ststament will be executed since the variable is set.

does that make ne more sense?

polorboy
02-09-2007, 01:36 PM
Yeah, but you will not need to unset the $_POST variable, the next time they submit the form the $_POST['Submit'] will be reset anyway. Also, I would not recommend unsetting a global anyway, that could be pretty bad. If I remember correctly I read in the php manual that if you unset a global it will not allow you to write to it again. If you absolutaly think you need to do that, then do unset($_POST['Submit']), don't unset the entire array, just the variable you want to unset. But still, there should be no reason where you would need to unset a $_POST variable. I have ton's of pages where it has $_SERVER['PHP_SELF'] and I have not ever had to unset the $_POST to be able to re-use the form. As far as I understand it, when you resubmit the form, all the old data will be over written anyway and the new data will be used, only if you hit the refresh button in the browser will the old data be used again.

baZzz
02-09-2007, 02:47 PM
Damn, I've had this problem as well in the past but I forget what the problem was exactly! :(
Instead, try the code below and see if it works better:
if ($HTTP_SERVER_VARS['REQUEST_METHOD'] == 'POST')
{
//process data
}
Good luck!

j-dearden
02-09-2007, 03:58 PM
Cheers for the responses much appricated, keep em comin ;)

the problem has been solved mind u, but im sure other people would benefit from these posts.

felgall
02-09-2007, 09:49 PM
One problem with giving your submit button the name "Submit" is that Internet Explorer will then overwrite the submit method with it so that you may then not be able to submit the form.

j-dearden
02-10-2007, 05:04 AM
Realy? i didnt know that... thanks for the advice.