danposs86 Posted March 21, 2007 Share Posted March 21, 2007 Hi, i am working on a forum that inputs data into a table on my database. I currently have it "echo"ing a message saying that it was successfuly submitted. Instead what i want it to do it to go to a different php page.Here's part of my code: <?phpif (!isset($_POST['submit'])) {?><form action="" method="post">Foreame: <input type="text" name="forename"><br>Surname: <input type="text" name="surname"><br><input type="submit" name="submit" value="Next Step >>"></form><?php} else {$Forename = $_POST['forename'];$Surname = $_POST['surname'];mysql_query("INSERT INTO `djpCustomer` (forename, surname) VALUES ('$Forename', '$Surname')");echo "Success! Your details has been added!";}?> I cant think of what to do at all, i know i want to replace the "echo" link with some code that, once the submit button is clicked, will take me to the next page ("step2.php"). Any suggestions? Link to comment Share on other sites More sharing options...
danposs86 Posted March 21, 2007 Author Share Posted March 21, 2007 ok found out how to do it so ignore me Link to comment Share on other sites More sharing options...
iyeru42 Posted March 22, 2007 Share Posted March 22, 2007 Just remember you can't use header() during the body of your script. Unless your body of script executes before the <!DOCTYPE (and html tags) Link to comment Share on other sites More sharing options...
justsomeguy Posted March 22, 2007 Share Posted March 22, 2007 You can use header wherever you want if you separate the PHP from the HTML. Link to comment Share on other sites More sharing options...
iyeru42 Posted March 22, 2007 Share Posted March 22, 2007 You can use header wherever you want if you separate the PHP from the HTML.IPB won't allow that though in the old templating system it used in 1.3.1 (Nor does vBulletin 2.)Putting header("blah") will output that error Header information already sent by header.tpl.php on line 1. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 22, 2007 Share Posted March 22, 2007 That just means that they didn't properly separate the PHP from the HTML. If you have the PHP and HTML totally separate, you can send headers at any point. The last step of a script like that is to load the HTML template and format it for display, so up until that point you can do whatever you want. Once the template gets sent to the browser then you can no longer do that, so the script should reserve all output for the last step, regardless of what else it needs to do. Link to comment Share on other sites More sharing options...
iyeru42 Posted March 22, 2007 Share Posted March 22, 2007 That's why when I try to edit the browser location, I use window.location instead. <?phpif (!isset($_POST['submit'])) {?><form action="" method="post">Foreame: <input type="text" name="forename"><br>Surname: <input type="text" name="surname"><br><input type="submit" name="submit" value="Next Step >>"></form><?php} else {$Forename = $_POST['forename'];$Surname = $_POST['surname'];mysql_query("INSERT INTO `djpCustomer` (forename, surname) VALUES ('$Forename', '$Surname')");echo "Success! Your details has been added!";?><script>window.location.href = 'blah.htm'; // Add setimeout if needed.</script><?php}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted March 22, 2007 Share Posted March 22, 2007 That wouldn't work if Javascript was disabled. That's why I just keep my PHP and HTML separated and use headers to redirect, or a meta refresh if necessary for cookies. Link to comment Share on other sites More sharing options...
iyeru42 Posted March 23, 2007 Share Posted March 23, 2007 Which will be a pain in the butt if you try to do a header after the header is init. Because people often times (when coming from JS) blindly put the header function where javaScript would put window.location.href (after the headers) and that doesn't work IE: <?phpfunction onClick2($locale) { if($locale !== "" || $locale !== false || $locale !== null) { header("Location: ", $locale); }}?><input type="button" onClick="<?php onClick2('blah.htm'); ?>"> Since PHP parsing puts any header information (unless specified in an include or require) before the first HTML it detects, it'll give the error:Cannot modify header information. Already outputted by blah.php on line XWhere blah.php is the code I just put down above. Link to comment Share on other sites More sharing options...
jlhaslip Posted March 23, 2007 Share Posted March 23, 2007 ob_start() Link to comment Share on other sites More sharing options...
iyeru42 Posted March 23, 2007 Share Posted March 23, 2007 ob_start()Read the fine print beforehand:This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.In this case, it wouldn't be best to use this. Since I may forget to close the OB. Link to comment Share on other sites More sharing options...
jlhaslip Posted March 23, 2007 Share Posted March 23, 2007 Since I may forget to close the OB.I'll bet you'd remember once the page came back blank. Link to comment Share on other sites More sharing options...
iyeru42 Posted March 23, 2007 Share Posted March 23, 2007 Doubtful. Because that also happens if I forget a curly bracket. Link to comment Share on other sites More sharing options...
jlhaslip Posted March 23, 2007 Share Posted March 23, 2007 the curly brace will likely give you a parse error and I think OB will dump on terminate. I'll try it and let you know how it works. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 23, 2007 Share Posted March 23, 2007 I think you're missing my point. I never redirect with Javascript, it's either a header or if I just set a cookie then it will be with a meta refresh. This is the point I'm making - if you separate the PHP from the HTML, you can redirect whenever you want. Look at this script for a user to update their information: <?phprequire_once("include/global.conf.php");auth_user();if ($page_mode == "submit"){ get_form_var("name"); get_form_var("address"); get_form_var("phone1"); get_form_var("phone2"); get_form_var("email"); get_form_var("email2"); get_form_var("fax"); get_form_var("website"); get_form_var("notes_sched"); get_form_var("password"); get_form_var("confirm_password"); #validate if (!validate_email($email)) $errorString .= "invalid_address;"; if (trim($name) == "") $errorString .= "blank_name;"; if ($errorString == "") { if (db_exists("SELECT * FROM {$DB_PREFIX}users WHERE email='" . db_escstr($email) . "' AND id!={$USER_LOGIN_ID}", __FILE__, __LINE__)) $errorString .= "email_exists"; if (db_exists("SELECT * FROM {$DB_PREFIX}client_users WHERE email='" . db_escstr($email) . "'", __FILE__, __LINE__)) $errorString .= "email_exists"; if ($password != "") { if (!validate_password($password)) $errorString .= "invalid_user_pw;"; elseif ($password != $confirm_password) $errorString .= "invalid_user_cpw;"; } if ($errorString == "") { if ($password != "") { $pw = sha1($password); $now = timest(); $salt = md5($email . $now); $db_pw = create_db_hash($pw, $salt); } $sql = "UPDATE {$DB_PREFIX}users SET " ."name='" . db_escstr($name) . "', address='" . db_escstr($address) . "', phone1='" . db_escstr($phone1) . "', phone2='" . db_escstr($phone2) . "', email='" . db_escstr($email) . "', email2='" . db_escstr($email2) . "', fax='" . db_escstr($fax) . "', website='" . db_escstr($website) . "', notes_sched='" . db_escstr($notes_sched) . ($password != "" ? "', password='{$db_pw}', salt='{$salt}" : "") . "'" ." WHERE id={$USER_LOGIN_ID}"; db_query($sql); } }}$page_name = "My Information";require_once("include/template.php");$page = new Page();$content = array();$content['sys_name'] = $SYSTEM_NAME;$content['sys_path'] = $HTTP_SYSTEM_PATH;$content['page_title'] = $SYSTEM_NAME . ($page_name != "" ? ": $page_name" : "");$content['error_text'] = parse_errors();$content['selected_menu_item'] = 'userinfo';#menu permissions$perms = get_menu_permissions();foreach ($perms as $key => $val){ $content[$key] = true;}$user = db_get("SELECT * FROM {$DB_PREFIX}users WHERE id={$USER_LOGIN_ID}", __FILE__, __LINE__);foreach ($user as $key => $val){ $content[$key] = ($val == "" ? " " : $val); //convert empty to nbsp}$result = db_query("SELECT * FROM pub_resumes WHERE name='{$user['name']}'", __FILE__, __LINE__);if (db_num_rows($result) > 0){ $resume = db_fetch_assoc($result); $content['has_resume'] = true; $content['resume_id'] = $resume['id'];}$page->set_template("edit_my_info.tpl");$page->process($content);$page->output();?> I can send whatever headers I want on literally any line up until the last line that outputs all of the HTML. It's not buffering, it is separation of presentation and logic. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now