1

In PHP I am writing:

$link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs='. $cat .'>5.000€</a>";

But the value "$cat" is not showing anything. Any suggestion?

Consider that if I write in HTML:

<a href="<?php echo '/?s=cqs3&importo_desiderato=5000&categoria_cqs=<?php echo $cat; ?>">5.000€</a>

It works...

Probably it is something I am missing in the sintax. Thank you!

5
  • You have there bad quotes. Commented Feb 22, 2021 at 12:19
  • <?php echo $link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs='. $cat .'>5.000€</a>"; Commented Feb 22, 2021 at 12:19
  • I cannot use " inside the variable Commented Feb 22, 2021 at 12:22
  • Might be of help: using single quotes within double quotes. Commented Feb 22, 2021 at 12:25
  • actually even "echo" I cannot use. It is inside a PHP file with several variables I call from a PHP page Commented Feb 22, 2021 at 12:26

3 Answers 3

1

I solved like this:

<?php

$cat = $_GET['categoria_cqs'];//(to get the value from the session)
$link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs=$cat'>5.000€</a>";

?>

I still cannot understand why I had to get it again (in HTML it worked without it) but that is fine. Thank you for your help!

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

Comments

0

The error is in the quotes, your line should look like this:

$link = '<a href="/?s=cqs3&importo_desiderato=5000&categoria_cqs='. $cat .'">5.000€</a>';

Remember that the string must be closed with the same quotation mark as it was opened.

Additionally, remember that the character " " and ' ' can "escape" themselves, as MoarCodePlz said.

Comments

0

I don't know what your real purpose is, if it is to create this directly in PHP or not, but maybe the code below will help you, I created a variable that will store the website URL that will pass the $cat variable as a parameter, to see the result I used the <a> tag in html

The code would look like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
    $cat = 'Pathname';
    $link = "https://yourwebsite.com/?s=cqs3&importo_desiderato=5000&categoria_cqs=' . $cat . '";
    ?>

    <a href="<?php echo $link; ?>">5.000€</a>
</body>
</html>

If you want to get the result only in PHP, use the following code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
    $cat = 'Pathname';

    $link = "<a href='/?s=cqs3&importo_desiderato=5000&categoria_cqs=" . $cat . "'>5.000€</a>";

    echo $link;
    ?>
</body>
</html>

Edit

You say that your code works only using HTML and not in PHP. Well, I've been analyzing it here and I saw that this was happening due to the fact that the file is in a directory, right? PHP was not understanding that the URL was from this directory, at least for me the error was this one in addition to the quotes

Please try the code below, I believe it will work in both PHP and HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
    $cat = $_GET['categoria_cqs'];

    $link = '<a href="/?s=cqs3&importo_desiderato=5000&categoria_cqs=' . $cat . '">5.000€</a>';
    echo $link;
    ?>

    <a href="/?s=cqs3&importo_desiderato=5000&categoria_cqs=<?php echo $cat; ?>">5.000€</a>

</body>
</html>

6 Comments

well, probably the variable is not stored, but if I write directly in HTML like this <a href="/?s=cqs3&importo_desiderato=5000&categoria_cqs=<?php echo $cat; ?>">5.000€</a> it works... I cannot understand why
In your PHP file, try to change ' . $cat . ' to " . $cat . "
What's the problem?
the problem is that the variable is empty only if I write the link in PHP $link = "<a class='box' href='/?s=cqs3&importo_desiderato=5000&categoria_cqs=" . $cat . "'>5.000€</a>"; but if I write directly in HTML like this <a href="/?s=cqs3&importo_desiderato=5000&categoria_cqs=<?php echo $link; ?>">5.000€</a> it works... I cannot understand why
Please check my answer and tell me if it's working now
|

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.