Howdy neighbors!
So I have this form, right. The form is being handled on the same page using
[php]
$_SERVER['REQUEST_METHOD']
[/php]
The very general and basic gist of the script is this:
[php]
include(header.php);
if (isset($inputError))
{
echo("Input error")
}
else
{
$stmt = $db->prepare("INSERT INTO accounts (fName, lName, email, password) VALUES (:fName, :lName, :email, :password)");
$stmt->bindParam(':fName', $fName, PDO::PARAM_STR, 50);
$stmt->bindParam(':lName', $lName, PDO::PARAM_STR, 50);
$stmt->bindParam(':email', $email, PDO::PARAM_STR, 50);
$stmt->bindParam(':password', $encPassword, PDO::PARAM_STR, 50);
if ($stmt->execute())
{
// redirect to new page here //
}
}
include(footer.php);
[/php]
I validate the inputs, if there's an error, it's assigned to the $inputError array. Obviously the details of outputting the errors have been left out, but what I'm concerned with is how to redirect this page to a new page basically in the middle of the script.
If it's possible, please do tell.
Any help would be appreciated to the ultimate.
Thanks!
You should be handling all logic before outputting to the browser.
Why do that when you can use Javascript?
[QUOTE=compwhizii;24576225]You should be handling all logic before outputting to the browser.[/QUOTE]
I don't really know what that means.
Something along the lines of...
[php]
$error = isset($inputError);
if( !$error )
{
$stmt = $db->prepare("INSERT INTO accounts (fName, lName, email, password) VALUES (:fName, :lName, :email, :password)");
$stmt->bindParam(':fName', $fName, PDO::PARAM_STR, 50);
$stmt->bindParam(':lName', $lName, PDO::PARAM_STR, 50);
$stmt->bindParam(':email', $email, PDO::PARAM_STR, 50);
$stmt->bindParam(':password', $encPassword, PDO::PARAM_STR, 50);
if ($stmt->execute())
{
header('Location: ./success.php');
exit();
}
}
include(header.php);
echo("Input error");
include(footer.php);
[/php]
...So you're not outputting anything to the browser before you're definitely sure you aren't going to need to do a redirect.
So just rearrange the code essentially.
Alright I got it working. Thanks deadbeef!
[QUOTE=Poo Monst3r;24576409]I don't really know what that means.[/QUOTE]
Basically, never send any 'content'/output to the browser until all the 'working out'/logic is done.
So, if you get data from a database, and fuck around with it, do that before displaying it. Keep the 'view' separate :)
If you're clever though and know you aren't going to be altering your headers you can dump all the static content to the client then flush() the cache, before working on the db access and CPU intensive stuff, then flush that out once it's finished.
Or set up a seperate thread for all the db crap to run in while your render the rest of the page, flush it, then recover the thread, flush its data and finish the request.
Or 'do a facebook' and send all the layout and static content first, then load the database intensive stuff via ajax.
[QUOTE=DEADBEEF;24578332]Or set up a seperate thread for all the db crap to run in while your render the rest of the page, flush it, then recover the thread, flush its data and finish the request.[/QUOTE]
This [i]is[/i] PHP we're talking about...
PHP has threads... [url]http://blog.motane.lu/2009/01/02/multithreading-in-php/[/url]
It's mainly useful if you have bunch of pages to get via cURL or APIs to call, you can do them Asynchronously whilst other stuff is happening.
It's also handy if you want to pass some info to the client (usually at least the HEAD contents, and maybe the HTML of any sidebars etc), while you're still figuring out complex DB lookups on the serverside.
That way the client has chance to download css/js/images while the server is still working on the content to be displayed. Then as soon as the content is ready it can be sent to the client who's already downloaded all supporting assets and is just waiting on the data.
[QUOTE=DEADBEEF;24579066]PHP has threads... [url]http://blog.motane.lu/2009/01/02/multithreading-in-php/[/url][/QUOTE]
holy shit :ohdear:
What you really want is a page life cycle. There are frameworks out there that provide this support for you, or you can hand write your own page model.
Basically you want your page to go through various stages, such as a 'Prepare' stage, a 'Render' stage, and a 'Post Render' stage at a very basic level.
For example you could do your business/decision logic (such as deciding to redirect to a different page) in the Prepare stage of the page cycle (before you ever write anything to the browser) and then if necessary perform your writing in the Render stage.
[CODE]exit('<META HTTP-EQUIV="Refresh" Content="0; URL=YOUR-URL-HERE">');[/CODE]
or you can add php into it.
[CODE]exit('<META HTTP-EQUIV="Refresh" Content="0; URL=http://www.example.com/index.php?id='.$_GET['id'].'&error=1">');[/CODE]
which would redirect to [url]http://www.example.com/index.php?id=6428&error=1[/url]
that redirects fine after a header. I use it on FastDL
[QUOTE=Wobbier;24617675][CODE]exit('<META HTTP-EQUIV="Refresh" Content="0; URL=YOUR-URL-HERE">');[/CODE]
or you can add php into it.
[CODE]exit('<META HTTP-EQUIV="Refresh" Content="0; URL=index.php?id='.$_GET['id'].'&error=1">');[/CODE]
which would redirect to [url]http://www.example.com/index.php?id=6428&error=1[/url]
that redirects fine after a header. I use it on FastDL[/QUOTE]
You don't need to exit, you can just echo.
Anyway,
[QUOTE=compwhizii;24576225]You should be handling all logic before outputting to the browser.[/QUOTE]
[QUOTE=nivek;24618539]You don't need to exit, you can just echo.
Anyway,[/QUOTE]
exit will kill the page then redirect. Preventing any unnecessary execution of code. There is a delay when using that.
[QUOTE=Wobbier;24617675][CODE]exit('<META HTTP-EQUIV="Refresh" Content="0; URL=YOUR-URL-HERE">');[/CODE]
or you can add php into it.
[CODE]exit('<META HTTP-EQUIV="Refresh" Content="0; URL=index.php?id='.$_GET['id'].'&error=1">');[/CODE]
which would redirect to [url]http://www.example.com/index.php?id=6428&error=1[/url]
that redirects fine after a header. I use it on FastDL[/QUOTE]
no
[QUOTE=Wobbier;24617675][CODE]exit('<META HTTP-EQUIV="Refresh" Content="0; URL=YOUR-URL-HERE">');[/CODE]
or you can add php into it.
[CODE]exit('<META HTTP-EQUIV="Refresh" Content="0; URL=index.php?id='.$_GET['id'].'&error=1">');[/CODE]
which would redirect to [url]http://www.example.com/index.php?id=6428&error=1[/url]
that redirects fine after a header. I use it on FastDL[/QUOTE]
Wouldn't just a simple[code]header("Location: xyz");[/code]work?
[editline]05:32PM[/editline]
After header, right.
Rate box.
[QUOTE=Qombat;24635039]Wouldn't just a simple[code]header("Location: xyz");[/code]work?
[editline]05:32PM[/editline]
After header, right.
Rate box.[/QUOTE]
gg
Sorry, you need to Log In to post a reply to this thread.