SamSmith
SamSmith

Reputation: 177

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.

Upvotes: 1

Views: 4984

Answers (5)

Nicholas Macharia
Nicholas Macharia

Reputation: 124

Try this

<?php
$child_page = 'https://www.google.com';

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>";
?>

Upvotes: 0

Glen Despaux Jr
Glen Despaux Jr

Reputation: 1114

If you begin with double-quotes instead of single-quotes, it can be done this way:

$child_page = 'this.html';

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>";

Double-quotes allow you to place a variable 'as-is' whereas with single-quotes you'll have to do a couple extra steps.

Upvotes: 0

SattyamBhatt
SattyamBhatt

Reputation: 328

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>';

Upvotes: 1

Ruslan Bes
Ruslan Bes

Reputation: 2735

It looks like you have a problem with your opening tag. The code

$child_page = '/n/2/ar/';
echo '<button ... onclick="location.href="' . $child_page . '"">';

renders to

<button ... onclick="location.href="/n/2/ar/"">

which is understood by browser as

<button ... onclick="location.href=" /n/2/ar/ "">

So the location is being empty. Try wrapping url into single quotes instead:

$child_page = '/n/2/ar/';
echo '<button ... onclick="location.href=\'' . $child_page . '\'">';

Upvotes: 0

Yossi Neiman
Yossi Neiman

Reputation: 825

You have a problem with the quotes Try this:

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

?>

Upvotes: 2

Related Questions