Click to See Complete Forum and Search --> : Redirecting but also... -[RESOLVED]-
Electroman
Nov 18th, 2004, 02:16 PM
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 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 :(.
CornedBee
Nov 18th, 2004, 04:10 PM
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.
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);
}
Electroman
Nov 18th, 2004, 04:30 PM
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. :)
visualAd
Nov 18th, 2004, 05:16 PM
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 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. ;)
CornedBee
Nov 19th, 2004, 01:35 AM
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.
visualAd
Nov 19th, 2004, 02:07 AM
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 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();
}
?>
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.