使用变量进行PHP复选框验证

时间:2013-03-20 18:25:27

标签: php javascript html

我正在尝试对我的代码应用一些验证来产生错误,如果没有从项目列表中选择项目dsiplayed(来自txt文件)我已设置验证如果没有输入访问者名称为复选框指定的名称是一个变量<input type='checkbox' name='$partno'>所以它使得设置一些验证非常困难,任何想法如何实现这一点将不胜感激。

 <script>
        function validateName()
        {
            var x=document.forms["purchase"]["visitor"].value;
            if (x==null || x=="")
            {
                alert("Visitor name must be entered");
                return false;
            }
        }
    </script>
</head>

<body>

    <h1>Items Available</h1>

    <form name="purchase" action="confirm.php" method="post" onsubmit="return validateName()">
        <table>

            <h3>Visitor Name: <input type='text' name='visitor'></h3>
            <tr><th></th><th></th><th></th><th>Price</th><th>&#10004;</th><th>QTY</th></tr>

            <?php
            if (!($data = file('items.txt'))) {
                echo 'ERROR: Failed to open file! </body></html>';
                exit;
            }
            foreach ($data as $thedata) {
                list($partno, $name, $description, $price, $image) = explode('|', $thedata);
                echo "<tr><td><img src='$image' width='60' height='60' alt='image'></td><td><h3>$name</h3></td><td>&nbsp;&nbsp;&nbsp;&nbsp;$description</td>
                    <td>&nbsp;&nbsp;&nbsp;&nbsp;<b>&pound;$price</b></td><td><input type='checkbox' name='$partno'></td><td><input type='text' size='1' name='qty' value='1'</td></tr>";
            }
            ?> 

        </table>

到目前为止,我已经尝试过了......

function validateName()
    {
        var x=document.forms["purchase"]["$partno"].value;
        if (x==null || x=="")
        {
            alert("Select atleast one item");
            return false;

失败了。

新代码

<script>
        function validateName()
        {
            var x=document.getElementById("purchase").value
            if (x==null || x=="")
            {
                alert("Visitor name must be entered");
                return false;
            }
        }
    </script>
</head>

<body>

    <h1>Items Available</h1>

    <form id="purchase" name="purchase" action="confirm.php" method="post" onsubmit="return validateName()">
        <table>

            <h3>Visitor Name: <input type='text' name='visitor'></h3>
            <tr><th></th><th></th><th></th><th>Price</th><th>&#10004;</th><th>QTY</th></tr>

            <?php
            if (!($data = file('items.txt'))) {
                echo 'ERROR: Failed to open file! </body></html>';
                exit;
            }
            foreach ($data as $thedata) {
                list($partno, $name, $description, $price, $image) = explode('|', $thedata);
                echo "<tr><td><img src='$image' width='60' height='60' alt='image'></td><td><h3>$name</h3></td><td>&nbsp;&nbsp;&nbsp;&nbsp;$description</td>
                    <td>&nbsp;&nbsp;&nbsp;&nbsp;<b>&pound;$price</b></td><td><input type='checkbox' name='$partno'></td><td><input type='text' size='1' name='qty' value='1'</td></tr>";
            }
            ?> 

        </table>

3 个答案:

答案 0 :(得分:0)

首先在JavaScript将访问它的表单中使用HTML属性“id”。

<form id="purchase" name="purchase" action="confirm.php" method="post" onsubmit="return validateName()">

然后使用以下内容访问它:

document.getElementById("purchase").value 

答案 1 :(得分:0)

如果要传递$ partno变量,则应将其作为隐藏

传递
<input type="hidden" value=$partno">

function validateName()
   {
    var x=document.forms["purchase"]["$partno"].value;
    if (x==null || x=="")
     {
        alert("Select atleast one item");
        return false;

答案 2 :(得分:0)

这对你没问题 -

<h3>Visitor Name: <input type='text' id="name" name='visitor'></h3>

和脚本使用

<script>
    function validateName()
    {
        var x = document.getElementById("name").value
        if (x==null || x=="")
        {
            alert("Visitor name must be entered");
            return false;
        }
    }
</script>