简体   繁体   English

如何在 JavaScript 的 echo 语句中插入 # 字符?

[英]How do I insert # character in echo statement for JavaScript?

When I include the # glyph in my statement, to return to an anchor on the page, the PHP code fails.当我在语句中包含 # 字形以返回到页面上的锚点时,PHP 代码将失败。

I've tried everything I can think of to resolve this issue, escaping PHP characters, writing JavaScript functions and the list goes on.我已经尝试了所有我能想到的方法来解决这个问题,转义 PHP 字符,编写 JavaScript 函数等等。

if (isset($_POST['name'])) {
    $_POST = array();
    echo "<script>window.location.href='Contact_Us.php#myForm'</script>";
    } else {
    unset($_POST);
}

There are no error messages.没有错误消息。 The page appears to refresh and the code to unset the POST variables fails.页面似乎刷新了,取消设置 POST 变量的代码失败。

If you really want to do things that way simply drop out of PHP instead of trying to echo things.如果您真的想以这种方式做事,只需退出 PHP 而不是试图回应事物。

if (isset($_POST['name'])) {
    $_POST = array();
?>
<script>window.location.href='Contact_Us.php#myForm'</script>
<?php
    } else {
    unset($_POST);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM