使用 jQuery AJAX 发送多个数据参数

Send multiple data parameters with jQuery AJAX

提问人:Maneesh Kumar 提问时间:11/1/2023 最后编辑:RiggsFollyManeesh Kumar 更新时间:11/1/2023 访问量:29

问:

在我的注册表中,种姓选项应根据“母语”和“宗教”显示。有人可以帮助我这样做吗?

<form>
<script type="text/javascript">
function getCaste(){
    var m = document.getElementByID('mToungId').value;
    var r = document.getElementByID('relId').value;
    $.ajax({
        type: "POST",
        url: "getCaste.php",
        data: {
            mtId=m,
            relg_id=r,
        }
        success:function(data){
            $("#castId").html(data);
        }
    })
}
</script>
<!--Mother Tongue-->
<div class="form-floating mb-3">  
<select name="mToungId" id="mToungId" class="form-control" onChange="getCaste();">
<option value disabled selected>Select Mother Tongue</option>
<?php
    $mtquery = mysqli_query($stopu,
                    "select * 
                    from  mothertongue 
                    where status = 'APPROVED' 
                    order by mtongue_name asc");
                                            
    while($mtresult=mysqli_fetch_array($mtquery)){
?>
<option value="<?php echo $mtresult["mtongue_id"]; ?>"><?php echo $mtresult["mtongue_name"]; ?></option>

<?php 
    } 
?>
</select>
<label for="inputState">Mother Tongue</label>
</div>

<!--Relogion-->
<div class="form-floating mb-3"> 
<select name="relId" id="relId" class="form-control" onChange="getCaste(this.value);">
<option value disabled selected>Select Religion</option>
<?php
$relquery = mysqli_query($stopu,
                    "select * 
                    from  religion 
                    where status = 'APPROVED' 
                    order by religion_name asc");
                                        
while($relresult=mysqli_fetch_array($relquery)){
?>
<option value="<?php echo $relresult["religion_id"]; ?>"><?php echo $relresult["religion_name"]; ?></option>

<?php 
} 
?>
</select>
<label for="inputState">Religion</label>
</div>
                            
<!--Caste-->
<div class="form-floating mb-3">
<select name="castId" id="castId" class="form-control">
<option value="">Select Religion First</option>
</select>
<label for="inputEmail">Caste</label>
</div>
</form>
getCaste.php

<?php
include_once('adithara.php');
if(!empty($_POST["relg_id"])){
    $cstquery = mysqli_query($stopu,
                "select * 
                from caste_view 
                where religion_id = '".$_POST["relg_id"]."' 
                and mtongue_id = '".$_POST["mtId"]."' 
                status = 'APPROVED' 
                order by caste_name asc");
?>
<option value disabled selected>Select Caste</option>
<?php 
    while($cstresult=mysqli_fetch_array($cstquery)){
?>
<option value="<?php echo $cstresult["caste_id"]; ?>"><?php echo $cstresult["caste_name"]; ?></option>
        
<?php 
    }
}
?>

我确定错误出在编码中。我想首先选择 Mother Toungue,然后选择 Religion,种姓应该根据这两个值从数据库中显示。

JavaScript PHP 阿贾克斯 MySQLI

评论

0赞 Pointy 11/1/2023
您是否查看过 JavaScript 控制台来检查错误?
1赞 RiggsFolly 11/1/2023
您丢失了,并且在查询中。您是否打开了错误报告AND"select * from caste_view
0赞 RiggsFolly 11/1/2023
好的代码缩进和布局是终生的,而不仅仅是圣诞节,它会帮助我们阅读代码,更重要的是,它将帮助你调试你的代码 为了你自己的利益,快速浏览一下编码标准。您可能会被要求在几周/几个月内修改此代码,您最终会很高兴您花了时间。
1赞 RiggsFolly 11/1/2023
错字:data: { mtId: m, relg_id: r }
0赞 RiggsFolly 11/1/2023
您的脚本容易受到 SQL 注入攻击即使你正在逃避输入,它也不安全!应始终在 或 API 中使用准备好的参数化语句,而不是将用户提供的值连接到查询中。永远不要相信任何用户输入!这也将消除未转义的字符问题,例如在文本字符串中,例如 。MYSQLI_PDO'O'Neal'

答: 暂无答案