How to Pass a PHP Variable Using the Url

How to pass a PHP variable using the URL

In your link.php your echo statement must be like this:

echo '<a href="pass.php?link=' . $a . '>Link 1</a>';
echo '<a href="pass.php?link=' . $b . '">Link 2</a>';

Then in your pass.php you cannot use $a because it was not initialized with your intended string value.

You can directly compare it to a string like this:

if($_GET['link'] == 'Link1')

Another way is to initialize the variable first to the same thing you did with link.php. And, a much better way is to include the $a and $b variables in a single PHP file, then include that in all pages where you are going to use those variables as Tim Cooper mention on his post. You can also include this in a session.

how to pass php variables through url

Try this:

<a href="http://localhost/profileviews/view.php?name= <?php echo $row['username']; ?> . "><?php echo $row['username']; ?></a>

Passing a PHP Variable in the URL

Edit, looking at your edits, I'd say the first section about the redirect looks like what you need

But I see you say

It is stored in a session variable.

Are you formatting a URL string from a php session and then trying to transmit get params somewhere? If so, did you try using the session API to get the username and then doing a php redirect?

 session_start();

$username = $_SESSION['myusername'];

// redirect
header("http://your/sites/dir/page.php?myusername=$username");

But it looks like you're relying on the deprecated session_register and register_globals, based on this comment:

I am wanting to pass this throughout the application, I do have
it saved as a session I think as this:
session_register("myusername");

That's probably not using because register_globals has been turned off. You should leave it off. You want to avoid session_register and instead use the $_SESSION array.

Here's how you pull a "myusername" from a URL GET query

To extract the username from a URL you want to use $_GET, ie

 $myusername = $_GET['myusername'];

The part of the URL you're looking at is known as the "query string". The parameters formatted

?var=value&var2=value2...
are known as "GET" parameters. $_POST parameters are not transmitted in the URL itself. You can read more about HTTP GET vs POST, here.

To save the username in the session do

 session_start();
// be sure to VALIDATE your inputs
$_SESSION['myusername'] = $_GET['myusername']; // replace with post if appropriate

Passing variables to another page with url - PHP

You set $_SESSION['status'] to 0 and then set it to 1, so it will always be 1. Additionally, the link you click has nothing to do with the session vars.

Page 1:

Normally to get just one variable from one page to another via hyperlink you would add it to the URL as a query parameter and access it using $_GET:

<?php
echo '<a href="page2.php?status=0">No</a>';

echo '<a href="page2.php?status=1">Yes</a>';
?>

Page 2:

You also need to check if $_GET['status'] is set:

<?php
if (isset($_GET['status']) && $_GET['status'] == 0) {
echo "No !";
}
elseif (isset($_GET['status']) && $_GET['status'] == 1) {
echo "Yes !";
}
else {
echo "NOT set !";
}
?>

Now, if you want it to be accessible on other pages without passing in the URL, then set it as a session var:

    session_start();
$_SESSION['status'] = $_GET['status'];

How to pass PHP variable to AJAX URL

Add data in your AJAX request like -

<?php $cheque = '78964Y' ?>
$(document).ready(function(){

function fetch_data()
{
$.ajax({
url:"cheque_select.php",
method:"POST",
dataType:"json",
data:{'cheque': "<?php echo $cheque; ?>"},
success:function(data)
{

var html = '';
for(var count = 0; count < data.length; count++)
{
html += '<tr>';
html += '<td><input type="checkbox" id="'+data[count].id+'" data-cheque_no="'+data[count].cheque_no+'" data-id="'+data[count].id+'" data-name="'+data[count].name+'" data-sum="'+data[count].sum+'" data-account="'+data[count].account+'" class="check_box" /></td>';
html += '<td>'+data[count].cheque_no+'</td>';
html += '<td>'+data[count].id+'</td>';
html += '<td>'+data[count].name+'</td>';
html += '<td>'+data[count].sum+'</td>';
html += '<td>'+data[count].account+'</td></tr>';
}
$('tbody').html(html);
}
});
}

fetch_data();

It will send the cheque value to the server end.

And get it at the PHP end just before the query by adding -

<?php
include 'connection.php';
$cheque = $_POST['cheque'];
$query = "SELECT * FROM entry WHERE entry.bank = ?";
$statement = $connect->prepare($query);
$statement->execute([$cheque]);
$data = $statement->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($data);

How to put a link passing variable inside php echo

Simply, put your anchor link in if condition:

<?php
if (YOUR_CONDITION_HERE) {
echo '<a href="page2.php?Id='.$Id.'">Product</a>';
}
?>

OR

<?php
if (YOUR_CONDITION_HERE) {
?>
<a href="page2.php?Id=<?php echo $Id;?>">Product</a>
<?php
}
?>

Above condition will display your link only when your condition is TRUE.

Otherwise, it will hide.



Related Topics



Leave a reply



Submit