How to pass a PHP variable using the URL - php

I want to pass some PHP variables using the URL.
I tried the following code:
link.php
<html>
<body>
<?php
$a='Link1';
$b='Link2';
echo 'Link 1';
echo '<br/>';
echo 'Link 2';
?></body></html>`</pre></code>
pass.php
<pre><code>`<html>
<body>
<?php
if ($_GET['link']==$a)
{
echo "Link 1 Clicked";
} else {
echo "Link 2 Clicked";
}
?></body></html>
Upon clicking the links Link1 and Link2, I get "Link 2 clicked". Why?

In your link.php your echo statement must be like this:
echo '<a href="pass.php?link=' . $a . '>Link 1</a>';
echo 'Link 2';
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.

You're passing link=$a and link=$b in the hrefs for A and B, respectively. They are treated as strings, not variables. The following should fix that for you:
echo 'Link 1';
// and
echo 'Link 2';
The value of $a also isn't included on pass.php. I would suggest making a common variable file and include it on all necessary pages.

I found this solution in "Super useful bits of PHP, Form and JavaScript code" at Skytopia.
Inside "page1.php" or "page1.html":
// Send the variables myNumber=1 and myFruit="orange" to the new PHP page...
Send variables via URL!
//or as I needed it.
<a href='page2c.php?myNumber={$row[0]}&myFruit={$row[1]}'>Send variables</a>
Inside "page2c.php":
<?php
// Retrieve the URL variables (using PHP).
$num = $_GET['myNumber'];
$fruit = $_GET['myFruit'];
echo "Number: ".$num." Fruit: ".$fruit;
?>

All the above answers are correct, but I noticed something very important. Leaving a space between the variable and the equal sign might result in a problem. For example, (?variablename =value)

Use this easy method
$a='Link1';
$b='Link2';
echo "Link 1";
echo '<br/>';
echo "Link 2";

You can do this like this:
<a href="./gemsPack.php?uid=<?php echo $id; ?>&amount=1.99">

just put
$a='Link1';
$b='Link2';
in your pass.php and you will get your answer and do a double quotation in your link.php:
echo 'Link 1';

Related

Php and Html code within echo

I'm writing an if statement in which a button needs to show if the cart is empty.
For this button I need to get the form key of the product for the data-url
So something like this:
Order
As mentioned above I need to wrap this button in an if statement, so something like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()){
echo 'Order';
}
else{
'<p>hello</p>';
}
?>
But obviously I can't have php echo within echo. Can anybody point me in the right direction of how to do this?
You don't put PHP inside HTML inside PHP. Since you're already in the context of PHP code, just concatenate the values you want to the output:
echo 'Order';
The resulting output is always just a string. You can simply build that string with whatever values you have.
You can just use string concatenation:
echo '<a href="#" data-url=".../' . Mage::getSingleton(...) . '"' ...
Simply don't open PHP up again. You can terminate the HTML interpretation inside an echo.
Your code should look like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()) {
echo 'Order';
}
else {
'<p>hello</p>';
}
?>

Run PHP code stored in a database

database has php value saved like that as string
<?php $s = 'my name is'; ?>
and what am trying to do is calling the value as php code and want when type echo $s; to print my name is but now it's given my empty value see the code below to get what i mean
<?php
$b = '<?php $s = 'my name is';?>';
echo $b; //empty value
?>
Sounds like you're talking about eval() - but I'd be wary of using it. If you do, be extremely careful.
"If eval() is the answer, you're almost certainly asking the wrong question." -Rasmus Lerdorf
You'd probably need to strip the <?php and ?> tags, and watch for double quotes surrounding variables you don't want to replace:
$s=0;
eval('$s = "my name is";');
echo $s;
PHP is not recursively embeddable:
$b = '<?php $s = 'my name is';?>';
That's not PHP code in there. it's some text with the letters <, ?, p, etc...
And you ARE getting output. But you're viewing it in a browser, so the <?php ... ?> gets rendered as an unknown/illegal html tag and simply not displayed. If you'd bothered doing even the most basic of debugging, e.g. "view source", you'd have seen your PHP "code" there.

Incorporating php variable into url with href attribute

