0

我有一个表单,我有 JS 表单验证。但验证过程不起作用。插入过程工作正常。

以下是将数据插入mysql数据库的表格-

<?php
session_start();
include("includes/connection.php");
include("header.php");
include("includes/adminmenu.php");
if(isset($_SESSION['username']))
        {
        //echo $_SESSION['username'];
        ?>
        <!--Javasript Validation File Import -->
<script type="text/javascript" src="js/qcheck.js"></script>
<script type="text/javascript" language="JavaScript">

function HidePart(d) { document.getElementById(d).style.display = "none";  }
function ShowPart(d) { document.getElementById(d).style.display = "block"; }
function CheckboxChecked(b,d)
{
   if(b) { ShowPart(d); }
   else  { HidePart(d); }
}
</script>
    <br />
    <div class="userstat">

        <div style="background-color:#666666; text-align:center; font-weight:bold; color:#FFFFFF; font-size:24px;"><span>Insert A new Question</span></div>
        <br />
        <div class="statdata">
            <form action="includes/insertq.php" method="POST" name="qform" onSubmit="return valide()">
                    <div style="text-align:center;">
                <select name="subject" size="0">
                <option selected="0" value="">Select Subject</option>
                <option value="bangla">Bangla</option>
                <option value="english">English</option>
                <option value="physics">Physics</option>
                <option value="chemistry">Chemistry</option>
                <option value="math">Mathematics</option>
                <option value="biology">Biology</option>
                <option value="gk">General knowledge</option>
                </select>
                     </div>
                 <br /><br />
                <label for="question">Write Down the Question below</label>
                <textarea name="question" rows="3" cols="40"></textarea><br /><br />
                <label for="ans">Options</label><br /><br />
                <label for="option1">a.</label>
                <input type="text" name="option1" size="40" /><br />
                <label for="option2">b.</label>
                <input type="text" name="option2" size="40" /><br />
                <label for="option3">c.</label>
                <input type="text" name="option3" size="40" /><br />
                <label for="option4">d.</label>
                <input type="text" name="option4" size="40" /><br /><br />
                <label for="correct">Correct.</label><br />&nbsp;&nbsp;&nbsp;
                <input type="text" name="correct" size="40" /><br /><br /><br />
                <div style="text-align:center;">
                <input type="submit" name="submit" value="Submit Question" />
                </div>
                <br />
                <br />
            </form>
        </div>
        </div>

     </body>
        <?php
        }



    else
        {
        header("location: admin.php");
        }
?>

<?php
include("includes/footer.php");
?>

和 Javascript 文件是

function valide()
{
var subject=document.forms["qform"]["subject"].value;
var question=document.forms["qform"]["question"].value;
var option1=document.forms["qform"]["option1"].value;
var option2=document.forms["qform"]["option2"].value;
var option3=document.forms["qform"]["option3"].value;
var option4=document.forms["qform"]["option4"].value;
var correct=document.forms["qform"]["correct"].value;

if(subject == null || Subject == "Select Subject")
    {
        alert("Select subject Type");
        return false;

        }
    else if(question==null || question=="" || question.length<5)
        {
            alert("Insert Valid question");
            return false;
            }   
    else if(option1==null || option1=="")
        {
            alert("Insert Option 1.");
            return false;
            }
    else if(option2==null || option2=="")
        {
            alert("Insert Option 2.");
            return false;
            }
    else if(option3==null || option3=="")
        {
            alert("Insert option 3.");
            return false;
            }   
    else if(option4==null || option4=="")
        {
            alert("Insert option 4.");
            return false;
            }   
    else if(correct==null || correct=="")
        {
            alert("Insert correct option.");
            return false;
            }                                   





}
4

1 回答 1

0

案件很重要

if(subject == null || Subject == "Select Subject")
                      ^

主题!==主题

值也不会为空。您应该检查长度为零。

于 2013-04-08T15:27:17.273 回答