Javascript和php引用阻止函数的执行


Javascript and php quotations preventing the execution of functions

我正在尝试使用以下代码构建注册表,我仍处于开始阶段。我在执行 PHP echo 语句中的任何 JavaScript 代码时遇到了非常困难的问题。

我尝试使用斜杠'''" 转义 javascript 函数中的单引号和双引号,这适用于顶级 javascript 函数,但是一旦我添加了其他两个函数并应用了相同的逻辑,所有 javascript 函数都停止工作。

有人可以建议防止问题并轻松执行php echo语句中的javascript代码的最简单方法是什么?

<?php
echo '
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>      
    <script type="text/javascript">
        function validateEmail(email) { 
            var re = /^(([^<>()[']''.,;:'s@'"]+('.[^<>()[']''.,;:'s@'"]+)*)|('".+'"))@(('[[0-9{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'])|(([a-zA-Z'-0-9]+'.)+[a-zA-Z]{2,}))$/;
            if(re.test(email)) {
                document.getElementById(''result'').innerHTML = ''<img src='"http://www.customotion.com/green_check_small.png'" style='"width:15px;'"/> Valid'';
            } else {
                document.getElementById(''result'').innerHTML = ''<img src='"http://biglistbigsales.com/m/templates/GPT/images/x_xMarkRed4.png'" style='"width:15px;'"/> Invalid'';
            }
        }   
    </script>
</head>
<body>
<div id="main-container">
<div id="body-container" style="padding-bottom:150px; height:800px;">
    <div style="height:100px; margin:0px auto;">
        <img class="reg-step" style="width:100%;" src="images/step1.png">
    </div>
    <div style="margin-left:110px;">
        <span style="font-size:24px;" class="smallheading">Tell Us About Yourself</span>
        <br/><span style="font-size:13.5px;"><i>*All fields required</i></span>
    </div>
    <form method="post" action="registernew.php">
    <div style="margin:0px auto; margin-left:110px; border:1px solid blue; float:left; font-size:12px; width:500px;">
        <div style="border:1px solid blue; float:left; width:80%;">
            <div style="float:left; border:1px solid red;">
                First Name:
                <br/><input type="text" style="width:140px;" name="fname" />
            </div>
            <div style="float:right; margin-right:50px; border:1px solid red;">
                Last Name:
                <br/><input type="text" style="width:140px;" name="lname" />
            </div>
        </div>
        <div style="border:1px solid blue; float:left; margin-top:20px; width:50%;">
            <div style="float:left; border:1px solid red;">
                City:
                <br/><input type="text" style="width:170px;" name="fname" />
            </div>
            <div style="float:right; border:1px solid red;">
                State:
                <br/>';
                include('inc/statedropdown.inc.php');
echo'       </div>
        </div>
        <div style="float:left; width:100%;">
            <br/>Company Name:<br/> <input type="text" name="fname" style="width:150px;"/>
            <br/><br/>E-mail Address:<br/> e-mail:<br/><input id="email" type="text" onblur="validateEmail(this.value)" /><span id="result"></span>
            <br/><br/>Phone Number:<br/> <input type="text" name="fname" style="width:110px;" />
            <br/><br/>Password:<br/><input type="password" name="fname" />
            <br/><br/>Confirm Password:<br/><input type="password"  name="lname" />
            <br/><input type="submit" value="Next Step" style="background-color:#ebda5f; color:blue; margin:15px 0px 0px 20px;">
        </div>
    </form>
    </div>
</div>
</div>
</body>
</html>';
?>

仅在需要时使用 <?php ?> 标记。任何不在标签内的内容都会被回显并被视为 HTML。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>      
    <script type="text/javascript">
        function validateEmail(email) { 
            var re = /^(([^<>()[']''.,;:'s@'"]+('.[^<>()[']''.,;:'s@'"]+)*)|('".+'"))@(('[[0-9{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'])|(([a-zA-Z'-0-9]+'.)+[a-zA-Z]{2,}))$/;
            if(re.test(email)) {
                document.getElementById(''result'').innerHTML = ''<img src='"http://www.customotion.com/green_check_small.png'" style='"width:15px;'"/> Valid'';
            } else {
                document.getElementById(''result'').innerHTML = ''<img src='"http://biglistbigsales.com/m/templates/GPT/images/x_xMarkRed4.png'" style='"width:15px;'"/> Invalid'';
            }
        }   
    </script>
</head>
<body>
<div id="main-container">
<div id="body-container" style="padding-bottom:150px; height:800px;">
    <div style="height:100px; margin:0px auto;">
        <img class="reg-step" style="width:100%;" src="images/step1.png">
    </div>
    <div style="margin-left:110px;">
        <span style="font-size:24px;" class="smallheading">Tell Us About Yourself</span>
        <br/><span style="font-size:13.5px;"><i>*All fields required</i></span>
    </div>
    <form method="post" action="registernew.php">
    <div style="margin:0px auto; margin-left:110px; border:1px solid blue; float:left; font-size:12px; width:500px;">
        <div style="border:1px solid blue; float:left; width:80%;">
            <div style="float:left; border:1px solid red;">
                First Name:
                <br/><input type="text" style="width:140px;" name="fname" />
            </div>
            <div style="float:right; margin-right:50px; border:1px solid red;">
                Last Name:
                <br/><input type="text" style="width:140px;" name="lname" />
            </div>
        </div>
        <div style="border:1px solid blue; float:left; margin-top:20px; width:50%;">
            <div style="float:left; border:1px solid red;">
                City:
                <br/><input type="text" style="width:170px;" name="fname" />
            </div>
            <div style="float:right; border:1px solid red;">
                State:
                <br/>
                <?php include('inc/statedropdown.inc.php'); ?>
            </div>
        </div>
        <div style="float:left; width:100%;">
            <br/>Company Name:<br/> <input type="text" name="fname" style="width:150px;"/>
            <br/><br/>E-mail Address:<br/> e-mail:<br/><input id="email" type="text" onblur="validateEmail(this.value)" /><span id="result"></span>
            <br/><br/>Phone Number:<br/> <input type="text" name="fname" style="width:110px;" />
            <br/><br/>Password:<br/><input type="password" name="fname" />
            <br/><br/>Confirm Password:<br/><input type="password"  name="lname" />
            <br/><input type="submit" value="Next Step" style="background-color:#ebda5f; color:blue; margin:15px 0px 0px 20px;">
        </div>
    </form>
    </div>
</div>
</div>
</body>
</html>