I am trying to create the necessary url from this code however it is working and I am struggling to find out why.
$linkere = $row['message'];
echo '<a href="me.php?message=<?php echo rawurlencode($linkere); ?>">'
Currently this code is producing the url: me.php?message= . But, I would like it to create the url: me.php?message=hello for example.
Thanks for helping!
You are passing $linkere to rawurlencode(). The variable is actually named $linker.
$linker = $row['message'];
echo '<a href="me.php?message=<?php echo rawurlencode($linker); ?>">'
You have alot of syntax problems here.
first, you need to use Concatenation message='.rawurlencode($linker).'"
second your variable do not exist, it should be $linker.
Second close the tag and insert the text, in this case i used Test.
$linker = $row['message'];
echo 'Test';
Can you try this,
$linker = $row['message'];
echo 'YOUR LINK TEXT HERE';
You don't need the <? ?> and echo in your echo, it should just be:
$linkere = $row['message'];
echo 'Test';
Otherwise you are turning php on and off again to echo something within an already open instance of php in which you are already echoing.

Add PHP variable inside echo statement as href link address?

I'm trying to use a PHP variable to add a href value for a link in an echo statement.
Here's a simplified version of the code I want to use. I know that I can't just add the variable into the echo statement, but I can't seem to find an example anywhere that works.
$link_address = '#';
echo 'Link';
Try like
HTML in PHP :
echo "<a href='".$link_address."'>Link</a>";
Or even you can try like
echo "<a href='$link_address'>Link</a>";
Or you can use PHP in HTML like
PHP in HTML :
Link
you can either use
echo 'Link';
or
echo "Link';
if you use double quotes you can insert the variable into the string and it will be parsed.
Basically like this,
<?php
$link = ""; // Link goes here!
print "Link";
?>
as simple as that: echo 'Link';
You can use one and more echo statement inside href
Link
link : "/profile.php?usr=firstname&email=email"
This worked much better in my case.
HTML in PHP: Link
The safest way to generate links in PHP is to use the built-in function http_build_query(). This function is very easy to use and takes an array as an argument.
To create a dynamic link simply echo out the result of http_build_query() like so:
$data = [
'id' => $id,
'name' => $name
];
echo 'Link';
If you want to print in the tabular form with, then you can use this:
echo "<tr> <td><h3> ".$cat['id']."</h3></td><td><h3> ".$cat['title']."<h3></</td><td> <h3>".$cat['desc']."</h3></td><td><h3> ".$cat['process']."%"."<a href='taskUpdate.php' >Update</a>"."</h3></td></tr>" ;

Output formatted version of variable if it has a value, blank otherwise

I'm looking to add an if statement to display a title if something is in a variable but not 100% sure how to go about it.
My current coding shows:
<?php echo $quote->getmessage(); ?>
But I would like it to show a title and the content of the message if there is content in the variable. If there is nothing within the variable I don't want to show anything.
IMHO it's better to be as verbose as necessary so that the code is easily readable and can be extended without totally rewriting it:
<?php
$message = $quote->getmessage();
if (!empty($message)) {
echo "Title!";
echo htmlspecialchars($message);
}
?>
Use php's isset function to check if a variable is set, and empty to see if it's empty.
For instance
<?php $a = $quote->getmessage();if (!empty($a)) echo $a; ?>
<?php echo ($quote->getmessage() == "") ? "" : "Title <br />".$quote->getmessage(); ?>
Use a ternary operator:
<?php
$quote_var = $quote->getmessage();
echo ($quote_var != null)?$quote_var:'NOTHING!';
//displays 'NOTHING' if the variable is null
?>
<?php
$mssg = $quote->getmessage();
echo (!empty($mssg))?$mssg:'';
?>
If you want your line to be concise, then I would advise this syntax:
<?php $msg = $quote->getmessage() AND print "<h6>title</h6>$msg"; ?>
The AND has a lower precedence than the assignment (but extra whitespace or braces make that more readable). And the second part only gets executed if the $msg variable receives any content. And print can be used in this exporession context instead of echo.
<?php
if(isset($quote->getmessage())
{
echo "My Title";
}
?>

Categories