我尝试了很多解决方案,但仍然是ajax调用无效。 它没有显示控制台错误或任何错误。我尝试了$ _POST和$ _REQUEST
HTML
<div class="form-group">
<label for="usernamee" class="col-sm-2">Select Customer :</label>
<div class="col-sm-10">
<select class="form-control select2" required="" name="uid" id="customer_name">
<?php
$getcust = $conn->query("SELECT id,full_name FROM `data_entry`");
while ($fetch1 = $getcust->fetch_array()) {
?>
<option value="<?=$fetch1[0]?>"><?=$fetch1[1]?></option>
<?php } ?>
</select>
</div>
</div>
Ajax Call
$("#customer_name").change(function(e) {
e.preventDefault();
var idd = $('#customer_name').find(":selected").val();
var data = 'idd=' + $('#customer_name').find(":selected").val();
//alert(data)
$.ajax({
type : "POST",
url : "ajax_get_subid.php",
//data : {idd:idd},
data : data,
success : function(data) {
alert(data);
}
});
e.preventDefault();
});
ajax调用位于$(document).ready(function(){()}
内。 Jquery版本是2.2.3
和PHP
<?php
if(isset($_REQUEST['idd'])){
$id = $_REQUEST['idd'];
echo $id;
}
?>
在我的文件JS中
<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="../plugins/input-image-display/fileinput.min.js" type="text/javascript"></script>
<script src="../dist/js/jquery-ui.min.js"></script>
<script>
$.widget.bridge('uibutton', $.ui.button);
</script>
<script src="../bootstrap/js/bootstrap.min.js"></script>
<script src="../plugins/select2/select2.full.min.js"></script>
<script src="../dist/js/moment.min.js"></script>
<script src="../plugins/daterangepicker/daterangepicker.js"></script>
<script src="../plugins/datepicker/bootstrap-datepicker.js"></script>
<script src="../plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js"></script>
<script src="../plugins/slimScroll/jquery.slimscroll.min.js"></script>
<script src="../plugins/fastclick/fastclick.js"></script>
<script src="../dist/js/app.min.js"></script>
<script src="../dist/js/demo.js"></script>
是冲突吗?
答案 0 :(得分:0)
如果你使用$ .getJSON或$ .get / $。post可能会更容易,在这种情况下,我认为$ .getJSON是一个最好的选择,特别是对于未来的代码更改。
JS代码: /scripts/main.js
function getData(requestID, callback){
$.getJSON("/api/getData.php",{id:requestID},callback);
}
function customerName_change(){
debugger;
getData(12345,function(result){
debugger;
console.log(result);
alert(result.ID);
});
}
$(document).ready(function(){
$('#customer_name').on('change',customerName_change);
});
Php代码: /api/getData.php
<?php
class ApiResult {
public $ID;
public $Extra;
}
$res = new ApiResult();
$res->ID = 234124;
$res->Extra =$_REQUEST['id'];
echo json_encode($res);
?>
答案 1 :(得分:0)
由于您使用的是select2
,因此您需要像这样使用它:
$("#customer_name").on('select2:select', function(e) {
e.preventDefault();
var selectedVal = $(this).val();
$.ajax({
type: "POST",
url: "ajax_get_subid.php",
data: {idd: selectedVal},
success: function(response) {
alert(response);
}
});
});
答案 2 :(得分:0)
$("#customer_name").change(function(e) {
e.preventDefault();
var id = $('#customer_name').find(":selected").val();
$.ajax({
type : "POST",
url : "ajax_get_subid.php",
data : {id:id},
dataType:'json',
success : function(data) {
alert(data);
}
});
e.preventDefault();
});
你的PHP:
if(isset($_POST['id'])){
$id = $_POST['id'];
echo json_encode($id);
}