Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple php conditional question

Status
Not open for further replies.

electricphp

Programmer
Feb 20, 2008
71
US
Ihave this line of code:

Code:
		   if ($_POST["town"] != "" && $_POST['submit'] == "yes"){header("Location: searchresults.php");}


For some reason it will not redirect even if both contitions are met. for some reason if i just use this:

Code:
		   if ($_POST["town"] != ""){header("Location: searchresults.php");}

then it works, what could be the problem
 
Hi

Probably the $_POST['submit']'s value is not "yes".

Note that if you not explicitly press the submit button, then some browsers will not send any submit button related information.

Feherke.
 
don't think that's the problem,

here's the code for the submit button:

Code:
<INPUT name="submit" TYPE="IMAGE" id="submit" SRC="images/singlesearch_button.png" border="0" value="yes">
 
[0] Use "submit" as name or id for the submit type button (image type included) is to pose client-side scripting problem sooner or later. But this is not critical to this issue here. I just want to get this out of the way first.

[1] Use the value in the submit button of type image is not a reliable way to pass form data to the server. It is subject to the caprice of browser-dependent. Ie will ignore the value and ff will submit it. This is documented in ie's documentation as well. And I won't blame ie for compliance. It does not offer unique reading at this spot.

[2] The robust posted form data associated with image type is [tt]$_POST['submit.x'][/tt] and [tt]$_POST['submit.y'][/tt], that all. [tt]$_POST['submit'][/tt] is browser-dependent as described in [1]. Image the user is using ie...
 
Basically, it comes down to the fact, that Image submit buttons like the one you are using generally don't send over their value when the form is submitted. but rather send the coordinates of were you clicked on the image.


Try echoing the $_POST variable and you'll see you are getting x and y values instead of the actual value of the button.
Code:
echo "<pre>";
print_r($_POST);
echo "</pre>";

So you'll want to check for that rather than the value.


Code:
 if ($_POST["town"] != "" && [red]isset([/red]$_POST['submit'][red]['x'])[/red]{header("Location: searchresults.php");}

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Try bracketing the 2 conditions separately
Code:
if ($_POST["town"] != "" [COLOR=red])[/color] && [COLOR=red]([/color]$_POST['submit'] == "yes"){header("Location: searchresults.php");}

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
[url=http://www.essexsteam.co.uk/]Drive a Steam Roller[/url]
 
what I'm trying to do is make the page redirect only if the submit button is pressed.

There is another way the form gets submitted, by a javascript on a dropbox. I don't want it to redirect then.

How can I do this?
 
Like I said, check for the x or y ooordinate. If Javascript submitted the form, then the coordinates won't be there.

So checking if they are set will let you know when the button was pressed.

Code:
if ($_POST["town"] != "" && isset($_POST['submit']['x']){header("Location: searchresults.php");}

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
The condition should be read like this. (Singling out _x is not generic of any sort, just being practical.)
[tt] if ($_POST["town"] != "" && $_POST['submit[red]_x[/red]']]!=null){header("Location: searchresults.php");}[/tt]
ps: [erratum-self] I also have to correct my own post above: .x and .y should be read _x and _y.
 
There is a typo slipped in, a re-take of the corresponding line.
[tt] if ($_POST["town"] != "" && $_POST['submit[red]_x[/red]']!=null){header("Location: searchresults.php");}[/tt]
 
Ohh, yes,its $_POST['submit_x'] that was my mistake.

However I still think checking whether its set, is better than checking whether its null. But that's just me.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top