|
-
Nov 18th, 2004, 03:16 PM
#1
Thread Starter
Ex-Super Mod'rater
Redirecting but also... -[RESOLVED]-
I have a script which a form submits to then that script checks the contents and does the neccasary actions. But if the form wasn't fully complete or there was a problem i'm using
PHP Code:
header("Location: /book1.php");
to send the user back to the form. The problem is I also want to send a chunk of text with it (via POST method) is there anything I can set in the headers that will allow me to do that?
If i can't then I guess I'll have to limit he amount of messages possible and have some kind of code just sending a number related to what message to display .
Last edited by Electroman; Nov 18th, 2004 at 05:30 PM.
When your thread has been resolved please edit the original post in the thread (  )
and amend "-[RESOLVED]-" to the end of the title and change the icon to  , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

-
Nov 18th, 2004, 05:10 PM
#2
You want to redirect the browser so that it sends POST data along? Sorry, not possible. But you can append GET variables to the URL.
Oh, and the Location header has to be absolute. You need to build the absolute URL yourself, though.
Code:
function get_scheme() {
if(isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
$scheme = 'https';
} else {
$scheme = 'http';
}
return $scheme;
}
function get_base_url() {
return $this->get_scheme().'://'.$_SERVER['HTTP_HOST'];
}
function redirect($rel) {
$this->log("Redirecting to $rel.");
// These redirects are for display after processing-only pages.
// So make sure HTTP/1.1 (and higher) clients get a 303.
$prt = $_SERVER['SERVER_PROTOCOL'];
$http_minor = intval(substr($prt, 6));
if($http_minor >= 1) {
header("$prt 303 See Also");
}
header('Location: '.$this->get_base_url().
dirname($_SERVER['PHP_SELF']).$rel);
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 18th, 2004, 05:30 PM
#3
Thread Starter
Ex-Super Mod'rater
I thought the chances would be slim, I'll just stick with sending a few numbers by GET instead. As to the relative links thanx but I'd actually done that in my code and forgot while writting the Q, I have a variable for holding the domain but removed it for the Q and forgot to replace it.
When your thread has been resolved please edit the original post in the thread (  )
and amend "-[RESOLVED]-" to the end of the title and change the icon to  , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

-
Nov 18th, 2004, 06:16 PM
#4
This is a good reason why you shouldn't use redirects for forms which have not been filled out correctly. The page which generates the HTML form should be the same page which submits it.
That way you can put the incorrect information back into the form and provide them with user friendly messages. Take a look at this:
PHP Code:
<?php ob_start(); ?>
<html>
<head>
<title>Form Demo</title>
</head>
<?php
if (isset($_POST['submit'], $_POST['name'], $_POST['desc'])) {
/* this flag is used to determine whether or not we re display the form */
$data['valid'] = true;
/* prepare input for database */
if (! get_magic_quotes_gpc()) {
$data['name']['data'] = addslashes($_POST['name']);
$data['desc']['data'] = addslashes($_POST['desc']);
} else {
$data['name']['data'] = $_POST['name'];
$data['desc']['data'] = $_POST['desc'];
}
if (stripslashes($data['name']['data']) == '') { /* check name is supplied */
$data['name']['err'] = 'You cannot leave this field blank.';
$data['valid'] = false;
} else if (strlen(stripslashes($data['name']['data'])) > 20) { /* check name is not too long */
$data['name']['err'] = 'Your name cannot be longer than 20 characters.';
$data['valid'] = false;
}
if (strlen(stripslashes($data['desc']['data'])) > 500) { /* check description is not too long */
$data['desc']['err'] = 'Your name cannot be longer than 500 characters.';
$data['valid'] = false;
}
if ($data['valid']) { /* if form data was all OK do database stuff and redirect */
/* do database stuff here */
echo('<h3>you have been successfully processed.</h3><p>You will be redirected shortly.</p>');
header('Refresh: 3; url=http://www.vbforums.com/');
} else {
/* re display the form */
display_form($data);
}
} else {
/* nothing submitted - display empty form */
display_form();
}
function display_form($data = null)
{
?>
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
<p>
<?php if (!is_null($data) && isset($data['name']['err'])): ?>
<p style="font-weight: bold; color: red;"><?php echo($data['name']['err']) ?></p>
<?php endif; ?>
Enter Your Name: <input type="text"
value="<?php if (! is_null($data)) echo_html_strip($data['name']['data']); ?>"
name="name" /><br />
<?php if (!is_null($data) && isset($data['desc']['err'])): ?>
<p style="font-weight: bold; color: red;"><?php echo(htmlspecialchars($data['desc']['err'])) ?></p >
<?php endif; ?>
Enter a description: <input type="text"
value="<?php if (! is_null($data)) echo_html_strip($data['desc']['data']); ?>"
name="desc" />
</p>
<p><input type="submit" name="submit" /></p>
<?php
}
?>
</body>
</html>
<?php
function echo_html_strip($text)
{
echo(htmlspecialchars(stripslashes($text)));
}
?>
You can see how it works here: http://adam.codedv.com/form.php
In my opinion its a lot cleaner doing it this way. It centralises everthing.
-
Nov 19th, 2004, 02:35 AM
#5
It centralises everthing.
It also makes your source files a mess. At least that was my experience with one project. For the next I used separate files.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 19th, 2004, 03:07 AM
#6
Originally posted by CornedBee
It also makes your source files a mess.
Well there is nothing stopping you from using separte files and including them where needed, which is what I do.
Could shorten the previous file to this:
PHP Code:
<?php ob_start(); ?>
<html>
<head>
<title>Form Demo</title>
</head>
<?php
include('form_html.php');
if (isset($_POST['submit'], $_POST['name'], $_POST['desc'])) {
include('form_validate.php');
} else {
/* nothing submitted - display empty form */
display_form();
}
?>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|