Forum Moderators: coopster
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.
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.
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.
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]
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