0

I am having a problem with passing variables. This is the code that I think should pass the $user variable to new_page.html:

if (mysqli_query($con,$newUser))
{
    $user = $_GET[username];
    header('Location: new_page.html?currentUser=$user');
}
else
{
    header('Location: sign up.html');
}

And inside the html page I try to create a link to a new page with the user variable (which has been passed in) as the text property:

<a href = "user_page.php"> <?php echo $currentUser ?><a/>

Can anyone see what I am doing wrong?

Thanks

  • 2
    `new_page.html` needs to be a PHP file too, or you'll have to use JavaScript in `new_page.html` to dynamtically set the `href=""` attribute. – Dai Dec 17 '13 at 23:10
  • a file name of "sign up.html" with a space in it is going to give you all sorts of problems... – DrCord Dec 17 '13 at 23:29
  • If you're going to use variables in strings (such as your header line), and want the actual value rather than the name of the variable, use double quotes (") – Mark Baker Dec 17 '13 at 23:29

3 Answers3

1

You can't process PHP in a html file. You can process HTML in a PHP file - so always use the .php extension.

I think username is meant to be a post? So:

$username = $_POST['username'];
header('Location: page.php?user='.$username);

then in page.php you can use the following to collect that variable from the URL:

$username = $_GET['user']; 

An important note: Note the use of concatenation to add a variable into the PHP Header function:

Instead of:

header('Location: new_page.html?currentUser=$user');

use concatenation:

header('Location: new_page.html?currentUser='.$user);

if you need more variables:

header('Location: new_page.html?currentUser='.$user.'&anothervar='.$anotherVar);
Arbiter
  • 456
  • 1
  • 8
  • 20
0

There's a problem where you assign $user, too, the quotes are missing:

$user = $_GET['username'];
Tim Zimmermann
  • 5,222
  • 3
  • 26
  • 35
0

Change new_page.html to new_page.php and then :

Replace this line :

 <a href = "user_page.php"> <?php echo $currentUser ?><a/>

By :

 <a href = "user_page.php"> <?php echo $_GET['currentUser'] ?><a/>

An other thing , when you access this variable , it's value will be $user as string,to get it's real value , change quotes to double quotes:

header("Location: new_page.html?currentUser=$user");

See :

  1. POST and GET methods
  2. PHP in HTML file
Community
  • 1
  • 1
Charaf JRA
  • 7,659
  • 1
  • 27
  • 41