1

我有这个 .php 文档,您可以在其中在网上商店注册。我想为其添加 javascript 验证,但该站点完全由 .php 组成,因此没有 html 头和正文标记。通常您可以将 javascript 源代码放在 head 标记中,但不能放在 .php 中。(或者至少我不知道如何)测试代码时,javascript文档看起来不错,但是当我把它放在.php文档中时,它给出了关于(“)和(')的某些错误。当我尝试链接时对于单独的 javascript 文档,代码似乎根本不起作用。

(我已经添加了整个 php 页面) PHP 文档开始:

<?php

include 'header.php';
include 'menu.php';

    echo '

function checkEmail(myForm) 
{
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(registration.emailAddress.value))
    {
    return (true)
    }
        alert("Invalid E-mail Address! Please re-enter.")
        return (false)
}

function validatePwd() 
{
    var invalid = " "; // Spatie mag niet
    var minLength = 6; // Minimaal 6 tekens
    var pw1 = document.registration.password.value;
    var pw2 = document.registration.passwordConfirm.value;
// Beide velden gelijke waarde
    if (pw1 == "" || pw2 == "") 
    {
        alert("Please enter your password twice.");
        return false;
    }
// Minimale lengte variabele
    if (document.registration.password.value.length < minLength) 
    {
        alert("Your password must be at least " + minLength + " characters long. Try again.");
        return false;
    }
// spatiegebruik variabele
    if (document.registration.password.value.indexOf(invalid) > -1); 
    {
        alert("Spaces are not allowed.");
        return false;
    }

    else 
    {
    if (pw1 != pw2) 
        {
        alert ("You did not enter the same new password twice. Please re-enter your password.");
        return false;
        }
    else 
    {
        return true;
    }
    }
}

        <div id="main">

    <form name="registration" action="registerconfirm.php" method="post" id="form" onSubmit="return validatePwd()">


        <h2>Personal Information</h2>


        <fieldset class="login_register">


            <label for="firstName" class="form">First name:</label> 

                <div class="registrationForm" id= "firstNameCheck"></div>

                    <input type="text" name="firstname" id="register_first_name"/>


            <label for="lastName" class="form">Last name:</label>

                <div class="registrationForm" id="lastNameCheck"></div>

                    <input type="text" name="lastname" id="register_last_name"/>


            <label for="email" class="form">E-mail:</label>

                <div class="registrationForm" id="emailCheck"></div>

                    <input type="text" name="emailAddress" id="register_email"/>


        </fieldset>


        <h2>Login Information</h2>

        <fieldset class="login_register">


            <label for="password" class="form">Password:</label>

                <div class="registrationForm" id="lastNameCheck"></div>                 

                    <input type="password" name="password" id="password"/>


            <label for="passwordCheck" class="form">Confirm password:</label>

                <div class="registrationFormAlert" id="divPassCheck"></div>

                    <input type="password" name="passwordConfirm" id="passwordConfirm"/>


            <input type="submit" class="button" value="SUBMIT"/>

        </fieldset>

    </form>

</div>

';

include 'footer.php';

?>

和Javascript代码:

<SCRIPT LANGUAGE="JavaScript">

function checkEmail(myForm) 
{
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(registration.emailAddress.value))
    {
    return (true)
    }
        alert("Invalid E-mail Address! Please re-enter.")
        return (false)
}

function validatePwd() 
{
    var invalid = " "; // Invalid character is a space
    var minLength = 6; // Minimum length
    var pw1 = document.myForm.password.value;
    var pw2 = document.myForm.passwordConfirm.value;
// check for a value in both fields.
    if (pw1 == '' || pw2 == '') 
    {
        alert('Please enter your password twice.');
        return false;
    }
// check for minimum length
    if (document.myForm.password.value.length < minLength) 
    {
        alert('Your password must be at least ' + minLength + ' characters long. Try again.');
        return false;
    }
// check for spaces
    if (document.myForm.password.value.indexOf(invalid) > -1) 
    {
        alert("Sorry, spaces are not allowed.");
        return false;
    }

    else 
    {
    if (pw1 != pw2) 
        {
        alert ("You did not enter the same new password twice. Please re-enter your password.");
        return false;
        }
    else 
    {
        return true;
    }
    }
}

4

3 回答 3

1

JavaScript 在客户端的浏览器(也称为客户端)中执行,PHP 是一种服务器端语言。

除非您打算为 PHP 编写 JS 解释器,否则您无法从 PHP 运行 JS。
更好的选择是用 PHP 重写 JS。


除非您在包含文件时遇到问题,否则您只需将 a:
<script type="text/javascript" src="myJSFile.js"></script>标签编辑到您的header.php.

所以:

echo '<script type="text/javascript" src="myJSFile.js"></script>';
于 2013-01-30T11:46:59.117 回答
1

有几件事:您可以在<script>任何您喜欢的地方添加标签。但是,属性language并不真正存在,但这并不重要。都一样,应该是<script type="text/javascript">
如果您的网站是(一堆)PHP 脚本的输出,为什么不简单地echo使用脚本呢?

echo '<script type="text/javascript" src="path/to/script.js"></script>';

或者,在头文件中:

    echo '<title>Title</title>';
?>
<script type='text/javascript'>
    var script ='just add it here';
</script>
<?php
    echo '</head>';//continue php

也没有什么能阻止你这样做。

于 2013-01-30T11:48:45.273 回答
0

尝试使用 PHPs HEREDOC 语法来输出函数。 http://www.php.net/manual/it/language.types.string.php#language.types.string.syntax.heredoc

于 2013-01-30T11:48:45.217 回答