来自Ajax的未定义结果

时间:2016-10-27 13:50:01

标签: php ajax

我正在使用Ajax将一些表单变量传递到php页面,但是当php代码运行时,表行中的值为undefined。

我检查了php代码,替换了表单变量,并且工作正常,所以我认为问题出在Ajax代码上,

AJAX

 $(document).ready(function(){
$('form.submit').submit(function () {
var name = $(this).find('.name').attr('value');
var address = $(this).find('.address').attr('value');
var number = $(this).find('.number').attr('value');
var price = $(this).find('.price').attr('value');
var deposit = $(this).find('.deposit').attr('value');
var product = $(this).find('.product').attr('value');
var payment_type = $(this).find('.payment_type').attr('value');
var deal_date = $(this).find('.deal_date').attr('value');
var install_date = $(this).find('.install_date').attr('value');
var installed = $(this).find('.installed').attr('value');
var notes = $(this).find('.notes').attr('value');
var contract_received = $(this).find('.contract_received').attr('value');
// ...
    $.ajax({
        type: "POST",
        url: "add.php",
        data: "name="+ name +"& address="+ address +"& number="+ number +"& price="+ price +"& deposit="+ deposit +"& product="+ product +"& payment_type="+ payment_type +"& deal_date="+ deal_date +"& install_date="+ install_date +"& installed="+ installed +"& notes="+ notes +"& contract_received="+ contract_received,

        success: function(){
            $('form.submit').hide(function(){$('div.success').fadeOut();});

        }
    });
return false;
});
 });

HTML

<form id="submit" name="submit" class="submit">
     <input name="name" id="name" type="text" class="form-control" placeholder="Name"/><br />
     <input name="address" id="address" type="text" class="form-control" placeholder="Address"/><br />
     <input name="number" id="number" type="text" class="form-control" placeholder="Number"/><br />
     <input name="price" id="price" type="text" class="form-control" placeholder="Price"/><br />
     <input name="deposit" id="deposit" type="text" class="form-control" placeholder="Deposit"/><br />
     <input name="product" id="product" type="text" class="form-control" placeholder="Product"/><br />
     <input name="payment_type" id="payment_type" type="text" class="form-control" placeholder="Payment"/><br />
     <input name="deal_date" id="deal_date" type="text" class="form-control" placeholder="Deal Date"/><br />
     <input name="install_date" id="install_date" type="text" class="form-control" placeholder="Install Date"/><br />
     <input name="installed" id="installed" type="text" class="form-control" placeholder="Installed"/><br />
     <textarea name="notes" id="notes" cols="" rows="" class="form-control" placeholder="Notes"></textarea><br />
     <input name="contract_received" id="contract_received" type="text" class="form-control" placeholder="Contract Received"/><br />
     <input type="submit"  name="button" id="button" value="Submit" />
</form>

PHP

$name        = htmlspecialchars(trim($_POST['name']));
$address        = htmlspecialchars(trim($_POST['address']));
$number       = htmlspecialchars(trim($_POST['number']));
$price        = htmlspecialchars(trim($_POST['price']));
$deposit        = htmlspecialchars(trim($_POST['deposit']));
$product        = htmlspecialchars(trim($_POST['product']));
$payment_type        = htmlspecialchars(trim($_POST['payment_type']));
$deal_date       = htmlspecialchars(trim($_POST['deal_date']));
$install_date        = htmlspecialchars(trim($_POST['install_date']));
$installed        = htmlspecialchars(trim($_POST['installed']));
$notes        = htmlspecialchars(trim($_POST['notes']));
$contract_received        = htmlspecialchars(trim($_POST['contract_received']));

$addClient  = "INSERT INTO DATA (
name, address,number,price,deposit,product,payment_type,deal_date,install_date,installed,notes,contract_recieved)VALUES('$name','$address','$number','$price','$deposit','$product','$payment_type','$deal_date','$installed_date','$installed','$notes','$contract_received')";
mysql_query($addClient) or die(mysql_error());

2 个答案:

答案 0 :(得分:1)

你让自己变得非常困难。如果您要发送整个表单,则无需从表单中获取所有单个值。但是,如果这样做,则需要使用encodeURIComponent()正确编码每个值。并且不要在查询字符串中发送任何空格。

最简单的解决方案是让jQuery序列化您的表单并发送:

$.ajax({
    type: "POST",
    url: "add.php",
    data: $('form.submit').serialize(),    // or just: data: $(this).serialize(),
    success: function(){
        $('form.submit').hide(function(){$('div.success').fadeOut();});
    }
});

现在,表单中的所有键值对都将被正确发送,jQuery也将为您处理编码。

答案 1 :(得分:0)

正确的语法是http://api.jquery.com/jQuery.ajax/

data: {name: name, address: address, number: number}

等等。