Jordan Vit Jordan Vit - 2 years ago 64
PHP Question

Simple redirect to previous page doesn't work php

I have page

and button on this page
. Now I'm trying when I go to
and after I add new value to redirect me back
. I've made it like this

if ( !empty($_POST) && isset($_POST['add'] ))
$field_name = $_POST['field_name'];

$sql = "INSERT INTO form_fields ( field_name, group_id ) VALUE ( ?, ? )";
$q = $pdo->prepare($sql);
$q->execute(array($field_name, $group_id));

header('Location: ' . $_SERVER['HTTP_REFERER']);

<form class="form-horizontal" role="form" action="" method="post">
<div class="form-group">
<label class="control-label col-sm-2" for="field_name">Name:</label>
<div class="col-sm-10">
<input type="username" class="form-control" name="field_name" id="field_name">

<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<input type="submit" name="add" value="Add Dropdown option" class="btn btn-primary btn-block">

So simple form with one field and I added this for redirection but after form submit it doesn't redirect

header('Location: ' . $_SERVER['HTTP_REFERER']);

Answer Source

According to what you posted in your comments, you don't need to use the referer, you have to hardcode the URL drops.php and append the group id.

The referer is the page from which you post comes from, and in this case, it's not the same page that you want to go back.

header('Location: drops.php?group_id=' . $group_id);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download