简体   繁体   English

jquery-validate-PHP嵌入式HTML表单

[英]jquery-validate - php embedded html forms

Can someone give me a hand with this please? 有人可以帮我吗? I have been using jquery-validate without issues for html forms until I came across an issue with HTML forms embedded in php. 我一直在使用jquery-validate来处理html表单,直到遇到嵌入php中的HTML表单的问题。

The example below is using jquery and jquery-validate. 下面的示例使用jquery和jquery-validate。 Form1 is validating the email field fine. Form1正在验证电子邮件字段正常。 Form3 however is not validating the number field (form3 is embedded in php). 但是,Form3无法验证数字字段(form3嵌入在php中)。

I have attempted to comment out all form1 validation to ensure it wasn't multiple forms causing the issue. 我试图注释掉所有form1验证,以确保不是导致问题的多种形式。

I also tried commenting out the submitHandler from the javascript for form3 and this prevented form3 from submitting so this tells me that something is working it's just not validating the number field in form3. 我还尝试将JavaScript从Form3的JavaScript中注释掉,这阻止了Form3的提交,因此这告诉我有些事情在起作用,只是不验证Form3中的数字字段。

<script>
$(document).ready(function() {
    $("#form1").validate( {
        errorPlacement: function(error, element) {
            error.appendTo( element.parent("span").next("div") );
        },

        debug:true,
        wrapper: "li", 
        submitHandler: function(){
            form.submit();
        },
        rules: {                
            email: {required:false, email:true},
        },

        messages: {
            email: "Enter a valid email address",
        }
    })

    $("#form3").validate( {
        errorPlacement: function(error, element) {
            error.appendTo( element.parent("span").next("div") );
        },

        debug:true,
        wrapper: "li", 
        submitHandler: function(){
            form3.submit();
        },

        rules: {                
            quotaLimit: {required:true, number:true},
        },

        messages: {
            quotaLimit: "Enter a valid quota limit",
        }
    })
});
</script>

<br>

<div class="tab-button-row" style="position:relative;">
<a href="#" class="tab-button" onclick="toggleTab('editUserContent', 'details');">User Details</a>
<a href="#" class="tab-button" onclick="toggleTab('editUserContent','quotas');">Quota and Limits</a>
</div>
<br>

<div id="editUserContent">
<div id="details">
    <form id=form1 method=POST
          action="index.php?function=edituser&command=save&username=<?php echo $user->{"username"}; ?>"
          name=form>

        <?php
        if ($user->{'authenticationType'} == 0) {
            $visible = "";
        } else {
            $visible = "style='display:none;'";
        }
        ?>

        <table class="list">
            <tr>
                <td colspan=5><b>Edit Details</td>
                </td>
                <td width=25%><input type=submit value=save></td>
            </tr>

            <tr>
                <td>First Name:</td>
                <td>
                    <input name="fname" value="<?php echo $user->{"fname"};?>">
                </td>
                <td>Last Name:</td>
                <td>
                    <input name="lname" value="<?php echo $user->{"lname"};?>">
                </td>
                <td>Email:</td>
                <td>
                    <span>
                    <input name="email" value="<?php echo $user->{"email"};?>" class='validate'>
                    </span>
                <div></div>
                </td>
            </tr>
        </table>

        <br>
        <table class="list" <?php echo $visible; ?>>
            <tr>
                <td colspan=5><b>Password Settings</td>
                </td>
                <td width=25%><input type=submit value=save></td>
            </tr>
            <tr>
                <td>Set Password</td>
                <td><input type=checkbox name=change_password></td>
                <td>New Password:</td>
                <td><input name="password" type=password></td>
                <td>Re-enter Password:</td>
                <td><input name="re_password" type=password></td>
            </tr>
        </table>
    </form>
</div>

<div id="quotas">
    <?php
    $table = new Table();
    $name = new Column("Quota Period");
    $name->render = function($data, $id)
    {
        echo $data->{'period'};
    };

    $desc = new Column("Quota Limit(MB)");
    $desc->render = function($data, $id)
    {
        echo $data->{'quotaLimit'};
    };

    $owner = new Column("Group Owner");
    $owner->render = function($data, $id)
    {
        if (isset($data->{'owner'})) {
            echo "<a href=index.php?function=editgroup&group=" . $data->{'ownerid'} . ">" . $data->{'owner'} . "</a>";
        }
    };

    $reached = new Column("Quota Reached");
    $reached->render = function($data, $id)
    {
        if ($data->{'exceeded'} == true) {
            echo "true";
        } else {
            echo "false";
        }
    };

    $ops = new Column("Operations");
    $ops->render = function($data, $id)
    {
        if (!isset($quotas[$i]->{'owner'})) {
            echo "<a href=index.php?function=edituser&command=deletequota&username=" . $_GET['username'] . "&period=";
            echo $data->{'period'} . "&quotaLimit=" . $data->{'quotaLimit'} . ">delete</a>";
        }
    };

    $table->addColumn($name);
    $table->addColumn($desc);
    $table->addColumn($owner);
    $table->addColumn($reached);
    $table->addColumn($ops);
    $table->setData($quotas);
    $table->footerMethod = function($footerArgs)
    {
        echo" <form id=form3 method=POST action=index.php?function=edituser&command=addquota&username=" . $_GET['username'] . ">";
        echo "<tr><td>";
        echo "<select name='period'>";
        echo "<option>NONE</option>";
        echo "<option>DAY</option>";
        echo "<option>WEEK</option>";
        echo "<option>MONTH</option>";
        echo "</select>";
        echo "</td>";

        echo "<td>";
        echo "<span><input name=quotaLimit class='validate' /></span><div></div>";
        echo "</td>";

        echo "<td></td>";
        echo "<td></td>";
        echo "<td><input type=submit value='Add/Set'></td></tr>";
        echo "</form>";
    };

    $table->render();
    ?>

</div>
</div>

<script type="text/javascript">
initToggleTab("editUserContent", "details");
</script>

Thanks for the help. 谢谢您的帮助。 Managed to fix the issue by moving the form tags outside of the table to where it is rendered by php: 通过将表格标签移动到表外以php呈现的位置来解决此问题:

    $table->footerMethod = function($footerArgs)
    {
        echo "<tr><td>";
        echo "<select name='period'>";
        echo "<option>NONE</option>";
        echo "<option>DAY</option>";
        echo "<option>WEEK</option>";
        echo "<option>MONTH</option>";
        echo "</select>";
        echo "</td>";

        echo "<td>";
        echo "<span><input name=\"quotaLimit\" class=\"validate\" type='text' /></span><div></div>";
        echo "</td>";

        echo "<td></td>";
        echo "<td></td>";
        echo "<td><input type=submit value='Add/Set'></td></tr>";
    };

    echo" <form id=form3 method=POST action=index.php?function=edituser&command=addquota&username=" . $_GET['username'] . ">";
    $table->render();
    echo "</form>";

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

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