如何使用jQueryajax从另一个文件调用PHP代码



这是我的index.php:

<!DOCTYPE html>
<html lang="en" >
<head>
<title>Cassette Decks</title>
<link rel="stylesheet" href="css/styles.css">
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="js/scripts.js" ></script>
<script>
</script>
</head>
<body>
<form role="form" id="attributes" name='attributes' class="cassette_deck_table" >
<table id="cassette_deck_params" >
<tr>
<td colspan='8' >
<label for='type3' >Type III</label><input type='checkbox' name='type3' id='type3' value='type3' />
<label for='type4' >Type IV</label><input type='checkbox' name='type4' id='type4' value='type4' >
<label for='dbb' >Dolby B</label><input type='checkbox' name='dbb' id='dbb' value='dbb' >
<label for='dbc' >Dolby C</label><input type='checkbox' name='dbc' id='dbc' value='dbc' >
<label for='dbs' >Dolby S</label><input type='checkbox' name='dbs' id='dbs' value='dbs' >
<label for='dbx' >DBX</label><input type='checkbox' name='dbx' id='dbx' value='dbx' >
<label for='anrs' >ANRS</label><input type='checkbox' name='anrs' id='anrs' value='anrs' >
<label for='sanrs' >SANRS</label><input type='checkbox' name='sanrs' id='sanrs' value='sanrs' >
</td>
</tr>
<tr>
<td><a><b>Manufacturer</b></a></td>
<td><a><b>Model</b></a></td>
<td><a><b>Made from</b></a></td>
<td><a><b>Made to</b></a></td>
<td><a><b>Has Dolby B</b></a></td>
<td><a><b>Has Dolby C</b></a></td>
<td><a><b>Has Dolby S</b></a></td>
<td><a><b>Has DBX</b></a></td>
</tr>
</table>
<div id="resultMsg"></div>
<select name="num_of_rows" id="nor">
<option selected value="10" >10</option>
<option value="25" >25</option>
<option value="50" >50</option>
<option value="100" >100</option>
</select> 
<h2>JSON</h2>
<pre id="result"></pre>
<button type="submit" id="search_button" name="search_button"  onClick="" />Search</button>
</form>
<div id="cassette_decks" >
<script>
$("#cassette_decks").load("cassette_tables.php");
</script>
</div>
<script>
$(document).ready(function() {
$("#search_button").click(function(e) {
e.preventDefault();
var search={
"type3" : document.getElementById("type3").checked ,
"type4" : document.getElementById("type4").checked ,
"dbb" : document.getElementById("dbb").checked ,
"dbc" : document.getElementById("dbc").checked ,
"dbs" : document.getElementById("dbs").checked ,
"dbx" : document.getElementById("dbx").checked ,
"anrs" : document.getElementById("anrs").checked, 
"sanrs" : document.getElementById("sanrs").checked,
"nor" : document.getElementById("nor").value
};
var searchStr=JSON.stringify(search);
$.ajax({
type: "POST",
url: "createTable.php",
dataType: "text",
data: {searchArray : search},
success : function(data,status){
console.log(data);
},
error : function(status){
console.log(status);
}
});
});
});
</script>
</body>
</html>

这大致是我的createTable.php:

<?php
if(isset($_POST['searchArray'])){
//run a bunch of code that basically creates a table from a database array
?>

我试着在互联网上看了一整天如何将数组从jQuery传递到php。

基本上,我要做的是删除一个表,然后从表单中创建一个具有不同参数的新表。如果我阻止表单提交,它就不起作用。

如果我使用dataType : "json",它不会做任何事情(成功时没有输出(。基本上,如果我确实发送了ajax请求,它确实成功地发送了它,但php中的POST什么都不做。

我使用的是php8,但我不知道该怎么办。我尝试了一个页面上的不同代码,它确实有效。我只是不知道该怎么办。对php和js/jQuery还是个新手。任何帮助都会奏效。

search是一个对象,而不是数组。但在PHP中,它将变成一个关联数组。

所有钥匙都将在$_POST['searchArray']中。因此,您可以使用$_POST['searchArray']['type3']来判断是否选中了type3复选框。

您也可以将jQuery代码更改为使用:

data: search,

那么search对象的每个键都将是$_POST中的一个键,所以您可以只使用$_POST['type3']。当然,在这种情况下,您不应该使用if (isset($_POST['searchArray'])),因为它已经不存在了。

我不太确定你想做什么,但这里有一个如何使用Ajax的例子:

Ajax.js

$(document).on("click", ".button-class", function() {
var id = $(this).attr("data-id");
$.ajax({
type: "POST",
url: "/absolute_path/to/php_file.php",
data: {my_id:id, my_custom_key: "my_custom_value"},
dataType: "JSON",
success: function (response) {
console.log(response);
},
error: function(response){
console.log(response);
}
})
.done(function(data) {
window.location = "customers/info";
})
})

Php_file.Php

<?php
$id = $POST["my_id"];
$customValue = $POST["my_custom_value"];
$response = ["message"=> "We took your id which is $id and your custom value which is $customValue"];
echo json_encode($response);
?>

这是如何使用ajax的一个基本示例。

解释

我从以前的一个答案中复制了这个ajax。我们在Ajax中传递一些参数,如Ajax.php文件所示。

PHP部分稍微简单一点。您得到的变量发布到PHP文件中。因此,您可以调用$POST变量来读取发布的数据。就是这样!

最新更新