0

I am trying to include a php variable inside this php script that displays an html link. What i need is to include my php $row['vin'] variable in the href html link after the ? to pass a value to the page i am linking to. Top code block works but i still need that php variable, bottom is an example of what ive tried which will not work.

Works but missing my php variable:

 <?php if($row['instock'] == "Yes")
    {
         echo '<a href="orderForm.php?">
         <span class="glyphicon glyphicon-plus" aria-hidden="true">
         </span>
         </a>';
    }
    ?>

Does not work:

<td>
<?php if($row['instock'] == "Yes")
{
      echo '<a href="orderForm.php?'. $row['vin']">
      <span class="glyphicon glyphicon-plus" aria-hidden="true">
      </span>
      </a>';
}
?>
</td>

2 Answers 2

3

You've forgotten the rest of the concatenation logic. It's just a syntax error:

<td>
<?php if($row['instock'] == "Yes")
{
      echo '<a href="orderForm.php?'. $row['vin'] . '">
      <span class="glyphicon glyphicon-plus" aria-hidden="true">
      </span>
      </a>';
}
?>
</td>
Sign up to request clarification or add additional context in comments.

Comments

1

An alternative to using PHP to echo out great chunks of mostly static content is to instead, drop in and out of the PHP context (ie <?php ... ?>) when necessary and use it like a templating language.

For example

<td>
  <?php if ($row['instock'] == "Yes"): ?>
    <a href="orderForm.php?<?= htmlspecialchars($row['vin']) ?>">
      <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
    </a>
  <?php endif ?>
</td>

See http://php.net/manual/control-structures.alternative-syntax.php

Comments

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.