简体   繁体   中英

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.

I've tried everything I can think of to resolve this issue, escaping PHP characters, writing JavaScript functions and the list goes on.

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.

If you really want to do things that way simply drop out of PHP instead of trying to echo things.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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