gpt4 book ai didi

php - AJAX 调用问题 - 没有数据传递(这里相对缺乏经验!)

转载 作者:行者123 更新时间:2023-11-29 17:48:07 25 4
gpt4 key购买 nike

我认为我在这里做了一些非常简单的错误(对于所有经验丰富的程序员),但是请原谅我,我花了很多时间阅读试图理解 AJAX 调用是如何工作的......

我的变量没有将其插入到recordsale.php 中以插入到SQL 表中。

JavaScript 变量显示在 HTML 页面上,因此我知道它们包含数据。

有人可以提供一些建议吗?

(任何影响我能力的回应都不会受到赞赏 - 我正在尝试:))

我的 html 表单提交按钮看起来像这样

<form action="recordsale.php" method="post">
<input type="image" src="assets/CompleteSale.png" alt="Submit"> </form>

我的 AJAX Post 功能:

 $( "#submit" ).submit(function( event ) {
alert("hello");
// Jquery code for making AJAX call to server side script
$.ajax({
method: "POST",
url: "recordsale.php",
data: { adultqty: adultticketqty, concessionqty: concessionticketqty, studentqty: studentticketqty, childqty: childticketqty, programqty: programqty, totalsell: totalsaleprice }
})
//alert(adultqty + childqty)
});

记录销售.php

<?php 

$adultticketqty = $_POST['adultqty'];
$concessionticketqty = $_POST['concessionqty'];
$studentticketqty = $_POST['studentqty'];
$childticketqty = $_POST['childqty'];
$programqty = $_POST['programqty'];
$totalsaleprice = $_POST['totalsell'];

//include('phpsqlget.php');

/* Attempt MySQL server connection. Assuming you are running MySQL server with default setting (user 'root' with no password) */

$link = mysqli_connect("localhost", "root", "speedway", "speedwayticketsales");
// Check connection
if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } // Attempt insert query execution
$sql = "INSERT INTO stdsalesrecords (qtyadulttickets, qtyconcessiontickets, qtystudenttickets, qtychildtickets, qtyprograms, totalsell) VALUES ($adultticketqty,$concessionticketqty,$studentticketqty,$childticketqty,$programqty,$totalsaleprice)";

if(mysqli_query($link, $sql)){
echo "Records inserted successfully."; }
else{ echo "ERROR: Could not able to execute $sql. "

. mysqli_error($link); } // Close connection mysqli_close($link);


?>

最佳答案

这就是我通常进行 ajax 提交的方式。还有其他处理表单提交的方法。查看 $(form).serialize() 等。

 $( "#submit" ).submit(function( event ) {
var adultticketcty = $('selector').val();
//continue filling out the fields and mirror them in the below dataString.

var dataString = 'adultqty='+adultticketcty+'&concessionqty='+concessionticketqty;

//Check if you set the correct values in the datastring.
console.log(dataString);

$.ajax({
type: "POST",
url: "recordsale.php",
data: dataString,
success: function(data){
//check whatever is returned from recordsale.php
console.log(data);
}
});
});

然后在你的recordsale.php中执行此操作以查看ajax成功调用中是否返回任何内容:

$adultticketqty = $_POST['adultqty']; 
$concessionticketqty = $_POST['concessionqty'];
echo $adultticketqty;

与问题无关,但我想建议检查 pdo 查询以防止 sql 注入(inject)等。

关于php - AJAX 调用问题 - 没有数据传递(这里相对缺乏经验!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49632937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com