Jquery Ajax with GET not working - php

I have this peace of HTML code
<select id="country" name="country" onchange="updateState()">
<option value="1">Country 1</option>
<option value="2">Country 2</option>
<option value="3">Country 3</option>
</select>
<select id="state" name="state">
<option value="1">State 1</option>
<option value="2">State 2</option>
<option value="3">State 3</option>
</select>
and im trying to update state select with JQuery ajax
<script type="text/javascript">
function updateState(){
var state = $('#country').val();
alert(state);
$.ajax(
{
type: "GET",
url: "../functions/selectstate.php",
data: { sta : state }
}).done(function( html )
{
$('#state').empty();
$('#state').append(html);
})
}
}
</script>
selectstate.php have this php code, i tested it and work fine
include_once('select.php');
include_once('cndb.php');
$sDB = new SelectDb();
$data = $sDB->selectStatesById($_GET['sta']);
foreach($data as $row)
{
echo "<option value=" . $row->state . ">" . $row->name . "</option>";
}
I just can't see where is the problem. Thanks in advance

HTML
<select id="country" name="country">
<option value="1">Country 1</option>
<option value="2">Country 2</option>
<option value="3">Country 3</option>
</select>
<select id="state" name="state">
<option value="1">State 1</option>
<option value="2">State 2</option>
<option value="3">State 3</option>
</select>
SCRIPT
<script type="text/javascript">
$(document).on('change','#country',function (){
var state = $('#country').val();
alert(state);
$.ajax(
{
type: "GET",
url: "../functions/selectstate.php",
data: { sta : state }
}).done(function( html )
{
//$('#state').empty();
//$('#state').append(html);
$('#state').html(html);
// you can use it insetead of the above two lines
})
//}<-- here is th problem remove this extra bracket
});
</script>

Try using the below approach. It might help
HTML Code:
<select id="country" name="country" onchange="updateState()">
<option value="1">Country 1</option>
<option value="2">Country 2</option>
<option value="3">Country 3</option>
</select>
<div id="stateDiv">
<select id="state" name="state">
<option value="1">State 1</option>
<option value="2">State 2</option>
<option value="3">State 3</option>
</select>
</div>
JavaScript Code:
...
...
.done(function( result )
{
$('#stateDiv').html(result);
})
...
...
PHP Code:
...
...
echo '<select id="state" name="state">';
foreach($data as $row)
{
echo "<option value=" . $row->state . ">" . $row->name . "</option>";
}
echo '</select>';
...
...

Related

Make the option selected based on Url parameter in php

I need to select both drop down based on the url parameter
Here is my code
<select id="parent-cat" name="parent-cat" onchange="getFilterUrl(this.value)">
<option value="www.site.co/?test=101">Test 1</option>
<option value="www.site.co/?test=102">Test 2</option>
</select>
<select id="child-cat" name="child-cat"
onchange="getFilterUrl(this.value)">
<option value="www.site.com/?sample=95">Sample 1</option>
<option value="www.site.co/?sample=96">Sample 2</option>
<option value="www.site.co/?sample=97">Sample 3</option>
<option value="www.site.co/?sample=98">Sample 4</option>
<option value="www.site.co" selected="">Sample 5</option>
</select>
<script>
function getFilterUrl(filterurl) {
var url = filterurl;
if (url) {
window.location = url; // redirect
}
return false;
}
</script>
Right now after selecting each drop down, that is refreshed to selected url,
I am looking for code, if we select first drop down, and next one,
I need to form url like below for second drop down first option.
Example: if Test 1 is selected, then, Sample 1 selected, the Url should be like beloww with both option as selected
site.co/?test=102&sample=95
How this can be done?
Please anyone help me to achieve this.
Thanks
As I said, your question is not clear
Here is a fixed version of your code.
document.getElementById("container").addEventListener("change",function(e) {
var parent = document.getElementById("parent-cat").value,
child = document.getElementById("child-cat").value;
if (parent && child) {
var url = "https://site.co?"+parent+"&"+child;
console.log(url)
// window.location=url;
}
});
<div id="container">
<select id="parent-cat" name="parent-cat">
<option value="">Please select</option>
<option value="test=101">Test 1</option>
<option value="test=102">Test 2</option>
</select>
<select id="child-cat" name="child-cat" >
<option value="">Please select</option>
<option value="sample=95">Sample 1</option>
<option value="sample=96">Sample 2</option>
<option value="sample=97">Sample 3</option>
<option value="sample=98">Sample 4</option>
</select>
</DIV>
In PHP:
document.getElementById("container").addEventListener("change", function(e) {
var parent = document.getElementById("parent-cat").value,
child = document.getElementById("child-cat").value;
document.getElementById("message").innerHTML= parent && child ? "":"Please choose both";
if (parent && child) document.getElementById("myForm").submit();
});
<form action="redirect.php" id="myForm" method="post">
<div id="container">
<select id="parent-cat" name="parent-cat">
<option value="">Please select</option>
<option value="test=101">Test 1</option>
<option value="test=102">Test 2</option>
</select>
<select id="child-cat" name="child-cat">
<option value="">Please select</option>
<option value="sample=95">Sample 1</option>
<option value="sample=96">Sample 2</option>
<option value="sample=97">Sample 3</option>
<option value="sample=98">Sample 4</option>
</select>
</div>
</form>
<span id="message"></span>
using
$parent = isset($_POST['parent-cat']) ? $_POST['parent-cat'] : null;
$child = isset($_POST['child-cat']) ? $_POST['child-cat'] : null;
if ($parent && $child) {
header("location: https:///www.site.co?".$parent."&".$child);
}
If you don't want to redirect immediately after choosing anyone just erase one of the onchange event.
HTML:
<select id="parent-cat" name="parent-cat" onchange="getFilterUrl()">
<option value="test=101">Test 1</option>
<option value="test=102">Test 2</option>
</select>
<select id="child-cat" name="child-cat"
onchange="getFilterUrl()">
<option value="sample=95">Sample 1</option>
<option value="sample=96">Sample 2</option>
<option value="sample=97">Sample 3</option>
<option value="sample=98">Sample 4</option>
<option value="www.site.co" selected="">Sample 5</option>
</select>
Script:
function getFilterUrl(filterurl) {
var s1 = $('#parent-cat').val();
var s2 = $('#child-cat').val();
if (s2 == 'www.site.co') {
s2 = '';
}
window.location.replace("http://www.site.co/?"+s1+'&'+s2);
return false;
}
In case of 5th option from the second select I exclude the second param from the url. If you have any questions - ask them.

