I'm stuck with this problem with in a week. I tried to set a onkeyup on datalist, only allow to submit within datalist autocomplete option value. I tried 2 different script but it has the same problem. When my option value is came via ajax, my textbox not allowing to type even if the value is in the option list. Why it is? Help please. I'm stuck with this :/
When I tried to echo the option list just like this
<input list="languages" id="none"></input>
<datalist id="languages" name="options">
<option value=""></option>
<?php echo $option1; ?>
</datalist>
the onkeyup works well. But when the value is came from ajax, the problem comes in. Why is that? Please help me with this.
index.php
Drop1
<?php
$mysqli = new mysqli("localhost", "root", "", "2015");
$combo = $mysqli->query("SELECT * FROM category GROUP BY cat_code ORDER BY id");
$option = '';
while($row = $combo->fetch_assoc())
{
$option .= '<option value = "'.$row['category'].'">'.$row['category'].'</option>';
}
?>
<select id="main" name="main">
<option value="" disabled="disabled" selected="selected">Choose</option>
<?php echo $option; ?>
</select>
<span id="result"> <input list="languages" id="none"></input>
<datalist id="languages" name="options">
<option value=""></option>
</datalist></span>
<input type="submit" name="submit" value="Submit"/>
<script type="text/javascript">
$('#main').change(function () {
$.ajax({
url: 'getajax.php',
data: {
mainlist_id: $(this).val()
},
dataType: 'html',
type: 'POST',
success: function (data) {
$('#languages').html(data);
}
});
});
</script>
<script>
$(document).ready(function() {
var validOptions =[];
$("#languages option").each(function(){
validOptions.push($(this).val())
});
$("#none").autocomplete(validOptions, { mustMatch: true });
});
$('input#none').result(function(event, data, formatted) {
$("#result").html(!validOptions ? "No match found!" : "Selected: " + formatted);
}).keyup(function() {
$(this).search();
$(this).css("background-color", "#D6D6FF");
});
</script>
Ajax
<?php
if (isset($_POST["mainlist_id"])) {
$mysqli = new mysqli("localhost", "root", "", "2015");
$main = $mysqli->real_escape_string($_POST["mainlist_id"]);
$result1 = $mysqli->query("SELECT * FROM code WHERE cat_code='$main' GROUP BY item_code ORDER BY item");
$option1 = '';
while($row = $result1->fetch_assoc())
{
$option1 .= '<option value = "'.$row['item'].'">'.$row['item'].'</option>';
}
echo $option1;
}
?>
Try this:
success: function (data) {
$('#languages').html(data);
$("#languages option").each(function(){
validOptions.push($(this).val())
});
}
Related
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 am trying to get the price of Item after selecting the item in a dropdown. I have used ajax calls but my code is not going inside $.ajax({.... What is wrong with my code?
I have used alert in the php file but nothing displays, which means my code is not calling php file.
<script type='text/javascript'>
$("#puja_name2").on('change',function()
{
var id=$(this).val();
var data = 'id='+ id;
$.ajax({
type: "POST",
url: "../ajax_price.php",
data: data,
cache: false,
success: function(html)
{
$("#puja_price2").html(data);
}
});
});
</script>
<div class="form-group">
<label>
Puja Name<span class="font-red">* </span>:
</label>
<select class="form-control" name="puja_name2" id="puja_name2" data-validetta="required">
<option value="">Select Puja Name</option>
<?php
$SQL_STATEMENT_puja = $DatabaseCo->dbLink->query("SELECT * FROM puja_type ");
while($DatabaseCo->dbRow = mysqli_fetch_object($SQL_STATEMENT_puja)){
?>
<option value="<?php echo $DatabaseCo->dbRow->puja_id; ?>" ><?php echo $DatabaseCo->dbRow->puja_name; ?></option>
<?php
}
?>
</select>
</div>
<div class="form-group">
<label> Price <span class="font-red">* </span>:</label>
<select id="puja_price2" >
</select>
</div>
You must change in jquery puja_name to puja_id:
$("#puja_id").on('change',function()
A couple things wrong with your code bud:
1) You have
$(document).ready(function(){
<script>
//code
</script>
}
What your code should look like is:
<script type="text/javascript">
$(document).ready(function() {
//code
});
</script>
If you need to add comments in your JS code, DO NOT use <!-- -->. Instead use // like you see in my code above.
2) You need to change in your jquery puja_name to puja_id because your <select></select> id is puja_id so:
this
$("#puja_name").on('change',function() {
turns to this
$("#puja_id").on('change',function() {
3) I'm not the greatest at SQL statements but I do feel that using the code below would help you even more (you don't HAVE to).
<?php
$SQL_STATEMENT_puja = "SELECT * FROM puja_type WHERE status='APPROVED' ORDER BY puja_name ASC";
$DatabaseCo = $conn->query($SQL_STATEMENT_puja);
$puja=$row['puja']; //This should actually be $puja=$row['puja_id'];
while($row = $DatabaseCo->fetch_assoc()) {
?>
<option value="<?php echo $row['puja_id']; ?>" <?php if($row['puja_id']==$puja) { echo "selected"; } ?>><?php echo $row['>puja_name']; ?></option>
<?php
}
?>
4) In your JQuery you have it as $("#puja_price").html(html); it should be $("#puja_price").html(data);
5) I also see that you have it as #puja_price but there's nothing in your html that has the id of puja_price so
I recommend changing this
$("#puja_price").html(html); to
$("#puja_price2").html(data);
6) Your JQuery code is
$.ajax
({
When it should be: $.ajax({
Full code:
<div class="form-group">
<label>
Puja Name<span class="font-red">* </span>:
</label>
<select class="form-control" name="puja_id" id="puja_id" data-validetta="required">
<option value="">Select Puja Name</option>
<?php
//If you already have the connection setup you don't need to add this
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$SQL_STATEMENT_puja = "SELECT * FROM puja_type WHERE status='APPROVED' ORDER BY puja_name ASC";
$DatabaseCo = $conn->query($SQL_STATEMENT_puja);
$puja=$row['puja'];
while($row = $DatabaseCo->fetch_assoc()) {
?>
<option value="<?php echo $row['puja_id']; ?>" <?php if($row['puja_id']==$puja) { echo "selected"; } ?>><?php echo $row['>puja_name']; ?></option>
<?php
}
?>
</select>
</div>
<div class="form-group">
<label>
Price <span class="font-red">* </span>:
</label>
<input type="text" class="form-control " id="puja_price2" name="puja_price2" disabled>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#puja_id").on('change',function() {
var id=$(this).val();
var data = 'id='+ id;
$.ajax({
type: "POST",
url: "../ajax_price.php",
cache: false,
data: data,
success: function(html) {
$("#puja_price2").html(data);
}
});
});
});
</script>
Let me know if the changes work for you
Try this in your JS code.
<script src="jquery.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
$('#puja_id').change(function(){
var id = $(this).val();
var data = 'id='+id;
$.ajax({
type: "POST",
url: "../ajax_price.php",
data: data,
cache: false,
success:function(res){
$('#puja_price2').html(res);
}
});
});
});
</script>
I have a drop menu that includes some category every category has their own subcategory i want to show them buy selecting category name
but it's not working, did i miss something or am i doing it completely wrong?
<script type="text/javascript">
$(function() {
$("#error").hide();
$("#category").change(function(){
$("#error").hide();
var category = $("#category").val();
if (category == "") {
$("#error").show();
return false;
}
var data = $("#form").serialize();
$.ajax({
type:"POST",
url:"index.php",
data:data,
success: function(){
}
});
return false;
});
});
</script>
<form id="form" name="form">
<label for="category" id="error">Empty</label>
<select name="category" id="category">
<option></option>
<option value="News">News</option>
<option value="Items">Items</option>
<option value="Updates">Updates</option>
</select>
</form>
<?php
include("connect.php");
if(!empty($_POST['category'])){
$sql=$con->prepare("SELECT * FROM categorys WHERE category=:category ");
$sql->bindparam(":category",$_POST['category']);
$sql->execute();
while($r=$sql->fetch()){
echo $r['subcategory'];
}
}
?>
SomePage.php
<form id="form" name="form">
<div id='category'>
<label for="category" id="error">Empty</label>
<select name="category" id="category">
<option></option>
<option value="News">News</option>
<option value="Items">Items</option>
<option value="Updates">Updates</option>
</select>
</div>
<div id='subcategory'>
</div>
</form>
<script>
$('#category').change(function(){
var CatName= $('#category').val();
$.ajax({url:"AjaxSelectCategory.php?CatName="+CatName,cache:false,success:function(result){
$('#subcategory').html(result);
}});
});
</script>
Create New Page AjaxSelectCategory.php
[NOTE: If you want to change this page name. Change in <script></script> tag too. Both are related.]
<?php
include("connect.php");
if(!empty($_GET['CatName']))
{
$sql=$con->prepare("SELECT * FROM categorys WHERE category=:category ");
$sql->bindparam(":category",$_GET['CatName']);
$sql->execute();
?>
<select name='subcategory'>
<?php
while($r=$sql->fetch())
{?>
<option value="<?php echo $r['subcategory'];?>"><?php echo $r['subcategory'];?></option>
</select>
<?php }
}?>
Try like this,
$.ajax({
type:"POST",
url:"get_subcategory.php",
data:data,
success: function(data){
alert(data)// this will have the second dropdown. add to desired place in your view.
}
});
In get_subcategory.php
$sql = "Select * from table_name where catregory = ".$_POST'category'];
// execute the query.
$sub = "<select name='sub-category'>";
foreach(result from database as $row) {
$sub .= "<option value='$row->id'>$row->name</option>";
}
$sub .= "</select>";
echo $sub;
I have here a dropdown select and autocomplete function. What I need to do is to pass the selected value of dropdown to autocomplete.php to use in my query. Textbox value should depending on value from dropdown. If selected value is supplies, all supplies only value in textbox (like pencil or ballpen).
I used this Ajax in Dynamic Dropdown. How I can use this to pass the value in autocomplete.php?
Note: this Ajax was not connected in my autocomplete function. How I can use this ajax to pass the value to my autocomplete.php query.
<script type="text/javascript">
$('#main').change(function(){
$.ajax({
url : 'getajax.php',
data :{mainlist_id : $(this).val()},
dataType:'html',
type:'POST',
success:function(data){
$('#sub').html(data);
}
});
});
</script>
Ajax.php
<script type="text/javascript" src="jquery.autocomplete.js"></script>
<script>
$(document).ready(function(){
$("#tag").autocomplete("autocomplete.php", {
selectFirst: true
});
});
</script>
Drop1
<?php
$combo = $mysqli->query("SELECT * FROM category GROUP BY cat_code ORDER BY id");
$option = '';
while($row = $combo->fetch_assoc())
{
$option .= '<option value = "'.$row['cat_code'].'">'.$row['category'].'</option>';
}
?>
<select id="main" name="main">
<option value="" disabled="disabled" selected="selected">Choose</option>
<?php echo $option; ?>
</select>
Auto Complete <input id="tag" type="text">
Autocomplete.php
<?php
$mysqli = new mysqli("localhost", "root", "", "2015") or die("Database Error");
$auto = $mysqli->real_escape_string($_GET["q"]);
$sql = $mysqli->query("SELECT * FROM code WHERE item LIKE '%$auto%' GROUP BY id ORDER BY item" );
if($sql)
{
while($row=mysqli_fetch_array($sql))
{
echo $row['item']."\n";
}
}
?>
$('#sub').html(data); should be $('#tag').html(data);
You have not defined #sub element in your html code, when placing this
$('#sub').html(data);
After that your name of the page must be same, while your using getajax.php, and its not you have defined us
I am using a JS/Ajax function to echo Selected values from select boxes. I am populating these select boxes with values from MySQL DB. The function works with text input fields but now that I am using it with these select boxes I am getting no response.
Can I echo the selected value from a select box with the JS? or Is there a better way? EXAMPLE
JS
<script>
$(document).ready(function() {
var timer = null;
var dataString;
function submitForm(){
$.ajax({ type: "POST",
url: "index.php",
dataType: 'json',
success: function(result){
$('#special').html('<p>' + $('#resultval', result).html() + '</p>');}
});
return false;
}
$('#category_form').on('change', function() {
clearTimeout(timer);
timer = setTimeout(submitForm, 2000);
});
});
</script>
PHP/HTML
try {
$pdo = get_database_connection();
$sql = "SELECT *
FROM `categories`
WHERE `master_id` = 0";
$statement = $pdo->query($sql);
$list = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo 'There was a problem';
}
<form action="" method="post" id="category_form'" name="category_form'">
<select name="main" id="main" size="7" class="update">
<option value="">Select one</option>
<?php if (!empty($list)) { ?>
<?php foreach($list as $row) { ?>
<option value="<?php echo $row['id']; ?>">
<?php echo $row['name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
<select name="subc1" id="subc1" size="7" class="update"
disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc2" id="subc2" size="7" class="update"
disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc3" id="subc3" size="7" class="update"
disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
</form>
<div id="special"></div>
It looks like you're selecting the wrong element with jQuery.
Change:
$('#category_form').on('change', function() {
clearTimeout(timer);
timer = setTimeout(submitForm, 2000);
});
To:
$('#main').on('change', function() {
clearTimeout(timer);
timer = setTimeout(submitForm, 2000);
});