2

i want to create a form which has 2 dropdown, the second dropdown will be trigger by the first dropdown without reload the page, after the first dropdown selected/change, so i decide to use ajax

instead trigger the second dropdown it is returning this error

Uncaught SyntaxError: Unexpected token < add_product.php:1

error on above happen if i use google chrome and this the error when i use firefox with firebug


Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /opt/lampp/htdocs/portofolio1/dd-multiple.php on line 15

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /opt/lampp/htdocs/portofolio1/dd-multiple.php on line 16
{"data":null}

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data script.js (line 34, col 26)

this is script.js file

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);
}

this my add_product.php file

<?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>

the problem dissapear when i remove this code onchange="AjaxFunction('provinsi', 'kota')"

and this is my dd-multiple.php in case someone asking for it

<?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);
?>

asked Feb 20, 2015 at 4:07
6
  • Do you have <script> tags in your JavaScript file? If so, that's the problem. Commented Feb 20, 2015 at 4:11
  • No, i didnt have. My .js file is on above Commented Feb 20, 2015 at 4:22
  • Are you sure the file ha the .php extension? Commented Feb 20, 2015 at 4:52
  • Are you sure the file ha the .php extension? - yes im sure Commented Feb 20, 2015 at 6:10
  • @Pointy , i already put newest error i had from firebug on above Commented Feb 20, 2015 at 6:28

1 Answer 1

1

The reason you get that error is because your mysqli_query is incorrect.

You have your link which is null. Your link should be the connection to your database, ie:

$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);
answered Feb 20, 2015 at 6:57
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.