jquery验证,数据不保存在数据库中

时间:2012-12-26 01:21:09

标签: php jquery

我这里有一个在数据库中插入数据的代码,它的工作并没有错误,但是当我把我的jquery代码用于验证数据时,我的php代码用于将数据插入到我的数据库中工作,请你检查我的代码。谢谢!

这是我的PHP代码:index.php

<form id="FormToValidate" action="insert.php" method="POST">
  <div id="content">
    <p>
      <input type="text" name="csname" id="csname" style="width:250px" margin-bottom="3px"/>
         </p>
        <p>
          <select name="units" style="width:254px">
            <option value="option">option</option>
           </option>
            <option value="option">option</option>
</option>

          </select>
        </p>
        <p>
          <input type="text" name="color" id="color" style="width:250px"/>
        </p>


      <p>
        <input type="text" name="consticker" id="consticker" style="width:250px"/>
      </p>
      <p>
        <select name="mp" style="width:254px">
          <option value="name">name</option>

        </select>
      </p>
        <p>
          <select name="grm" style="width:254px">
            <option value="name">name</option>

          </select>
        </p>
        <p><select name="typeoftransact" style="width:254px">
<option value="type">type</option>

</select></p>
      <p>
        <input name="dp" type="dp" style="width:250px" />
      </p>
      <p>
        <input name="amount" type="amount" style="width:250px" />
      </p>
      <p>
        <input name="dirate" type="dirate" style="width:250px" />
      </p>
      <p align="right">AMNT PAID: </p>
<p>NEXT</p>
</div>

    <div id="secondary">
      <p>VSI No.:</p>
        <p>DATE:</p>
        <p>&nbsp; </p>
      <p>TYPE OF SUBSIDY 1:</p>
        <p>TYPE OF SUBSIDY 2:</p>
        <p>TYPE OF SUBSIDY 3:</p>
        <p>TYPE OF SUBSIDY 4:</p>
        <p>TYPE OF SUBSIDY 5:</p>
        <p>CHATTEL MORTGAGE:</p>
        <p>&nbsp;</p>
        <p>
          <input name="chapaid" type="text" style="width:120px" />
        </p>
</div>
    <div id="third">
      <p>
        <input type="text" name="vsi" id="vsi" style="width:180px"/>
</p>
<p>
  <input type="text" name="date" id="date" style="width:180px"/>
</p>
<p>&nbsp;</p>
<p>
  <select name="type1" style="width:180px">
    <option value="option">option</option>
  </select>
</p>
<p>
  <select name="type2" style="width:180px">
   <option value="option">option</option>
  </select>
</p>
<p>
  <select name="type3" style="width:180px">
    <option value="option">option</option>
  </select>
</p>
<p>
  <select name="type4" style="width:180px">
    <option value="option">option</option>
  </select>
</p>
<p>
  <select name="type5" style="width:180px">
    <option value="option">option</option>
  </select>
</p>
        <p>FREE: 
          <input name="chafree" type="text" style="width:100px" />
        </p>
</div>
<div id="fourth">
        <p>&nbsp;</p>
<p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>Php: 
      <input name="type1amount" type="text" style="width:120px" />
        </p>
        <p>Php:
          <input name="type2amount" type="text" style="width:120px" />
        </p>
        <p>Php:
          <input name="type3amount" type="text" style="width:120px" />
        </p>
        <p>Php:
          <input name="type4amount" type="text" style="width:120px" />
        </p>
        <p>Php:
          <input name="type5amount" type="text" style="width:120px" />
        </p>
    <p>COST: 
      <input name="chacost" type="text" style="width:120px" />
    </p>
        <p>&nbsp;</p>

    <p align="right"><input class="small button blue" name="Save" type="submit" id="Save" value="Save" /></p>


</div>
</div>
</form>

这是我的函数insert.php

<?php
include("connection.php");

$vsi = $_POST["vsi"];
$date = $_POST["date"];
$custname = $_POST["csname"];
$unit = $_POST["units"];
$color = $_POST["color"];
$consticker = $_POST["consticker"];

