Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 来自Ajax的未定义结果_Php_Ajax - Fatal编程技术网

Php 来自Ajax的未定义结果

Php 来自Ajax的未定义结果,php,ajax,Php,Ajax,我使用Ajax将一些表单变量传递给php页面,但是当php代码运行时,表行中填充了未定义的值 我已经检查了php代码,替换了表单变量,效果很好,因此我认为问题在于Ajax代码 阿贾克斯 HTML 正确的语法是 等等 你让自己很难受。如果发送整个表单,则无需从表单中获取所有单个值。但如果这样做,则需要使用encodeURIComponent()正确编码每个值。也不要在查询字符串中发送任何空格 最简单的解决方案是让jQuery序列化表单并发送: $.ajax({ type: "POST",

我使用Ajax将一些表单变量传递给php页面,但是当php代码运行时,表行中填充了未定义的值

我已经检查了php代码,替换了表单变量,效果很好,因此我认为问题在于Ajax代码

阿贾克斯

HTML

正确的语法是


等等

你让自己很难受。如果发送整个表单,则无需从表单中获取所有单个值。但如果这样做,则需要使用
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也将为您处理编码问题。

您是否检查了浏览器开发工具中的AJAX请求,以检查A)是否向PHP脚本发送了正确的数据,B)PHP脚本是否返回了正确的响应?在AJAX请求检查这些VAR是否正确设置之前,您是否尝试过类似于
alert(name)
的方法?谢谢您的解释
<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>
$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());
data: {name: name, address: address, number: number}
$.ajax({
    type: "POST",
    url: "add.php",
    data: $('form.submit').serialize(),    // or just: data: $(this).serialize(),
    success: function(){
        $('form.submit').hide(function(){$('div.success').fadeOut();});
    }
});