Forum Moderators: coopster

Message Too Old, No Replies

Redirect after INSERT

         

asprinwizard

5:29 pm on Aug 22, 2007 (gmt 0)

10+ Year Member



Hi there,

I have taken old Dreamweaver INSERT record code and turned it into a generic function. Basically another function generates the SQL and this one inserts it.

The INSERT works great but I then want the page to redirect back to the index.php page and this is not working at all. Does anyone know the best way to do this. The code is below:

The form action is below:

<form action="<?php insertSQL($c13_db, $add_competition);?>" method="POST">

This is the insert function:

function insertSQL($_db, $sql)
{
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["form_submit"])) && ($_POST["form_submit"] == 1)) {
$sql = get_sql($sql);
$result = mysql_query($sql, $_db) or die(mysql_error());

$insertGoTo = "index.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?'))? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
}

The header function is left over from the old Dreamweaver code and I cannot get it to work now it's within a function. Can anyone please help me out.

d40sithui

5:41 pm on Aug 22, 2007 (gmt 0)

10+ Year Member



do you get any errors?
also, remember header() needs to be called before any actual output, so if you have any output before header, it won't work.

asprinwizard

6:14 pm on Aug 22, 2007 (gmt 0)

10+ Year Member



No, I get no errors. Instead the form goes blank and and the SQL is inserted as I require.

I also have an update function which does similar but the form returns back to how it was before any edits were made. E.g. If I changed an entry field from 'hello' to 'hello again' once submitted the form returns to 'hello' but the entry in the database is updated correctly.

Not sure why this is but i guess it must be something to do with cache. Anyhow the main problem is that the page will not redirect. I've even tried adding a javascript redirect (window.location...) to the submit button but this has no effect. The page still stays on the form.

Can someone look into this for me. I can't believe it's such a hard thing to achieve.

Many thanks.

d40sithui

6:31 pm on Aug 22, 2007 (gmt 0)

10+ Year Member



your error display might be off. add this to your function to see if it'll run it.

echo "hello from insertSQL<br>";

asprinwizard

9:18 pm on Aug 22, 2007 (gmt 0)

10+ Year Member



No, I've put error display back on. I get warnings about missing arguments for functions and undefined index's but no errors at all once the form is submitted.

It's very strange and annoying. I did add the echo statemeent you posted but this does not seem to appear when the form is submitted. Also I have tried to return variables from the function without success.

Initially I tried getting it to return a variable as true and then writing a conditional in the main page which used the header function to go back to the index if that variable was set as true. This did not work. Neither did the javascript redirect. It seems determined to return me to the same page no matter what do.

The only way it will go to another page is if I set the form action to another page, but then where do I put the insertSQL function. I really need it to execute the insert, then go back to the index.

So can someone show me where I'm going wrong with the code.

MaximumPig

12:27 am on Aug 23, 2007 (gmt 0)

10+ Year Member



Technically you need to make sure that no code or data or stray newlines, carriage returns or spaces get sent to the browser because that will force the server to send headers which will make your header redirect ineffective. However I have found that sometimes I have even a perfectly clean script and it will not redirect for some inexplicable reason. The way to deal with this is to wrap all the code up to the header statement in ob_start(); and ob_flush(); statements as follows:
_____________________________
ob_start();
error_reporting(E_ALL); // debugging
ini_set('display_errors','On'); // debugging
require_once('includes/error.php');
require_once('includes/config.php');
require_once('includes/function.php');
$data = $_REQUEST;
arraydumper($_REQUEST);
$timebits = explode("/",$_REQUEST['publish_date']);
$unixified = mktime(0,0,0,$timebits[0],$timebits,$timebits[2]);
arraydumper($unixified);
session_write_close();

header("Location: http://example.com/index.php?page_id=xx&content_id=yy);

ob_flush();
_________________________________

This page is a work-in-progress that I am about to add the actual MySQL insert functions to but you get the idea.
(arraydumper is a function i wrote to ... dump the contents of an array)

[1][edited by: eelixduppy at 1:07 am (utc) on Aug. 23, 2007]
[edit reason] delinked code [/edit]

jatar_k

3:15 am on Aug 23, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld MaximumPig

MaximumPig

3:28 am on Aug 24, 2007 (gmt 0)

10+ Year Member



Thanks.

asprinwizard

11:11 am on Aug 30, 2007 (gmt 0)

10+ Year Member



Hello Again,

Thanks for you replies. I've made some changes. I took the redirect out of the function and added the following code to the module itself.

if ($_POST['form_submit']==1) {
insertSQL($c13_db, $add_competition);
header('Location: index.php');
}

This does not seem to work either. I have tried placing the code at various points and it does work if placed before the <head> tags but not after. But because the code is made up of various modules I cannot place the code before the head tag very easily so I tried using Javascript:

if ($_POST['form_submit']==1) {
insertSQL($c13_db, $add_competition);
echo '<script language="JavaScript">window.location="index.php";</script>';
}

This works and I don't mind doing it this way if necessary. However I'd prefer a PHP solution if possible so can someone please advise if it's possible. The page layout is below:

1) PHP Code - Declaring functions, variables, db setup etc.
2)Start of HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $_local['page_title'];?></title>
<link href="/styles/main.css" rel="stylesheet" type="text/css" />
</head>

<body>
3) Page module including the if statement above
4) Page End

d40sithui

12:09 pm on Aug 30, 2007 (gmt 0)

10+ Year Member



you need to take off ALL HTML tags above the code. Header will not work if there are output before it is called, which is what you are doing.so take off everything starting with <body> and up. you should ahve something like this in the end

<?
//if statement

?>