Forum Moderators: coopster

Message Too Old, No Replies

Redirect to page based upon condition

Go to a particular .php page if a certain condition is met

         

neophyte

4:36 am on Aug 24, 2004 (gmt 0)

10+ Year Member



I'm sure this question is really showing my "neophyte" status in php, but here goes:

I've got a form that has a number of buttons. On submit, certain functions are called and, depending on which button was pressed, certain things are processed. So far, that part works fine.

However, in two instances (add record and edit record) I want to display two different pages. This is where I'm having the problem.

As you can see below in the code sample, if $buttonHit == Edit - I want to display edit_row.php; ELSE I want to display Add_row.php.

if ($buttonHit == 'Delete'){
deleteEntry($rowId);
} else if ($buttonHit == 'Edit') {
// LOAD EDIT_ROW.PHP;
} else {
// LOAD ADD_ROW.PHP;
}

Seemed straight forward enough to me at the outset, but I can't find the right .php function or syntax to get the job done.

I know I'm probably doing this the hard way: I've read issues about a page being able to call it self based upon a condtion but, for me, I really need to take this one step at a time (I am an absolute beginner at php) or else my brain starts to frost over.

As always, any help and guidence is greatly appreciated.

Neophyte

Birdman

6:37 am on Aug 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's the header() [php.net] function you are after.

if ($buttonHit == 'Delete'){
deleteEntry($rowId);
} else if ($buttonHit == 'Edit') {
header("Location: /EDIT_ROW.PHP");
} else {
header("Location: /ADD_ROW.PHP");
}

neophyte

7:37 am on Aug 24, 2004 (gmt 0)

10+ Year Member



Hi Birdman -

Actually tried that afternoon after scouring the internet for a solution to this problem before I went about bothering the membership here.

Here's the code with header included:

if ($buttonHit == 'Delete'){
deleteEntry($rowId);
} else if ($buttonHit == 'Edit') {
header("Location: /con_edit_entry.php");
//editEntry($rowId);
} else {
header("Location: /con_add_entry.php");
//addEntry();

here's what I get when I try and use the header function:

Warning: Cannot modify header information - headers already sent by (output started at c:\apache\htdocs\learning_php\con_process_change.php:11) in c:\apache\htdocs\learning_php\con_process_change.php on line 21

"con_process_change" is the page which evaluates the $_POST variable and then redirects to the "add" or "edit" page based upon the condition statement.

I'm sure it's "operator error" on my part, but I don't know where the error lies.

Neophyte

Netizen

8:57 am on Aug 24, 2004 (gmt 0)

10+ Year Member



Or:

if ($buttonHit == 'Delete'){
deleteEntry($rowId);
} else if ($buttonHit == 'Edit') {
include("con_edit_row.php");
} else {
include("con_add_row.php");
}

if you want to be able to access other parameters passed to the script from the form.

Mr_Brutal

9:15 am on Aug 24, 2004 (gmt 0)

10+ Year Member



Hi, the

"Cannot modify header information"

refers to the fact that you have already sent some content back to the client browser e.g. <html> or something (this is includes single spaces). To use the header function you mustn't send anything back to the browser before its called.

You'll have to check your code and see if you can call the header function before you send any HTML etc.

HTH

lazydog

11:22 am on Aug 24, 2004 (gmt 0)

10+ Year Member



To get rid of the "cannot modify header", you can use the output control functions.

[php.net...]

Saurabh.

neophyte

2:16 am on Aug 25, 2004 (gmt 0)

10+ Year Member



Used the "include" technique within the conditional statement which worked just fine.

Thanks to all!

Neophyte