If..Else statement help

This is a discussion on "If..Else statement help" within the PHP Forum section. This forum, and the thread "If..Else statement help are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Oct 4th, 2006, 08:28
New Member
Join Date: Sep 2006
Location: United Kingdom
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
If..Else statement help

Hi,

I have a page that has an input box on it, I have a bit of code that says if the user enters the numbers 123 in the input box named serial_number then they will be progressed onto the support.php page, if they enter a different number then they will get a different page.

The code I have looks ok (although im no php whizz ! ) it just doesnt seem to work.

Was hoping somebody here might be able to spot what is wrong.

here are two snippets of my code, firstly the php code

Quote:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Support</title>
<link href="../style.css" rel="stylesheet" type="text/css">
</head>
<?php
if($_GET['do'] == "form" ){
die($_POST['serial']);
exit;
if(substr($_POST['serial'], 0, 2) == "123"){
header("Location: support_welcome.php"); /* Redirect browser */
exit;
}else{
header("Location: error.php"); /* Redirect browser */
exit;
} // Close Serial Number check statement
} // Close First IF Statement
?>
<body>
<table width="745" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td valign="top">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="295" valign="top" rowspan="2">&nbsp;</td>
<td width="449" height="19"></td>
<td width="1"></td>
</tr>
<tr>
Secondly, the code for the input box

Quote:
<form name="form1" method="post" action="./support.php?do=form">
<p>
<input type="text" name="serial" id="serial"><br /><br /><br />
<input type="submit" value="Submit Serial">
</p>
</form>
</td>
</tr>
<tr>
Hopefully someone can see whats wrong !
Reply With Quote

  #2 (permalink)  
Old Oct 4th, 2006, 12:18
Up'n'Coming Member
Join Date: Jun 2006
Location: Florida
Age: 29
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Re: If..Else statement help

It looks like you are telling the scripit to die if the user clicks submit

PHP: Select all

 if($_GET['do'] == "form" ){
die(
$_POST['serial']);
exit; 
If you remove the:
PHP: Select all

die($_POST['serial']);
exit; 
at the top the scripit should work
Reply With Quote
  #3 (permalink)  
Old Oct 4th, 2006, 19:53
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: If..Else statement help

All your 'exit' instruction are surplus to requirements and will stop things happening as you intended.

Also, because you are using redirection, you need to buffer the output that goes back to the browser until you have made you decision.

Do this by adding;
Code: Select all
ob_start();
at the very top of your page and make your last bit of php code;
Code: Select all
ob_end_flush();
Reply With Quote
  #4 (permalink)  
Old Oct 6th, 2006, 13:40
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: If..Else statement help

I've never done things like this, so I may be missing some points in your method. I always structure the submit button with a "value=submit" and use "if (isset($_POST['submit']))" or "if (isset($_GET['submit']))" as my trigger. Sometimes I'll pass a variable "&sumitted=yes" with a hidden button.

First off, I don't understand how you're going to pass a value onto another page or even to your "if ($_GET)" conditional with (method="post") in your form. Aren't you looking for method="get"? Like I said, you might have an alternate technique I don't know -- if so, just ignore me But apparently you are looking to pass a value 123** to the support_welcome page and it just isn't that hard.

Second, Geoff is certainly right - you can't just pass a header to the browser after you've passed anything else. One bit of whitespace passed to the browser and your header requires buffering. The first thing that happens on your page is that the user's brower receives html.

But the whole point of the GET method is that you don't need a header to redirect the browser. Once the form is submitted with (action="another page") then php will point to the code located on the action page and pass the GET variables to it, without need for a redirect. The redirection is already placed in the GET method.

Using a redirect header for the error page, with buffering, will work for the error portion. But it seems unnecessarily complex. My first instinct would be to do the form with a single submit and choose the action like this:
Code: Select all
<form name="form" method="get" action="
<?php
 if (substr($_GET['serial'], 0, 2) == "123") { 
echo './../support_welcome.php'; 
}else{ echo 'error.php';} 
?>
"><input . . .
I've never seen this but I'm pretty sure it would work. It should be all the code you need, other than boilerplate. You might want an "if" clause in the main php section to trim any variables off the error URL. It might be more satisfactory to use a "die" statement in the conditional.
Reply With Quote
Reply

Tags
code problem, help needed, if else

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP If Statement... mcdanielnc89 PHP Forum 16 Dec 9th, 2007 17:44
SELECT statement gecastill Databases 1 Feb 15th, 2006 23:27
Help with If Statement. JohnMitch Classic ASP 2 Jan 5th, 2005 01:05
With Statement Trebz Classic ASP 2 Feb 2nd, 2004 14:56


All times are GMT. The time now is 02:00.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43