Adam
Adam

Reputation: 9049

unable to get my window.location to work within echo tag

echo "<form><input type='button' value='$back_label' onclick='window.location="'$url'"'/></form>";

I cant figure the whole single and double quotes thing when it comes to the window.location code because it has an extra set of single quotes to wrap around the url. I have no idea what to do. I tried escaping the quotes.

Also, can you use a relative path for this method?

Thanks

Upvotes: 0

Views: 5256

Answers (4)

ramesh babu
ramesh babu

Reputation: 11

Just change the quotes at the end to onclick='window.location="$url"'/>

echo "<form><input type='button' value='$back_label'
onclick='window.location="$url"'/></form>";

Upvotes: 1

Patrick
Patrick

Reputation: 3172

I believe to meet the HTML 'standard' the almost all attributes must use double quotes and for javascript you need to encapsulate the url so:

echo '
    <form>
        <input type="button" value="'.$back_label.'" onclick="window.location=\''.$url.'\'" />
    </form>';

EDIT

A cleaner way to code this is to use heredoc syntax as it eliminates the need for escaping:

echo <<<EOL
        <form>
            <input type="button" value="$back_label" onclick="window.location='$url'" />
        </form>';
EOL;

UPDATE

You are able to go down a directory structure, and I just did a quick test and it appears to work going up the hierarchy as well.

Upvotes: 0

Mark Grey
Mark Grey

Reputation: 10257

Can you take it out of the PHP context?

    <?php $url = 'http://www.yourdomain.com'; ?>
    <form>
      <input type='button' value='<?php echo $back_label;?>' onclick='window.location="<?php echo $url;?>"'/>
    </form>

Upvotes: 1

Shakti Singh
Shakti Singh

Reputation: 86336

Try this

 echo "<form><input type='button' value='$back_label' onclick='window.location=\"$url\"'/></form>";

A working example on http://codepad.org/K7AafokT

Upvotes: 5

Related Questions