0

Age old question. I have tried everything I can think of included multiple searches on this site.

My example URL is mydomain.com/login.php?aircraft_id=3542

$aircraft_id = $_GET['aircraft_id'];
echo $aircraft_id;   //This works fine to verify the $_GET is working

... bunch of login code to verify user ... if password verified:

 session_start()
 $_SESSION['username'] = $username;
 header('Location: test.php?aircraft_id=' . $aircraft_id);

I can not get this variable to pass to the next page for the life of me.

I have tried

 header('Location: test.php?aircraft_id=' . $aircraft_id);

 header("Location: test.php?aircraft_id=$aircraft_id");

as well as other forms of single quote / double quotes.

The $_GET is working as I can echo the $aircraft_id on the login page but once I submit, the header will not capture the variable to pass on to the next page.

Here is the form code:

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
        <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
            <label>Username</label>
            <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
            <span class="help-block"><?php echo $username_err; ?></span>
        </div>    
        <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
            <label>Password</label>
            <input type="password" name="password" class="form-control">
            <span class="help-block"><?php echo $password_err; ?></span>
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-primary" value="Login">
        </div>
        <p>Don't have an account? <a href="register.php">Sign up now</a>.</p>
    </form>
12
  • Form submissions are usually POST not GET. Commented Aug 3, 2018 at 19:58
  • Show us the form Commented Aug 3, 2018 at 20:03
  • Do you get redirected to test.php?aircraft_id=? If so, your variable declaration is wrong, it's probably $_POST[] Commented Aug 3, 2018 at 20:04
  • Also, what does the value of $aircraft_id look like? Commented Aug 3, 2018 at 20:05
  • I have tried $_GET and $_POST. Commented Aug 3, 2018 at 20:08

1 Answer 1

1

Your issue is that $_SERVER["PHP_SELF"] gets rid of your GET parameters in your URL. There are a few ways to fix this:

Add a Hidden Field to Your Form

<input type="hidden" name="aircraft_id" value="<?=$_GET['aircraft_id'];?>"/>

Alter Your action Call

action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?aircraft_id=<?=$_GET['aircraft_id'];?>"

Remove Your action Call

You could also simply remove action="" from your form, and it should also fix the issue. Forms by default will post to the same page they are on given they have no action instruction.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes ... that was it. This is the exact fix I tried. Thank you!!
@Spence Added some alternatives. Keep in mind you should mark answers as "accepted" if they've helped you solve your problem.
Wish I could upvote ... I am new to this site. Thanks again!
@Spence You may not be able to upvote, but accepting the answer will earn us both a little bit of reputation, but only accept answers that have actually helped you. :) EDIT: Thanks!
Done! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.