在此链接中,我仍然存在相同的问题。因此,我使用了该append方法。这是AJAX代码:
append
AJAX
<script> 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('<tr><td>'+selectW+'</td><td>'+select_at+'</td><td>'+pay+'</td><td>'+facture+'</td><td>'+select_opt+'</td></tr>'); $('#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!!"); } } </script>
这里是我去掉表格action=insert.php和method=post并转化按钮type=submit进入button我的<form>。所以形式是:
action=insert.php
method=post
type=submit
button
<form>
<form name="insertForm" id="form1"> <input type="hidden" name="insert" value="true"> <tr> <td align="center"> <select id="selectW" name="type"> <option value="لم يتم التحديد">اختر النوع</option> <option value="دولارات">دولارات</option> <option value="أيام + دولارات">أيام + دولارات</option> <option value="بطاقات">بطاقات</option> <option value="هواتف">هواتف</option> <option value="اكسسوارات">اكسسوارات</option> <option value="تسديد فواتير">تسديد فواتير</option> </select> <!--<select> <?php foreach($result5 as $rows){ ?> <option value="<?php echo $rows['item_name'] ?>"><?php echo $rows['item_name'] ?></option> <?php } ?> </select>--> </td> <td align="center"><select id="select_at" name="alfa_touch"> <option value="غير محدد">Not Required</option> <option value="Alfa">Alfa</option> <option value="Touch">Touch</option></select></td> <td align="center"><input type="text" id="pay" name="pay"/></td> <td align="center"><input type="text" id="facture" name="facture" placeholder="في حال دفع الفواتير عبر omt"/></td> <td align="center"><select id="select_opt" name="currency"> <option value="9">ليرة</option> <option value="10">دولار</option> </select></td> <td align="center"><input type="button" id="insert_new" onClick="addFunction()" name="insert_new" value="اضافة" /> </td> </tr> </form>
这是我的insert.php文件,在我的项目中,我有很长的代码用于不同的提交按钮,但是我只会发布用于此AJAX函数的代码:
insert.php
$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("y-m-d")); $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("y-m-d")); $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 []变量不匹配