简体   繁体   English

如何在PHP中使用ajax jquery将列表框值发送到另一个列表框而不刷新页面

[英]How can send one list box value to another list box without refreshing page using ajax jquery in php

When i select Course in Course listbox automatically load Related Course levels in Course Level Listbox. 当我在“课程列表”框中选择“课程”时,会自动在“课程级别列表”框中加载“相关课程”级别。

  <script type="text/javascript" src="js/jquery.js"></script>
<div>Course:<select name="course_id" id="course_id" class="list_box">
<option name="" value="">Select</option>
<?php 
$course_sql=mysql_query("select * from tbl_course_master");
while($course=mysql_fetch_array($course_sql))
{
?>
<option value="<?php echo $course['id'];?>" <?php if($item_fetch['course_id']==$course['id']) { echo "selected='selected'"; } ?> ><?php echo $course['course'];?></option>
<?php } ?>
</select>
</div>


<div> Course Level:
<select name="course_level_id" id="course_level_id" class="list_box">
<option name="" value="">Select</option>
<?php 
$course_level_sql=mysql_query("select * from tbl_course_level where course_id='$_GET[course_id]'") or die(mysql_error());
while($course_level=mysql_fetch_array($course_level_sql))
{
?>
<option value="<?php echo $course_level['id'];?>" <?php if($item_fetch['course_level_id']==$course_level['id']) { echo "selected='selected'"; } ?> ><?php echo $course_level['level'];?></option>
<?php } ?>
</select>
</div>

  Here the ajax code what iam using is
    <script>
$("#course_id").live("change",function(){
var course_id=$(this).val();
//alert(course_id);
$.ajax({
            url:"add-itemmaster.php",
            type: 'GET',
            data:{course_id: course_id},
            success:function(data){
            window.location.href=link;
            }

        }); // end ajax
});
</script>

Check this ajax code and let me know this is correct or not or add new ajax code 检查此ajax代码,并让我知道这是正确的还是添加新的ajax代码

Please help me how to load selected Course in Course listbox automatically load Related Course levels in Course Level Listbox. 请帮助我如何在“课程列表”框中加载选定的课程,并自动在“课程级别列表”框中加载相关课程级别。 Please help 请帮忙

Your ajax is correct. 你的ajax是正确的。 But instead of reloading page(window.location.href) in success add tag with respective values dynamically in success function itself. 但是,与其成功地重新加载page(window.location.href),不如在成功函数本身中动态添加带有相应值的标记。

I am assuming that you are fetching data in your function/file "add-itemmaster.php" and returning respective data 我假设您正在获取函数/文件“ add-itemmaster.php”中的数据并返回各自的数据

$.ajax({
   url:"add-itemmaster.php",
   type: 'GET',
   data:{course_id: course_id},
   success:function(data){
        var item_fetch_id = $('#item_fetch_id').attr('rel');
        for(var i in data) {
            if(item_fetch_id  == data[i].course_level_id) {
                $('#course_level_id').append('<option value=' + data[i].course_level_id + ' selected>' + data[i].course_level + '</option>');
            } else {
                $('#course_level_id').append('<option value=' + data[i].course_level_id + '>' + data[i].course_level + '</option>');
            }
        }
   }
});

Also you have to add one hidden element in your DOM with id as "item_fetch_id" to store item fetched course id 另外,您还必须在DOM中添加一个ID为“ item_fetch_id”的隐藏元素,以存储获取的课程ID

like this: 像这样:

<input type="hidden" id="item_fetch_id" rel="<?php echo $item_fetch['course_id'];?>">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用AJAX在PHP中将选择框值作为数组从一个页面传递到另一页面? - How to pass the select box value as an array using AJAX from one page to another in PHP? 如何使用jQuery设置List框的值? - How can I set the value of a List box using jQuery? 如何使用Ajax从列表框中发送选定的值 - How to send the selected values from list box using ajax 在另一个JQuery的一个列表框中删除重复项 - Remove duplicates in one list box in another JQuery 如何在 JQuery 中禁用从一个列表框到另一个列表框的选定选项? - How to disable selected options from one list-box to another list box in JQuery? 使用javascript或PHP,如何使用下拉列表或文本框中的值。 - Using javascript or PHP how can I use the value from a drop down list or a text box. 我如何在没有页面刷新的情况下使用 ajax/jquery 在我的弹出表单框中显示 php 错误消息 - How do i showing php error message on my popup form box with ajax/jquery without page refresh 如何在数据表上添加复选框选择并将所有值作为数组发送到另一个 html/php 页面? - How to add check box selection on datatable and send all value as array to another html/php page? 在MVC 4剃须刀中一个接一个地选中列表中的每个复选框后如何调用Ajax函数 - How can I call Ajax function after check each check box in a list one by one in mvc 4 razor 如何在不刷新页面的情况下使用jquery ajax请求通过浏览器url传递参数值 - how to pass parameter value through browser url using jquery ajax request without refreshing page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM