user2965726
user2965726

Reputation:

passing php variable to html

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

Upvotes: 0

Views: 123

Answers (3)

Arbiter
Arbiter

Reputation: 504

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

Upvotes: 1

Charaf JRA
Charaf JRA

Reputation: 8334

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

Upvotes: 0

Tim Zimmermann
Tim Zimmermann

Reputation: 6420

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

$user = $_GET['username'];

Upvotes: 0

Related Questions