开发者

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


Try this

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

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


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>


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