我想创建一个有2个下拉列表的表单,第二个下拉列表将在没有重新加载页面的情况下由第一个下拉列表触发,在第一个下拉列表选中/更改后,所以我决定使用ajax
而是触发第二个下拉列表,它返回此错误
如果我使用谷歌浏览器,未捕获的SyntaxError:意外的令牌< add_product.php:1
错误就会发生 这个错误当我使用firefox与firebug
警告:mysqli_query()要求参数1为mysqli,在 /opt/lampp/htdocs/portofolio1/dd-multiple.php 15 <中给出null / b>
警告:mysqli_fetch_assoc()要求参数1为mysqli_result,在 /opt/lampp/htdocs/portofolio1/dd-multiple.php 16 < / b>
{&#34;数据&#34;:空}SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符 script.js(第34行,第26栏)
这是script.js文件
function AjaxFunction(id1,id2)
{
alert(id1);
var httpxml;
try
{
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
function stateck(){
if(httpxml.readyState===4)
{
var myarray = JSON.parse(httpxml.responseText);
// Before adding new we must remove previously loaded elements
for(j=document.getElementById(id2).length-1;j>=0;j--)
{
document.getElementById(id2).remove(j);
}
for (i=0;i<myarray.data.length;i++)
{
var optn = document.createElement("OPTION");
optn.text = myarray.data[i].subcategory;
//optn.value = myarray.data[i].subcat_id; // You can change this to subcategory
optn.value = myarray.data[i].subcategory;
document.getElementById(id2).options.add(optn);
}
}
}
var str='';
var s1ar=document.getElementById(id1);
for (i=0;i< s1ar.length;i++) {
if(s1ar[i].selected){
str += s1ar[i].value + ',';
}
}
//alert (s1ar);
var str=str.slice(0,str.length -1); // remove the last coma from string
//alert(str);
/////
//alert(str);
var url="dd-multiple.php";
url=url+"?cat_id="+str;
url=url+"&sid="+Math.random();
//alert(url);
httpxml.onreadystatechange=stateck;
httpxml.open("GET",url,true);
httpxml.send(null);
}
&#13;
这是我的add_product.php文件
<?php
require_once './model/functions.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link href="css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body>
<div class="container">
<div class="row">
<div class="center-block" style="width: 130px;">
<h3><strong>Add Book</strong></h3>
</div>
</div>
<form method="post" action="" class="form-horizontal">
<div class="form-group">
<label for="kategori" class="col-lg-4 control-label">Kategori : </label>
<div class="col-lg-6">
<input type="text" class="form-control" id="kategori" name="kategori" placeholder="pilih kategori">
</div>
</div>
<div class="form-group">
<label for="tipeIklan" class="col-lg-4 control-label">Tipe Iklan : </label>
<div class="col-lg-6">
<label class="radio-inline">
<input type="radio" name="genderRadioOptions" id="tipeIklan" value=1>Dicari
</label>
<label class="radio-inline">
<input type="radio" name="genderRadioOptions" id="tipeIklan" value=0>Dijual
</label>
<label class="radio-inline">
<input type="radio" name="genderRadioOptions" id="tipeIklan" value=2>Disewakan
</label>
<label class="radio-inline">
<input type="radio" name="genderRadioOptions" id="tipeIklan" value=3>Jasa
</label>
</div>
</div>
<div class="form-group">
<label for="judul" class="col-lg-4 control-label">Judul : </label>
<div class="col-lg-6">
<input type="text" class="form-control" id="judul" name="judul" placeholder="Judul iklan anda">
</div>
</div>
<div class="form-group">
<label for="deskripsi" class="col-lg-4 control-label">Deskripsi : </label>
<div class="col-lg-6">
<textarea id="deskripsi" class="form-control" rows="5"></textarea>
</div>
</div>
<div class="form-group">
<label for="harga" class="col-lg-4 control-label">Harga(Rp.) : </label>
<div class="col-lg-3">
<input type="text" class="form-control" id="harga" name="harga" aria-describedby="helpBlock" placeholder="cukup tuliskan angka">
<span id="helpBlock" class="help-block">Contoh: 2000</span>
</div>
</div>
<div class="form-group">
<label for="kondisi" class="col-lg-4 control-label">Kondisi : </label>
<div class="col-lg-6">
<label class="radio-inline">
<input type="radio" name="genderRadioOptions" id="kondisi" value=0>Bekas
</label>
<label class="radio-inline">
<input type="radio" name="genderRadioOptions" id="kondisi" value=1>Baru
</label>
</div>
</div>
<div class="form-group">
<label for="provinsi" class="col-lg-4 control-label">Provinsi : </label>
<div class="col-lg-3">
<select id="provinsi" name="s1[]" class="form-control" onchange="AjaxFunction('provinsi', 'kota')">
<option>select one</option>
<?php
$provinsi_set = find_all_province();
while($provinsi = mysqli_fetch_assoc($provinsi_set)){
?>
<option value="<?php echo $provinsi["id"];?>"><?php echo $provinsi["nama"]; ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="kota" class="col-lg-4 control-label">Kota : </label>
<div class="col-lg-3">
<select id="kota" name="s2[]" class="form-control">
</select>
</div>
</div>
<div class="form-group">
<label for="foto" class="col-lg-4 control-label">Upload Foto : </label>
<div class="col-lg-6">
<img src="uploads/raditya.jpg" alt="" width="140" height="140" class="img-thumbnail">
<img src="uploads/Kambing_Jantan_buku_2.jpg" alt="" width="140" height="140" class="img-thumbnail">
<img src="uploads/raditya.jpg" alt="" width="140" height="140" class="img-thumbnail">
</div>
</div>
<div class="col-lg-offset-4">
<button type="button" id="registrationbutton" class="btn btn-default">Tayangkan!</button>
</div>
</form>
</div>
<script src="jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/script.js"></script>
<?php close_connection(); ?>
</body>
</html>
&#13;
当我删除此代码onchange="AjaxFunction('provinsi', 'kota')"
这是我的dd-multiple.php,以防有人要求
<?php
@$cat_id=$_GET['cat_id'];
$mn=split(",",$cat_id); // creating array
while (list ($key, $val) = each ($mn)) {
if(!is_numeric($val)){ // checking each element
echo "Data Error ";
exit;
}
}
global $id_mysql;
$query = "SELECT nama,id FROM KOTA WHERE id_prov IN ($cat_id)";
$row = mysqli_query($id_mysql, $query);
$result = mysqli_fetch_assoc($row);
$main = array('data'=>$result);
echo json_encode($main);
?>
&#13;
答案 0 :(得分:1)
您收到该错误的原因是您的mysqli_query
不正确。
您的link
为空。您的link
应该是与数据库的连接,即:
$link = mysqli_connect("localhost", "my_user", "my_password", "database");
$query = "SELECT nama,id FROM KOTA WHERE id_prov IN ($cat_id)";
$row = mysqli_query($link, $query);
$result = mysqli_fetch_assoc($row);
$main = array('data'=>$result);
echo json_encode($main);