I have two files, one index.php and the other get_content.php, unfortunately I can not show anything on get_content.php, I am confused about where my fault lies either in index.php or get_content.php?
Full Code
Click here
index.php
AJAX :
$("#id_content").click(function() {
var id_content = $("#id_content").val();
$.ajax({
type:"POST",
url:"get_content.php",
data: "id_content="+id_content,
cache:false,
success:function(msg){
$("#result_of_ajax").html(msg);
}
})
})
PHP :
<select id='id_content' name='id_content'>
<option value='ID Content'>Content</option>
<?php
$sql = "select id_content from content where status = 'setuju'";
$hasil = mysqli_query($konek, $sql);
while($data = mysqli_fetch_assoc($hasil)) {
echo "<option value='$data[id_content]'>$data[content]</option>";
}
?>
</select>
and here is get_content.php
get_content.php
echo $_POST['id_content'];
why in get_content.php does not show anything ???
Sorry it is not an answer yet. But I believe you should change your jQuery+AJAX code a bit like:
$("#id_content").on('change', function() {
var id_content = this.value;
$.ajax({
type:"POST",
url:"get_content.php",
data: { id_content: id_content },
cache:false,
success:function(msg){
$("#result_of_ajax").html(msg);
}
})
})
You have done mistake here `echo "<option value='$data[id_content]'>$data[content]</option>";`
Change to
<option value=<?echo $data['id_content'];?>><?echo $data['content'];?></option>
And one more change was there.. check below..
<select id='id_content' name='id_content'>
<option value='ID Content'>Content</option>
<?php
$sql = "select id_content from content where status = 'setuju'";
$hasil = mysqli_query($konek, $sql);
while($data = mysqli_fetch_assoc($hasil))
{?>
<option value=<?echo $data['id_content'];?>><?echo $data['content'];?></option>
<?}
?>
</select>
<br>
<div id="result_of_ajax">
</div>
$("#id_content").on('change', function() {
var id_content = $('#id_content').val();
$.ajax({
type:"GET",
url:"get_content.php?id_content="+id_content,
cache:false,
success:function(msg){
$("#result_of_ajax").html(msg);
}
})
})
get_content.php
<?echo $_GET['id_content'];?>
Related
This is a code with DropDown list which can update the database upon on change, it doesn't work for some reason. I realize that the value $ gp_name didn't send out, can someone help me to fix it?
<select name='status' id='status'>
<option value="<?php echo $status ?>"><?php echo $status ?></option>
<option value="Inquiry">Inquiry</option>
</select>
<input type="hidden" name="gp_name" id="gp_name" value="<?php echo $gp_name;?>" />
<div id="autosavenotify"></div>
<script>
$(document).ready(function() {
var gp_name = $('#gp_name').val();
$('select').on('change', function() {
var statusVal = $(this).val();
var gp_name = $('#gp_name').val();
alert(statusVal,gp_name);
$.ajax({
type: "POST",
url: "save.php",
data: {
statusType: statusVal,
gp_name: gp_name
},
success: function(msg) {
$('#autosavenotify').text(msg);
}
})
});
});
</script>
save.php
<?php
require_once("connMysql.php");
$gp_name=$_POST['gp_name'];
$st=$_POST['statusType'];
$qry ="UPDATE lotus_gp SET status='".$st."' where gp_name='".$gp_name."'";
$done = mysql_query($qry);
if($done)
{
echo $gp_name;
echo $st;
}
?>
First, check if the data is getting from your front-end sides on your PHP page.
if(isset($_POST['gp_name']) && isset($_POST['statusType'])){
$gpname = $_POST['gp_name']; // echo it to ensure
$satype = $_POST['statusType']; // echo it to ensure
}
If the data is not printed then you might have an issue with inputs.
This code comes from a guide I've seen in YouTube.
I'm trying to populate the second dropdown based on the choices on the first dropdown, in my test page its working, however when I tried to attach it to my main page, the second dropdown is not turning into a dropdown.
I've tried to re-code it, but still the problem persist.
This is for the AJAX
<?php
include('db.php');
if($_POST['id']){
$id=$_POST['id'];
if($id==0){
echo "<option value='0'>Select Type</option>";
}else{
$sql = mysqli_query($con,"SELECT * FROM `ConcernType` WHERE Concern_Id='$id'");
while($row = mysqli_fetch_array($sql)){
echo '<option value="'.$row['ConcernType_id'].'">'.$row['ConcernType_name'].'</option>';
}
}
}
?>
This is for the index.php
<label>Concern Category :</label><select name="concerncategory" class="concerncategory">
<option value="0">Select Category</option>
<?php
include('db.php');
$sql = mysqli_query($con,"select * from ConcernCategory");
while($row=mysqli_fetch_array($sql))
{
echo '<option value="'.$row['Concern_Id'].'">'.$row['ConcernCategory_name'].'</option>';
} ?>
</select><br/><br/>
<label>Concern Type :</label><select name="concerntype" class="concerntype">
<option value="0">Select Type</option>
</select>
<br /><br />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".concerncategory").change(function()
{
var Concern_Id=$(this).val();
var post_id = 'id='+ Concern_Id;
$.ajax ({
type: "POST", url: "ajax.php", data: post_id, cache: false,
success: function(Type) {
$(".concerntype").html(Type);
} }); }); });
</script>
Here are some screenshots.
https://ibb.co/2NTTdG8
https://ibb.co/fFXBzFS
I'm experiencing trouble with 2 level dependent dropdown.
When I select 1st dropdown, i have a PARSERERROR alert and second dropdown is not pouplated
Please, any advice about what's wrong?
Here is my code:
HTML
<div class="form-group">
<label>CLASSIFICATION<span class="text-danger">*</span></label>
<select name="document_classification" id="document_classification" class="select-search" data-placeholder="">
<?php
$sql = "SELECT * FROM rm_document_classification_id";
$result = $mysqli->query($sql);
while($row = $result->fetch_assoc()){
echo "<option></option>";
echo "<option value='".$row['classification_id']."'>".$row['classification_description']."</option>";
}
?>
</select>
</div>
<div class="form-group">
<label>SUBJECTS<span class="text-danger">*</span></label>
<select name="document_subject" class="form-control" style="width:350px">
</select>
</div>
SCRIPT
$( "select[name='document_classification']" ).change(function () {
var class_id = $(this).val();
//alert(class_id);
if(class_id) {
$.ajax({
url: "ajax.php",
type: "GET",
dataType: 'json',
data: {'classification_id':class_id},
success: function(data) {
$('select[name="document_subject"]').empty();
$.each(data, function(key, value) {
$('select[name="document_subject"]').append('<option value="'+ key +'">'+ value +'</option>');
});
},
error: function (request,status, error) {
console.log(error);
alert(status);
}
});
}else{
$('select[name="document_subject"]').empty();
}
});
PHP
<?
include_once "../config.php";
$sql = "SELECT * FROM rm_document_subject_id
WHERE subject_classification_id LIKE '%".$_GET['classification_id']."%'";
$result = $mysqli->query($sql);
$json = [];
while($row = $result->fetch_assoc()){
$json[$row['subject_id']] = $row['subject_description'];
}
echo json_encode($json);
?>
Thanks all. HID
Thanks all. HID
Thanks all. HID
Please, remove in your php file:
$json = [];
The title says my goal here. I'm trying to change the second select tag's values on change of the first tag. Here is what I've tried. The values are coming from the database. So this will involve some php selects.
<div class="row">
<div class="col-md-5">
<label for="project">From Project</label>
<select class="form-control"id="project"onchange="dropDrown(this.value)" name="project">
<?php
$sql = $db->prepare("SELECT * FROM tbl_project WHERE projectStatus = 1");
$sql->execute();
while($result=$sql->fetch(PDO::FETCH_ASSOC)){
$value = $result['projectID'];
$projectName = $result['projectName'];
echo"
<option value='$value'> $projectName </option>
";
}
?>
</select>
</div>
</div>
<div class="row">
<div class="col-md-5">
<select class="form-control" id="village" name="village"></select>
</div>
</div>
The AJAX:
<script type="text/javascript">
function dropDown(id){
var theID = id;
// assign your data to a varaible
var dataString= {theID:id};
$.ajax({
url: "includes/getVillage.php",
type: "POST",
data: dataString,
cache: false,
success: function (data){
$("#village").html(data);
}
});
}
getVillage.php
<?php
include '../../connection';
$village = $_POST['theID'];
$sql = "SELECT * FROM tbl_village WHERE projectID = '$village'";
$query = $db->prepare($sql);
$results = $query->execute();
while($results=$sql->fetch(PDO::FETCH_ASSOC)){
$value = $results['villageID'];
$text = $results['villageName'];
echo "<option value'$value'>$text</option>";
}
It seems you are not passing data through your ajax call.
function dropDown(id){
var theID = id;
// assign your data to a varaible
var dataString= {theID:id};
$.ajax({
url: "includes/getVillage.php",
type: "POST",
data: dataString,
cache: false,
success: function (data){
$("#village").html(data);
}
});
}
Alternatively you can pass the values in the below format
var dataString= "theID="+id;
It turns out that I made a wrong connection. After evaluating the getVillage.php file alone. I received a lot of errors regarding my connection.php. Thanks guys.
I am trying to create 3 drop down boxes dependent on each other. Each drop down box is getting its data from its own tables. There are 3 tables as shown:
This is the form:
<label for="tourtype">
Tour Type
</label>
<select id="tourtype" name="tourtype" required>
<option value="" selected="selected">
--Select--
</option>
<?php
$sql=mysql_query("Select tour_type_id,tour_name from tour_type");
while($row=mysql_fetch_array($sql))
{
$tour_type_id=$row['tour_type_id'];
$name=$row['tour_name'];
echo "<option value='$tour_type_id'>$name</option>";
}
?>
</select>
<label>
Country
</label>
<select id="country" name="country" class="country" required>
<option value="" selected="selected">
-- Select --
</option>
</select>
<
<label>
Destination
</label>
<select id="destination" name="destination" class="destination" required>
<option value="" selected="selected">
-- Select --
</option>
</select>
This is the javascript:
<script type="text/javascript">
$('#tour_type').change(function () {
var id = $(this).val();
$.ajax({
type: "POST",
url: "ajax.php",
data: "&id=" + id + "&get_countries=1",
success: function (html) {
$("#country").html(html);
}
});
});
$('#country').change(function () {
var id = $(this).val();
$.ajax({
type: "POST",
url: "ajax.php",
data: "&id=" + id + "&get_destination=1",
success: function (html) {
$("#destination").html(html);
}
});
});
</script>
And this is the ajax.php
<?php
include ('../config.php');
< ? php
if ($_REQUEST['get_countries']) {
$sql = mysql_query("SELECT * FROM `countries` where `tour_type_id`=" . $_REQUEST['id']);
$countries = "";
while ($row = mysql_fetch_array($sql)) {
$cid = $row['countries_id'];
$name = $row['countries_name'];
$countries.= "<option value='" . $cid . "'>" . $name . "</option>";
}
echo $countries;
}
elseif ($_REQUEST['get_destination']) {
$destination = "";
$sql = mysql_query("SELECT * FROM `destination` where `country_id` =" . $_REQUEST['id'])
while ($row = mysql_fetch_array($sql)) {
$destination_id = $row['destination_id'];
$name = $row['destination_name'];
$destination.= "<option value='" . $destination_id . "'>" . $name . "</option>";
}
echo $destination;
}
?>
The problem is the 2nd and 3rd drop down boxes are not populating anything. Can anyone help me? For example if i select culture on the 1st drop down, the 2nd drop down should show Holland and Belgium. Then if i select Holland, the 3rd drop down should show Amsterdam.
Your identifier in your Javascript is #tour_type when your id is #tourtype.
If the syntax is correct and your SQL results are correct too, it should work.
EDIT: some of your JS isn't right.
data: "&id=" + id + "&get_countries=1",
should be
data: {id: id, get_countries: 1},
You should also put a debug on your ajax call by adding
error: function () { alert("ajax failed"); }
after your success callback
full sources now:
$('#tourtype').change(function() {
var id=$(this).val();
$.ajax
({
type: "POST",
url:"jurassicbase5/admin/ajax.php",
data: {id: id, get_countries: 1},
success: function(html){
$("#country").empty();
$("#country").append(html);
},
error: function () { alert("ajax failed"); }
});
});
$('#country').change(function() {
var id=$(this).val();
$.ajax
({
type: "POST",
url: "jurassicbase5/admin/ajax.php",
data: {id: id, get_destination: 1},
success: function(html)
{
$("#destination").empty();
$("#destination").append(html);
},
error: function () { alert("ajax failed"); }
});
});