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

3
  • 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. Commented Dec 17, 2013 at 23:10
  • a file name of "sign up.html" with a space in it is going to give you all sorts of problems... Commented Dec 17, 2013 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 (") Commented Dec 17, 2013 at 23:29

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

$user = $_GET['username'];

Comments

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

Comments

Your Answer

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