S.I.
S.I.

Reputation: 3375

Simple redirect to previous page doesn't work php

I have page drops.php and button on this page addDrops.php?group_id=6. Now I'm trying when I go to addDrops.php and after I add new value to redirect me back addDrops.php?group_id=6. 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>
    </div>                  
    <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">
            </div>
    </div>
</form>

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']);

Upvotes: 0

Views: 40

Answers (1)

SanF
SanF

Reputation: 186

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);

Upvotes: 2

Related Questions