-3

I have an href link in my code that is calling the new_requisition.php, and i am also disabling it if it does not fulfill an argument. The code is below:

<?php 
if ($row['req_status']=='Approved'){
    echo '<a href="new_requisition.php?cid=<?php echo $cid;?>" class="btn btn-block btn-primary">
                  <i class="glyphicon glyphicon-shopping-cart text-blue"></i> 
                  Add New Request
              </a>';
} else {                  
    echo "Not allowed to raise a PR";                         
}
?>

The $cid is set prior with $_POST follows:

$cid=$_POST['cid'];

I succesfully get the value passing from the previous page as this determines the data showing to this one but i cannot pass it to the next one.

My problem is that i cannot get it to pass the $cid variable. When I click it what I get is:

new_requisition.php?cid=<?php%20echo%20$cid;?>

Instead of new_requisition.php?cid=3 for example, which creates me trouble as i need to insert the cid to the database.

Any ideas what am i missing?

juamx214
  • 1
  • 1
  • 5
  • 1
    What's in `$cid` ? – Jules R Dec 17 '18 at 14:37
  • 1
    1) You're already in a PHP block, you don't need to use the PHP tags inside the string. 2) Even if you remove the PHP tags and just use the variable, it won't parse because it's inside of a single quote block. You'll need to switch the quotes to encapsulate the whole string inside of a double quote, or break out of the single quotes and use concatenation. – aynber Dec 17 '18 at 14:38
  • `$cid` isn't defined in your code – Quentin Dec 17 '18 at 14:38
  • Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – aynber Dec 17 '18 at 14:39
  • When I click the link it goes to `new_requisition.php?cid=` not `new_requisition.php?cid=`. Your code doesn't reflect your symptoms. Provide a real [mcve] – Quentin Dec 17 '18 at 14:40
  • You're trying to put HTML into php. Break up the php blocks. – Isaac Dec 17 '18 at 14:41

2 Answers2

0

use this code,

if ($row['req_status']=='Approved'){
        echo "<a href='new_requisition.php?cid=".$cid."' class='btn btn-block btn-primary'>
                      <i class='glyphicon glyphicon-shopping-cart text-blue'></i> 
                      Add New Request
                  </a>";
    } else {                  
        echo "Not allowed to raise a PR";                         
    }
Massoud
  • 33
  • 6
-1

You're repeating the PHP tags, when you shouldn't. The output should be:

echo '<a href="new_requisition.php?cid='.$cid.'" class="btn btn-block btn-primary">

You need to use concatenation instead, since the ?> PHP tag inside of the href block is causing your PHP to exit early.

BenM
  • 52,573
  • 26
  • 113
  • 168