0

Raising a three years old question. How to get a user id from one php file and retrieve it in another php file

From my own experience I tried to get an id from one file to execute it in another to retrieve some data from another website. Whenever the page loads I could see the value in url on my browser but the value is returning empty when I tried to use it. Kindly check below what I have tried and suggest if there is a way I could do it with javascript or jquery or just do it with only PHP.

a.php

<?php
    
$aa = b.php;

$bar = 1234;

echo “<a href=$aa?$bar target=new window>”Click here”</a>”;
 
?>

b.php

<?php

$foo = $_GET[$bar];

// this one returns empty. But bar is seen on the url as ?bar
echo $foo;
?>
3
  • 1
    You've forgot to add the bar variable name in url: echo "<a href=\"$aa?bar=$bar\" target=\"new window\">Click here</a>"; and always enclose tag atributes into double quotes (escape them when needed) Commented Jun 5, 2020 at 17:26
  • I didn't know having it inside a variable again was important. Commented Jun 5, 2020 at 18:04
  • 1
    Yes, it's important, other way you'd had a "variable with no value" (1) or "value assigned to no variable" (2) in your URL. 1) Is the way the browser will parse the URL, 2) Maybe is the user/programmer point of view Commented Jun 5, 2020 at 18:18

2 Answers 2

1

How u echo the anchor tag doesn't seems good to me, maybe more like:

echo '<a href="'.$aa.'?bar='.$bar.'" target="new window">Click here</a>';

You can access the value like:

$_GET['bar']

And you have to echo $foo and not $bar.

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

4 Comments

Why is it better to concatenate instead of parsing variables using double quotes?
I've always done like this, and I think in html there's must be "" in attributes, what you can't do if the whole string in double quotes.
Yes, you can do it just by escaping double quotes: echo "<a href=\"$aa?bar=$bar\" target=\"new window\">Click here</a>"; I like to parse variables instead of concatenation, but who knows wich method is better?
Yes you are right. I'm used to concatenation, seems cleaner for me, but it's a matter of taste :)
1

Check the comments added... This is how it's done...

<?php
//a.php

$b_php = "b.php"; //file name
$parm = "?bar="; //parameter
$parm_data = "1234"; //DATA
echo "<a href='$b_php$parm$parm_data' target='_blank'>Click here</a>";
?>

//b.php

<?php 
if(isset($_GET["bar"])){ //check if the variable parameter is set or not
$foo = $_GET["bar"]; // $_GET["$bar"] only works if variable exists in the file
echo $foo; //echo $foo
}else{
echo "Empty!";
}
?>

It's returning empty because you are trying to echo Variable $bar but it doesn't exist. Also look in to GET & POST forms for data from HTML or JS or Jquery to PHP.

1 Comment

Nice answer, it'd be much better with indentation and, maybe, 1) Separate codes for a.php and b.php 2) adding and option and explanation to get the variable using ternary operator.

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.