Solved

Redirecting in PHP

Posted on 2003-10-23
9
1,084 Views
Last Modified: 2008-02-01
Hi I did a registration form and I want that when the user have error when they submit the form that another window open with the errors over there and the main one with the registration form so they can modify theirs errors.

I now I can redirect with

header ("Location: test.php");

but I try to do the target in the location and I didn't make it work

Thanks

0
Comment
Question by:juanfigs
9 Comments
 
LVL 6

Expert Comment

by:aolXFT
ID: 9605547
You're going to have to give us way more information(and show us some code probably), before we can do anything to help.

How did it not work? What happened instead?

Did Location have any effect at all? Did you get any error messages(if so what were they)?

I understand you have a page, where they get a form, they fill it in. it is submitted to ANOTHER(different) page, The other page checks for errors, and should send it back to the first page if there are any, and if not continue on with it's jobs.

Is this understanding accurate?

PS, you will probably have to pass some params(or set cookies - params are better) to the script you are relocating them to, to pass on what errors they made.
0
 
LVL 9

Expert Comment

by:waygood
ID: 9606111
add in exit after your header(); as you dont want to process any more html or php after it.
and make sure you don't output anything prior to it either, like headers.

header("location: index.php");
exit;


e.g. Try this and use REFRESH and back and forward. You dont get the "resend form" message.

index.php
--------------------
<?php

if(sizeof($_POST)>0)
{
  // process post
  header("location: index.php?done=true");
  exit;
}
if($_GET="true")
{
   echo "done";
}
else
{
?>
<h3>my form</h3>
<form name="myform" method="post" action="index.php">
<input........
.....
</form>

<?php
}
?>
-----------------------
0
 
LVL 9

Expert Comment

by:waygood
ID: 9606165
I should read the questions better.

With mine you could validate and redo the form with the errors listed.

$error=array();
if(empty($_POST['cust_name']))
{
  $error['cust_name']="You must fill this in";

}
if( (sizeof($_POST)>0) && (sizeof($error)==0) )
{
  // process it with no errors
}
else
{
  // display form

.
.
<input name="cust_name" value="<?php echo $_POST['cust_name']; ?>" />
<?php echo $error['cust_name']; ?>
.
.

}
?>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:LornaJane
ID: 9606241
I think waygood's comment will be very helpful to you, and I would recommend that it would be more useful to return the form with the problems labelled than to open another window with this information in.  If you do want to open another window with this information in, you would be better off using javascript.  I can post an example if you still want to do it this way

Good luck!
0
 
LVL 11

Expert Comment

by:shmert
ID: 9607774
call ob_start() at the very beginning of your page, and then your call to header() will work.
0
 

Author Comment

by:juanfigs
ID: 9608180
Hi again.

The header ("Location: test.php")

that works but how about if i want to open that in a new small page not in the same one the user have the registration form.
0
 
LVL 6

Expert Comment

by:aolXFT
ID: 9610082
AFAIK if you want to open it in a new small page, you'll have to use JS.

If it was a hyperlink, you could use the target atribute, but I can't really see how to do wit with headers.
0
 
LVL 2

Accepted Solution

by:
ramonklown earned 100 total points
ID: 9614259
You have to do a post to a test page let's say testcode.php then if everything is okay (after all those ifs and stuff) you do a header to another page like

the code to post it to a new page in Javascript in full mode is

 <SCRIPT LANGUAGE="JavaScript">

function Start(page) {
OpenWin = this.open(page, "CtrlWindow", "toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes");
}
</SCRIPT>

THE BUTTON IS

<input type=button value="CLICK HERE" onclick="Start('testcode.php');">

<?
//testcode.php
if blah blah blah

else{

header ("Location: aproved.php")
}

?>

0
 
LVL 1

Expert Comment

by:pfiev
ID: 9620887
You should target your FORM in a new window, so
- if you've got errors, it will appear in your new windows
- if no error, your new windows should (that's your work) modify its opener (change its location) and close itself.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question