dsgwoh7038 2015-02-05 07:35
浏览 41

无法使用php脚本插入ajax数据

I have this jquery script

<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>

and this insert.php script

<?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);
?>

but when I try to click button with id=btn1 nothing happens, no errors, no messages in Chrome console and no data inserted in the database. What is wrong here?

edit#1: this is head part of my document:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Car Repair | Locations</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" media="screen" href="css/reset.css">
<link rel="stylesheet" type="text/css" media="screen" href="css/style.css">
<link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:700,300' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="js/jquery-1.7.min.js"></script>
<script type="text/javascript" src="js/jquery.easing.1.3.js"></script>
<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>

so jquery library is loaded here, I believe.

  • 写回答

2条回答 默认 最新

  • douhuan6065 2015-02-05 07:39
    关注

    In your case (note the & as a separator):

    'name='+name+'&last_name='+last_name
    

    But jQuery does that for you if you specify your data as an object:

    data: { name: name, last_name: last_name}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入