没有添加datainto数据库,并且没有在表中显示使用AJAX和PHP附加

我在这个链接中仍然存在同样的问题。 所以,我使用了append方法。 这是AJAX代码:

  function addFunction() { var selectW = $('#insert_new').val(); var selectW = $('#selectW').val(); var select_at = $('#select_at').val(); var pay = $('#pay').val(); var facture = $('#facture').val(); var select_opt = $('#select_opt').val(); if(pay!="") { $.ajax({ data: {'text': insert_new, 'text': selectW, 'text': select_at, 'text': pay, 'text': facture, 'text': select_opt}, type: "post", url: "insert.php", success: function(response){ if(response=="success") { $('#incident_table').append(''+selectW+''+select_at+''+pay+''+facture+''+select_opt+''); $('#selectW').val(''); $('#select_at').val(''); $('#pay').val(''); $('#facture').val(''); $('#select_opt').val(''); } else { alert("No data added"); } }, error: function(){ alert('error; ' + eval(error)); } }); } else { alert("Specify a value of payment please!!"); } }  

这里是我删除action=insert.phpmethod=post的表单,并将按钮type=submit转换为我的

button 。 所以表格是:

       اختر النوع دولارات أيام + دولارات بطاقات هواتف اكسسوارات تسديد فواتير  <!--  <option value="">  -->   Not Required Alfa Touch    ليرة دولار      

这是我的insert.php文件,我的项目中有不同提交按钮的长代码,但我只发布用于此AJAX函数的代码:

  $selectOpt1=""; if(isset($_REQUEST['insert_new'])){ error_reporting(E_ALL); require_once ('../include/global.php'); $selectOpt1 = $_REQUEST['select_opt']; if($selectOpt1=="9"){ $type = $_REQUEST['selectW']; $provider = $_REQUEST['select_at']; $pay = $_REQUEST['pay']; $facture = $_REQUEST['facture']; try{ $query = "INSERT INTO sales (type, provider, pay, facture, date_now, time_now) VALUES (:type, :provider, :pay, :facture, :date, now())"; $stmt = $conn->prepare($query); $stmt->bindValue(":type", $type); $stmt->bindValue(":provider", $provider); $stmt->bindValue(":pay", $pay); $stmt->bindValue(":facture", $facture); $stmt->bindValue(":date", date("ymd")); $count = $stmt->execute(); //header("location: home.php"); echo "success"; //$last_id = $conn->lastInsertId(); //echo $last_id; } catch(PDOException $e) { echo $e->getMessage(); //header("location: ../pages/insert_false.php?id=".$projid); print_r($conn->errorInfo()); } } if($selectOpt1=="10"){ $type = $_REQUEST['type']; $provider = $_REQUEST['alfa_touch']; $payL = $_REQUEST['pay']; $pay = $payL*1500; $facture = $_REQUEST['facture']; try{ $query = "INSERT INTO sales (type, provider, pay, facture, date_now, time_now) VALUES (:type, :provider, :pay, :facture, :date, now())"; $stmt = $conn->prepare($query); $stmt->bindValue(":type", $type); $stmt->bindValue(":provider", $provider); $stmt->bindValue(":pay", $pay); $stmt->bindValue(":facture", $facture); $stmt->bindValue(":date", date("ymd")); $count = $stmt->execute(); echo "success"; //header("location: home.php"); } catch(PDOException $e) { echo $e->getMessage(); //header("location: ../pages/insert_false.php?id=".$projid); print_r($conn->errorInfo()); } } } 

在控制台网络中我有这些值:

在此处输入图像描述

并且控制台中没有错误。 我一直看到警报:没有添加数据。

编辑我收到此错误消息:insert_buy.php中未定义的select_opt(我将文件从insert.php更改为insert_buy.php):

在此处输入图像描述

尝试将ajax中的数据更改为此

 data: {'insert_new': 'insert_new', 'selectW': selectW, 'select_at': select_at, 'pay': pay, 'facture': facture, 'select_opt': select_opt}, 

您没有获得任何值,因为您的数据变量与$ _request []变量不匹配

[1]为什么要抓住button value

 var selectW = $('#insert_new').val(); 

我认为没有必要这样做。

[2]在ajax调用之前添加它

 dataString = 'selectW='+ selectW + '&select_at='+ select_at + '&pay='+ pay+ '&facture='+facture+ '&select_opt='+ select_opt; 

[3]在ajax调用中设置`data’为

 data: dataString, 

代替

  data: {'text': insert_new, 'text': selectW, 'text': select_at, 'text': pay, 'text': facture, 'text': select_opt}, 

[4]为什么要捕获response ,你没有从insert_buy.php发回任何string

  if(response=="success") { 

因为你没有获得return value作为"success"else部分正在执行并获得alert消息“没有添加数据”。

请更正您要发送到服务器的json数据

  data: {'insert_new': 1, 'selectW': selectW, 'select_at': select_at, 'pay': pay, 'facture': facture, 'select_opt': select_opt}, 

并在PHP中添加一个骰子

  if(isset($_REQUEST['insert_new'])){ // your code die(); // just before closing the if to make sure you are not echoing anything more; }