Page 1 of 1

php redirect back to form page

Posted: Thu Feb 18, 2016 6:50 pm
by karyoker
Want to redirect back to the form page after successful entry. The redirect method does not work all the time (part is commented out) I want echo below the form Successful entry.

//These are the variables for our redirect.
$redirect=$_POST['redirect_to'];
$referred=$_SERVER['HTTP_REFERER'];
$query=parse_url($referred,PHP_URL_QUERY);
$referred=str_replace(array('?',$query), '', $referred); echo "referred", $referred;

full code

Code: Select all


<?php
require_once ('core/connect.php');
$mysqli = mysqli_connect($dbhost, $dbuser, $dbpass) or die($dberror1);
$select_db = msqli_select_db($msqli, 'dustysvi_oregon2') or die ($dberror2);

$fields = mysql_query("SHOW columns FROM dustysvi_oregon2");echo $fields;
while($row = mysql_fetch_array($fields))
{
    echo '<tr>';
    echo '<td>';
    echo $row["Field"];
    echo '</td>';
    echo '<td>';
    echo "<input type='text' class='' name='tags' id='tags' value=''>";
    echo '</td>';
    echo '</tr>';
}
if (isset($_SERVER['HTTP_REFERER'])) { // check there was a referrer

	$uri = parse_url($_SERVER['HTTP_REFERER']); // use the parse_url() function to create an array containing information about the domain
	Echo $uri['host']; // echo the host

}
$domain = $_SERVER['HTTP_HOST'];
$uri = parse_url($_SERVER['HTTP_REFERER']);
$r_domain = $uri['host'];

echo "uri", $uri, $domain;
 /*Checks to see if the specified field exists and if it doesn't, creates it.;*/
function f_fieldExists($table, $column, $column_attr = "VARCHAR( 30 ) NULL" , $mysqli) {
	$exists = false;
	  $columns = mysqli_query($mysqli,"show columns from $table");  
	while($row = mysql_fetch_assoc($columns))
{
    foreach($row as $key => $value){
        echo "$key=$value";
    }
		if($c['Field'] == $column){
			$exists = true;
			break;
		}
	}
	if(!$exists){
		mysqli_query($mysqli, "ALTER TABLE `$table` ADD `$column`  $column_attr ");
	}
}
	
	

//if ( $domain == $r_domain ) {
	

	
	
    //These are the main variables we'll use to process the form.
    $table=$_POST['county'];  print_r($_POST); echo "<br \n>";
	
	unset($_POST['submit']);
	
unset($_POST['county']);

print_r($_POST);
    $keys=implode(", ",(array_keys($_POST))); echo "<br \n>";  echo "keys =", $keys;
    $values=implode("', '",(array_values($_POST)));  echo "<br \n>";  echo "values =", $values;

 /*Check to see if the fields specified in the form exist and if they don't, create them.*/
    foreach ($_POST as $key => $value) {
        $column = mysqli_real_escape_string($mysqli,$key);
        $alter = f_fieldExists($table, $column, $column_attr = "VARCHAR( 30 ) NULL", $mysqli );

        if (!$alter) {
			echo "<br \n>";
			
            echo 'Unable to add column: ' . $column;
        }
    }

 //These are the variables for our redirect.
    $redirect=$_POST['redirect_to'];
    $referred=$_SERVER['HTTP_REFERER'];
    $query=parse_url($referred,PHP_URL_QUERY);
    $referred=str_replace(array('?',$query), '', $referred); echo "referred", $referred;


	
    /*Insert out values into the database.*/
    $sql="INSERT INTO $table ($keys) VALUES ('$values')";echo "<br \n>"; Echo $sql;

    if (!mysqli_query($mysqli,$sql)) {
        die('Error: ' . mysqli_error($mysqli));
    }


$mysqli->close();




 /*Redirect the user after a successful form submission*/
   
        header("Location: $referred?msg=1");
   
//} 
//else {
    die('You are not allowed to submit data to this form');
//}

?> 

Re: php redirect back to form page

Posted: Fri Feb 19, 2016 1:23 am
by Christopher
I suspect that your redirect does not work because there are echo() statements sending output before your header() statement. You cannot have any output before calling header().