gpt4 book ai didi

php - Jquery AJAX 发布到 PHP 不起作用

转载 作者:行者123 更新时间:2023-11-29 00:17:12 24 4
gpt4 key购买 nike

这是jquery

$(document).ready(function () {
$('#Main').change(function () {
var Sel = $(this).val();
console.log(Sel);
$(".Subselect").hide();
$(".lastSel").hide();
$("#" + Sel).show();
$.ajax({
type: "POST",
url: "test.php",
data: "id=" + Sel + "&part_id="+$(this).attr('name'), //part_id = region
success: function(data) {
$(".region").html(html);
}
});
});
$('.Subselect').change(function () {
var subSel = $(this).val();
console.log(subSel);
$(".lastSel").hide();
$("#" + subSel).show();
$.ajax({
type: "POST",
url: "test.php",
data: "id=" + subSel + "&part_id="+$(this).attr('name'), //part_id = city
success: function(data) {
$(".city").html(html);
}
});
})
});

这是mysql表

CREATE TABLE IF NOT EXISTS `s_countries` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`code` varchar(2) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
`name` varchar(128) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `id_name` (`name`),
KEY `id_code` (`code`)
) ENGINE=MyISAM;

CREATE TABLE IF NOT EXISTS `s_regions` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`countryid` int(11) unsigned NOT NULL DEFAULT '0',
`name` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `id_countryid` (`countryid`),
KEY `id_name` (`name`)
) ENGINE=MyISAM;

CREATE TABLE IF NOT EXISTS `s_cities` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`countryid` int(11) unsigned NOT NULL DEFAULT '0',
`regionid` int(10) unsigned NOT NULL DEFAULT '0',
`name` varchar(128) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `id_name` (`name`),
KEY `id_regionid` (`regionid`),
KEY `id_countryid` (`countryid`)
) ENGINE=MyISAM;

这是 mailtest.php(我尝试了很多选项,但 cat 确实有效)这里是 (html + Jquery http://jsfiddle.net/Nazaret2005/bPUbb/

<?
require "include/db.php";
dbconn(true);
loggedinorreturn(true);


echo "<form method=\"POST\">";
$cat = "<select id='Main' name='country'>";
$cat .= "<option value='0'>Выбрать</option>";
$results = do_mysql_query("SELECT id, code, name FROM s_countries") or sqlerr(__FILE__, __LINE__);
while($c = mysqli_fetch_array($results)){
$cat .= "<option value='".$c['id']."'>".$c['name']."</option>";
}
$cat .= "</select>";
echo $cat;
echo "<select name='region' class='Subselect'>";
echo "<option value='0'>-Выбрать-</option>";
echo "</select>";
?>

这是test.php

<?
require_once("include/db.php");
dbconn(true);

if(isset($_POST['id'])){
$id=(int)$_POST['id'];
$res = do_mysql_query("SELECT id, name FROM s_regions WHERE countryid = '".$id."'") or sqlerr(__FILE__, __LINE__);
if(mysqli_num_rows($res) > 0){
while($r = mysqli_fetch_array($res)){
echo "<option value='".$r['id']."'>".$r['name']."</option>";
}
} }

?>

我想像在 html 示例中那样使用数据库来完成这项工作 http://jsfiddle.net/Nazaret2005/bPUbb/ 、国家、地区和城市。但是我做不到....

如果这对某些人来说很容易。请帮忙做它的工作。谢谢

最佳答案

这是我发现的两个错误..

  1. 将 ajax 类型更改为“GET”,因为您在 url 中发送数据
  2. 更改 test.php $_GET['id'] 替换为 $_POST

在上面进行了更改,让我们看看会发生什么..

或者试试这个..如果你想使用POST

发送数据

如果你想使用post方式发送数据

$(document).ready(function () {
$('#Main').change(function () {
var Sel = $(this).val();
console.log(Sel);
$(".Subselect").hide();
$(".lastSel").hide();
$("#" + Sel).show();
$.ajax({
type: "POST",
url: "test.php",
data: {id: Sel ,part_id: $(this).attr('name'), //part_id = region
success: function(data) {
$(".region").html(data);
}
});
});
$('.Subselect').change(function () {
var subSel = $(this).val();
console.log(subSel);
$(".lastSel").hide();
$("#" + subSel).show();
$.ajax({
type: "POST",
url: "test.php",
data: {id:subSel ,part_id :$(this).attr('name'), //part_id = city
success: function(data) {
$(".city").html(data);
}
});
})
});

在你的 PHP 文件中保持不变

关于php - Jquery AJAX 发布到 PHP 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22551766/

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