SamSmith SamSmith - 3 months ago 13
CSS Question

onclick location href not working with php variable

I have a button with a link that involves a PHP variable, but when the button is clicked, nothing happens and I am not brought to a new page according to the link. I tested the link with a

<a>
tag instead of a
<button>
tag, and the link works. I am new to PHP so any suggestions are appreciated.

Here is the code:

<?php
echo
'<button type="button" class="btn btn-secondary" id="btn" style="margin-left:0%;color:gray" onclick="location.href="' . $child_page . '"">
KEEP READING &nbsp;<span class="glyphicon glyphicon-chevron-right" style="color:gray"></span>
</button>';


echo '<a href="' . $child_page . '">Link </a>';
?>


The variable can look like:
/n/2/ar/
and will apply like this:
www.example.com/n/2/ar/
. Please note that the inline CSS is only for testing purposes.

Answer

There is a problem with the quotes..Nothing else Use this:

echo 
       '<button type="button" class="btn btn-secondary" id="btn" style="margin-left:0%;color:gray" onclick=location.href="'.$child_page.'">
                        KEEP READING &nbsp;<span class="glyphicon glyphicon-chevron-right" style="color:gray"></span>
          </button>';