$mp = $_POST["mp"];
$grm = $_POST["grm"];
$typetrans = $_POST["typeoftransact"];
$dp = $_POST["dp"];
$amount = $_POST["amount"];
$dirate = $_POST["dirate"];
$typesub1 = $_POST["type1"];
$typesub2 = $_POST["type2"];
$typesub3 = $_POST["type3"];
$typesub4 = $_POST["type4"];
$typesub5 = $_POST["type5"];
$chacost = $_POST["chacost"];
$chapaid = $_POST["chapaid"];
$chafree = $_POST["chafree"];
$type1amount = $_POST["type1amount"];
$type2amount = $_POST["type2amount"];
$type3amount = $_POST["type3amount"];
$type4amount = $_POST["type4amount"];
$type5amount = $_POST["type5amount"];

if($_POST["Save"]=="Save")
{
$query="INSERT INTO accounts (vsi,date,cname,unit,color,consticker,typetrans,downpayment,amountfinanced,dirate,mp,grm,typesub1,typesub2,typesub3,typesub4,typesub5,chapaid,chacost,chafree,type1amount,type2amount,type3amount,type4amount,type5amount)
VALUES
('".$vsi."','".$date."','".$custname."','".$unit."','".$color."','".$consticker."','".$typetrans."','".$dp."','".$amount."','".$dirate."','".$mp."','".$grm."','".$typesub1."','".$typesub2."','".$typesub3."','".$typesub4."','".$typesub5."','".$chapaid."','".$chacost."','".$chafree."','".$type1amount."','".$type2amount."','".$type3amount."','".$type4amount."','".$type5amount."');";

(mysql_query($query))

or die ("error!".mysql_error() );

}

header('Location: index.php');

?>

如果我把这个jquery代码,我的PHP代码不起作用,但如果没有,它的工作正常, 这是我的jquery script

<script src="Scripts/jquery-1.6.1.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery-ui-1.8.13.custom.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.validate.wrapper.js" type="text/javascript"></script>

<script type="text/javascript" language="javascript">

    $(document).ready(function () {
        // 1. prepare the validation rules and messages.
        var rules = {
            vsi: {
                required: true,
                minlength: 7
            },
            date: "required",
            csname: "required",
            color: "required"
        };
        var messages = {
            vsi: {
                required: "VSI Number must be Inputted.",
                minlength: "VSI Number should be atleast 7 numbers."
            },
            date: "Date is requried",
            csname: "Customer Name is required",
             color: "Color is required"
        };

        // 2. Initiate the validator
        var validator
            = new jQueryValidatorWrapper("FormToValidate",
                rules, messages);

        // 3. Set the click event to do the validation
        $("#Save").click(function ()
                     {
                        if (!validator.validate())
                           {
                             return;
                            }
                        $('#FormToValidate').submit();
                         alert("Data Saved!");
                          return;

                    });
    });

</script>

1 个答案:

答案 0 :(得分:0)

<script src="Scripts/jquery-ui-1.8.13.custom.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.validate.wrapper.js" type="text/javascript"></script>
    $(document).ready(function(){         // 1.准备验证规则和消息。         var rules = {             vsi:{                 要求:是的,                 最小长度:7             },             日期:“必填”,             csname:“required”,             颜色:“必填”         };         var messages = {             vsi:{                 必需:“必须输入VSI编号。”,                 minlength:“VSI编号应至少为7个数字。”             },             日期:“需要日期”,             csname:“客户名称是必填项”,              颜色:“颜色是必需的”         };         // 2.启动验证器         var验证器             = new jQueryValidatorWrapper(“FormToValidate”,                 规则,消息);         // 3.设置click事件以进行验证         $(“#Save”)。click(function()                      {                         if(!validator.validate())                            {                              返回;                             }                         $( '#FormToValidate')提交()。                          alert(“Data Saved!”);                           返回;                     });     });