how to display jquery/ajax data into select field?

code:
<script>
$(document).ready(function(){
$(".field").change(function(){
field = $(".field").val();
$.ajax({
type:"POST",
data:{"field":field},
url:"potential-courses.php",
success:function(data){
$(".course").val(data);
}
});
});
});
</script>
potential-courses.php
<?php
include("conn.php");
$field = $_POST['field'];
$sql = "select * from course_master where field = '$field' order by course_full_name";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
echo "<option value=".$row['course_short_name'].">".$row['course_full_name']."</option>";
}
?>
html code:
<select name='field' class='field' id="field">
<option value="">Select Field</option>
<option value='engineering'>Engineering</option>
<option value='law'>LAW</option>
<option value='medical'>Medical</option>
<option value='management'>Management</option>
<option value='pharmacy'>Pharmacy</option>
<option value='hotel management'>Hotel Management</option>
<option value='mass communication'>Mass Communication</option>
<option value='agriculture'>Agriculture</option>
<option value='architecture'>Architecture</option>
<option value='education'>Education</option>
<option value='paramedical'>Paramedical</option>
<option value='design'>Design</option>
<option value='commerce'>Commerce</option>
<option value='film/tV/media'>Film /TV/ Media</option>
</select>
<select name="course" class="course">
<option value="">Select Courses</option>
</select>
In this code I have two dropdown list i.e
<select name='field' class='field' id="field">
and another is
<select name="course" class="course">
when I change value from "name=field" it display nothing in "name=course". where I am doing wrong please help me.
Thank You
Change it:
$(".course").val(data);
to
$(".course").html(data);
It will add the <option> set that you have returned from php to your <select>

Select onselect all disable other values - PHP and jQuery

