Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.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
Javascript Can';t使用php脚本插入ajax数据_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript Can';t使用php脚本插入ajax数据

Javascript Can';t使用php脚本插入ajax数据,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有这个jquery脚本 <script> $("#btn1").click(function(e) { e.preventDefault(); var name = $("#id1").val(); var last_name = $("#id2").val(); var dataString = 'name='+name+'&last_name='+last_name; $.ajax({ type:'POST', data:d

我有这个jquery脚本

<script>
    $("#btn1").click(function(e) {
  e.preventDefault();
  var name = $("#id1").val(); 
  var last_name = $("#id2").val();
  var dataString = 'name='+name+'&last_name='+last_name;
  $.ajax({
    type:'POST',
    data:dataString,
    url:'php/insert.php',
    success:function(data) {
      alert(data);
    }
  });
});
</script>

$(“#btn1”)。单击(功能(e){
e、 预防默认值();
var name=$(“#id1”).val();
var last_name=$(“#id2”).val();
var dataString='name='+name+'&last_name='+last_name;
$.ajax({
类型:'POST',
数据:dataString,
url:'php/insert.php',
成功:功能(数据){
警报(数据);
}
});
});
还有这个insert.php脚本

<?php
  $dbconn = pg_connect("host=localhost dbname=kinoseansy user=postgres password=postgrespass")
        or die('Could not connect: ' . pg_last_error());
  $name = $_POST['name'];
  $last_name = $_POST['last_name'];
  $result = pg_query($dbconn, "INSERT INTO contacts(address,phone_number,city,others) 
                  VALUES('+1 123 456 7890', 'John', 'Doe','123');");
  var_dump($result);
  pg_close($dbconn);
?>
在您的情况下(注意&作为分隔符):

但是,如果将数据指定为对象,jQuery会为您做到这一点:

data: { name: name, last_name: last_name}

尝试绑定doc ready块中的
单击
事件:

$(function() { // <----doc ready starts
  $("#btn1").click(function(e) {
    e.preventDefault();
    var name = $("#id1").val();
    var last_name = $("#id2").val();
    var dataString = 'name=' + name + '&last_name=' + last_name;
    $.ajax({
      type: 'POST',
      data: dataString,
      url: 'php/insert.php',
      success: function(data) {
        alert(data);
      }
    });
  });
}); // <-----doc ready ends

$(function(){//1)是否在按钮存在后执行脚本
$(function(){script here});
2)是否加载了jQuery库?3)控制台中是否有错误?4)php调用是否正确?(同一控制台中的网络选项卡)如果您使用的是chrome,请检查开发者控制台的网络选项卡,并查看ajax进程请插入您的html代码。请确保您包括jquery libraryPHP需要在web服务器上运行。ajax需要从web服务器加载。您不能使用file:///c:.....I 添加了这个函数,但是现在我发现这个错误ad文件:(my path)/php/insert.php。只有协议方案支持跨源请求:http、data、chrome、chrome扩展、https、chrome扩展资源。jquery-1.7.min.js:4 f.support.ajax.f.ajaxTransport.sendjquery-1.7.min.js:4 f.extend.ajaxlocations.html:18(匿名函数)jquery-1.7.min.js:3 f.event.dispatch jquery-1.7.min.js:3 f.event.add.h.handle.i
当然,所有id在我的文档中都是唯一的。这里可能有问题:
@andreysalba什么是
(我的路径)
?它是像
http://example.com/
?它是一个本地路径,类似于
C:/Users/AndreyK/Desktop/cr/php/insert.php
,因此php没有在web服务器上运行?那么您就不能这样做
data: { name: name, last_name: last_name}
$(function() { // <----doc ready starts
  $("#btn1").click(function(e) {
    e.preventDefault();
    var name = $("#id1").val();
    var last_name = $("#id2").val();
    var dataString = 'name=' + name + '&last_name=' + last_name;
    $.ajax({
      type: 'POST',
      data: dataString,
      url: 'php/insert.php',
      success: function(data) {
        alert(data);
      }
    });
  });
}); // <-----doc ready ends