I have select box with multiple option in my project. The code is below
<select name='sendto[]' id='sendto' multiple required>
<option value='branchstaff'>Branch Staff</option>
<option value='manager'>Manager</option>
<option value='cashier'>Cashier</option>
<option value='hostaff'>Head Office Staff</option>
<option value='all'>All of Above</option>
</select>
I am using jQuery in this project. Now I want to select some values to send message. How can I deselect all other values when I click All of Above
This is how I would do it:
$("option").click(function () {
if ($(this).text() == "All of Above") {
$(this).siblings().removeAttr("selected");
}
});
Here is the JSFiddle demo
$('#sendto').change(function() {
alert($('option:selected', this).val())
if ($('option:selected', this).val() === 'all') {
$("select[multiple] option").prop("selected", "selected");
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select name='sendto[]' id='sendto' multiple required>
<option value='branchstaff'>Branch Staff</option>
<option value='manager'>Manager</option>
<option value='cashier'>Cashier</option>
<option value='hostaff'>Head Office Staff</option>
<option value='all'>All of Above</option>
</select>
$('#sendto').change(function () {
if ($('option:selected', this).val() == 'all') {
$("select[multiple] option").prop("selected", "selected");
}
})
<select name='sendto[]' id='sendto' multiple required>
<option value='branchstaff'>Branch Staff</option>
<option value='manager'>Manager</option>
<option value='cashier'>Cashier</option>
<option value='hostaff'>Head Office Staff</option>
<option value='all'>All of Above</option>
</select>
Try this
try below
$('#sendto option[value="all"]').on('click',function(){
$('#sendto').find('option').attr('selected','selected');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name='sendto[]' id='sendto' multiple required>
<option value='branchstaff'>Branch Staff</option>
<option value='manager'>Manager</option>
<option value='cashier'>Cashier</option>
<option value='hostaff'>Head Office Staff</option>
<option value='all'>All of Above</option>
</select>
$('option[value="all"]').on('click',function(){
$(this).parent().find('option').attr('selected','selected');
$(this).removeAttr('selected');
});

Javascript Jquery Get Dropdown value always 0

I am developing a website using CI, PHP, JQuery. I have a dropdown menu. When I try to get the value using JQuery, the result always 0. Here is my code.
html:
<div id="label"><label>Merk</label></div><div id="pilihan"><select name="brand">
<option value="0">PILIH SATU</option>
<option value="1">ACURA</option>
<option value="2">ALFA ROMEO</option>
<option value="3">ASTON MARTIN</option>
<option value="4">AUDI</option>
<option value="5">BENTLEY</option>
<option value="6">BERTONE</option>
<option value="7">BIMANTARA</option>
<option value="8">BMW</option>
<option value="9">BUGATTI</option>
<option value="10">BUICK</option>
<option value="11">CADILLAC</option>
<option value="12">CHANA</option>
<option value="13">CHERY</option>
<option value="14">CHEVROLET</option>
<option value="15">CHRYSLER</option>
<option value="17">CITROEN</option>
<option value="18">CODA</option>
<option value="19">DAEWOO</option>
<option value="20">DAIHATSU</option>
<option value="21">DAIMLER</option>
<option value="22">DATSUN</option>
<option value="23">DODGE</option>
<option value="24">DYNA</option>
<option value="25">FERRARI</option>
<option value="26">FIAT</option>
<option value="27">FORD</option>
<option value="28">FOTON</option>
<option value="29">GREELY</option>
<option value="30">GMC</option>
<option value="31">GREAT WALL</option>
<option value="32">HAGLER</option>
<option value="33">HINO</option>
<option value="34">HOLDEN</option>
<option value="35">HONDA</option>
<option value="36">HUMMER</option>
<option value="37">HYUNDAI</option>
<option value="38">INFINITI</option>
<option value="39">ISUZU</option>
<option value="40">JAGUAR</option>
<option value="41">JEEP</option>
<option value="42">KIA</option>
<option value="43">KTM</option>
<option value="44">LAMBORGHINI</option>
<option value="45">LAND ROVER</option>
<option value="46">LEXUS</option>
<option value="47">LIGIER</option>
<option value="48">LOTUS</option>
<option value="49">MASERATI</option>
<option value="50">MAYBACH</option>
<option value="51">MAZDA</option>
<option value="52">MCLAREN</option>
<option value="53">MERCEDES-BEN</option>
<option value="54">MINI</option>
<option value="55">MITSUBISHI</option>
<option value="56">MORGAN</option>
<option value="57">NISSAN</option>
<option value="58">OPEL</option>
<option value="59">PEUGEOT</option>
<option value="60">PORSCHE</option>
<option value="61">PROTON</option>
<option value="62">RANGE ROVER</option>
<option value="63">RENAULT</option>
<option value="64">ROLLS-ROYCE</option>
<option value="65">SAAB</option>
<option value="66">SKODA</option>
<option value="67">SMART</option>
<option value="68">SSANGYONG</option>
<option value="69">SUBARU</option>
<option value="70">SUNBEAM</option>
<option value="71">SUZUKI</option>
<option value="72">TATA</option>
<option value="73">TIMOR</option>
<option value="74">TOYOTA</option>
<option value="75">TRIUMPH</option>
<option value="76">VAUXHALL</option>
<option value="77">VOLKSWAGEN</option>
<option value="78">VOLVO</option>
<option value="79">WRANGLER</option>
</select></div>
JQuery:
$('select[name="brand"]').change(function(){
var brand = $("select[name='brand']").val();
alert(brand);
$.ajax({
type: "POST",
url: '<?php echo site_url("ajax_data/getmodel"); ?>',
dataType: 'json',
data: { vehicle_class : '', vehicle_brand : brand },
success: function(data) {
// Clear all options from vehicle class select
$('select[name="model"] option').remove();
// Fill vehicle class select
$.each(data, function(i, j){
var row = "<option value=\"" + j.model_id + "\">" + j.model_name + "</option>";
$(row).appendTo('select[name="model"]');
});
}
});
});
What is wrong with my code actually? Thank you.
I've just put your code in to a text file (named test.html)...
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
<div id="label"><label>Merk</label></div><div id="pilihan"><select name="brand">
<option value="0">PILIH SATU</option>
<option value="1">ACURA</option>
<option value="2">ALFA ROMEO</option>
<option value="3">ASTON MARTIN</option>
: : :
<option value="77">VOLKSWAGEN</option>
<option value="78">VOLVO</option>
<option value="79">WRANGLER</option>
<script type="text/javascript">
$('select[name="brand"]').change(function(){
var brand = $("select[name='brand']").val();
alert( brand );
});
</script>
</body>
</html>
And the code works, perfectly in FireFox, Chrome and IE8.
Also, my mate says salam sejahtera. :o)
You're selecting with select[name=brand], which means you're trying to access
<select value="THIS_IS_WHAT_YOU_ARE_SELECTING">
but you want the selected option, so use
$('select[name=brand] option:selected').val();

jQuery submit data not refresh

Is this the best way of doing this...
I have some drop downs and when a user selects an option I take their selection and send it to a php page to query a database then return that data to a div...
code is below
<?PHP
include('includes/inc_header.php');
#<-- SQL QUERIES START ------------------------------------------->
$sql1 = "SELECT Team.ShortLabel ShortLabel,Team.Label Label
FROM adlTeam Team
INNER JOIN adlPlanet Planet ON Team.TeamKey = Planet.TeamKey
GROUP BY ShortLabel";
$queryrow1 = mysql_query($sql1);
#<-- SQL QUERIES END ------------------------------------------->
?>
<script type="text/javascript">
$(document).ready(function()
{
$("#selectOwner").change(function ()
{
$.post('process.php', $(this).serialize()
,function(data){$("#ownerInfo").html(data)});
return false;
});
$("#selectZoom").change(function ()
{
$.post('zoomchange.php', $(this).serialize()
,function(data)
{
$("#mapchanges").html(data)
});
return false;
});
$("#nameStatus").change(function ()
{
$.post('mapchanges.php', $(this).serialize(), function(data)
{
$("#zoomchanges").html(data)
});
return false;
});
});
</script>
<div id="primary_content">
<form name="myform" id="myform" action="" method="POST">
<h3>SELECT TEAM</h3>
<select name="selectOwner" id="selectOwner" size="10"
style = "width:250px;margin-top:-12px;margin-left:15px;">
<?PHP
while ($clanlist = mysql_fetch_array($queryrow1))
{
#<-- SQL QUERIES START ------->
$sql3 = "SELECT Red, Green, Blue FROM adlteam
WHERE ShortLabel = '".$clanlist['ShortLabel']."'";
$queryrow3 = mysql_query($sql3);
$colors = mysql_fetch_array($queryrow3);
#<-- SQL QUERIES END ------------>
?>
<option id="ownerID_input" name="ownerID"
value="<?php
echo $clanlist['ShortLabel']; ?>"><?php echo $clanlist['Label']; ?>
</option>
<?PHP
}
?>
</select>
</form>
<div id="ownerInfo"></div>
<div id="planetInfo"></div>
<div id="mapchanges"></div>
<div id="zoomchanges"></div>
<div id="mapoptions">
<span class="h4">ZOOM:</span>
<select name="selectZoom" id="selectZoom">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
</select>
<input type="checkbox" style="margin-top:10px;"
name="nameStatus" id="nameStatus">
<span class="pinfo">Planet Names</span>
</div>
</div>
<div id="secondary_content">
<iframe src="mapgendisplay.html" name="testFrame" id="testFrame"
width="695" height="564" scrolling="no"
frameborder="0" allowtransparency></iframe>
</div>
<?PHP include('includes/inc_footer.php'); ?>
From what I can see, that does look like a good way of doing what you're trying to do. More information would be useful though.
It's possible you could reduce the code by making the functions more general:
var phpfile = new Array();
phpfile["selectZoom"] = "zoomchange.php";
...
...
var elementID = new Array();
elementID["selectZoom"] = "#mapchanges";
...
...
$("select").change(function() {
$.post(
phpfile[$(this).id()],
$(this).serialize(),
function(data) {
$(elementID[$(this).id()]).html(data)
}
);
});

Categories