Can't obtain selected value for AJAX parameter - php

I want to pass id and value of the selected option as parameters to my ajax. Somehow I can see id passing but not the value.
How can this be achieved, I am posting my code here, please any advice will be helpful.
I have tried many ways but still the datString shows up Object { id: "3", designationname2: "3" } on console.log.
<b>Designation</b>
<select name="designation" class="form-control" id="desig" >
<option value="">Select a Designation/Role</option>
<?php
$sql = mysql_query("SELECT id, designation FROM tbl where status =1 and designationtype_id = 1 ");
while ($rows = mysql_fetch_assoc($sql)) {
$des_id2 = $rows['id'];
$designationname2 = $rows['designation'];
echo "<option value=" . $des_id2 . ">" . $designationname2 . "</option>";
}
?>
</select>
And AJAX,
<script src = "http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#desig").change(function () {
var id = $(this).val();
var designationname2 = $('#desig').val();
var dataString = {'id': id, 'designationname2': designationname2};
console.log(dataString);
$.ajax({
type: "POST",
url: "escalation_ajax.php",
data: dataString,
cache: false,
success: function (html) {
$("#rephead").html(html);
}
})
})
})
</script>

You can change below line
var designationname2 = $('#desig').val();
This line will get the value of the selected option, that you don't required
to
var designationname2 = $(this).find("option:selected").text()
Above line will give you text of the selected option of text of the current element.
OR
var designationname2 = $('#desig option:selected').text()
Above line will give the text of any selector with option selected

Try this:
var designationname2 = $(this).find('option:selected').text();

try this one
Use text() instead of val()
var designationname2 = $( "#desig option:selected" ).text();

try this :
var designationname2 = $('#desig option:selected').val();

Related

retrieve values from database and display in textbox for the corresponding value selected using drop down in ajax

I have a dropdown list where it lists values fetched from database
echo "<select name='training_name' id='training_name' value='' class='form-control' onchange='tr_name()' required><option value=''>Select</option>";
while($r = mysql_fetch_array($result)) {
$dt=date("Y-m-d",strtotime($r['date']));?>
<option value="<?php echo $r['nomination_form_trainer_id'];?>" ><?php echo $r['training_title']."(".$dt.")";?></option>;
<?php }?>
<?php echo "</select>";
I have a input type as textbox
<input type="text" class="form-control txtOnly" id="trainer_name" name="trainer_name" value="" required/>
I have my ajax code as follows
<script type="text/javascript">
function tr_name()
{
var tname= $('#training_name').val();
var dataString = 'tname=' + tname;
$.ajax({
type: "POST",
url: "training_name.php",
data: dataString,
success: function(result){
$("#trainer_name").html(result);
}
});
}
</script>
here is my php code for training_name.php
<?php require_once 'includes/config.php';
$training_name = $_POST["training_name"];
mysql_select_db("ptlct_training");
//here, you should test whether employee_detail matches what you expect
//here, split $employee_detail into $first_name, $last_name and $company_name
//now you are ready to send the MYSQL query:
$sql = 'SELECT initiator_name FROM nomination_form_trainer WHERE nomination_form_trainer_id=2';
$result = mysql_query($sql);
//since you expect a single matching result, you can test for num_rows == 1:
while ($row =mysql_fetch_array($result)) {
$trainer_name = $row['initiator_name'];
}
echo $trainer_name;
?>
I am trying to fetch values I am new to ajax so i couldn't find where I am going wrong.Someone please help me soon.Thanks in advance
var dataString = '&tname=' + tname;
check param is passing in php or not .....!
I found my answer by myself
<script type="text/javascript">
$("#training_name").change(function(){
{
var tname = $('#training_name').val();
var dataString = 'tname=' + tname;
$.ajax({
type: "POST",
url: "training_name.php",
data: dataString,
success: function(result){
$("#trainer_name").val(result);
}
});
}
});
</script>
I have declared the variable inside the function that is why the parameter did not pass properly

How to pick multiple rows of values of the same id value from the database and display in ajax?

I have written code for button to send a value to php file using ajax as show below :
<script>
$(document).ready(function(){
$(".open-AddBookDialog").click(function(){
var packageid = $(this).attr("data-id");
var dataString = 'packageid='+ packageid;
$.ajax({
type: "POST",
url: "data1.php",
data: dataString,
cache: false,
success: function(result1){
var packagetype = package_type;
alert(packagetype);
}
});
});
});
</script>
This is the ajax code for the button which i have written. My button code is :
<a data-toggle="modal" id="custId" data-name="<?php echo $arr["package_name"]; ?>" data-id="<?php echo $arr["id"]; ?>" href="#myModal" class="open-AddBookDialog btn btn-submit" OnClick='change(this);'>Book / Enquiry / Pay</a>
When clicking this button in href, I want to send a id value to a php file using ajax.
data1.php
<?php
include('database.php');
$id=$_POST['packageid'];
$query1 = mysqli_query($con,"select * from ayurveda_packagetypes where package_id = '$id'");
$arr = mysqli_fetch_array($query1);
echo $arr['package_type'];
echo $arr['package_price'];
mysqli_close($con);
?>
using the id value, I want to pick multiple rows of package type and package price from the database having the same id value.After picking the multiple rows of these values i want to send it to the main php file and display all the values of all the rows picked from the database.
Can anyone suggest how to do this ?
<script>
$(document).ready(function()
{
$(".open-AddBookDialog").click(function()
{
var packageid = $(this).attr("data-id");
var dataString = 'packageid='+ packageid;
$.ajax(
{
type: "POST",
url: "data1.php",
data: dataString,
cache: false,
success: function(result)
{
resultJson=jQuery.parseJSON(result);
$.each(resultJson.packageDetails, function(i, item) {
var packagetype = item.package_type;
var package_price = item.package_price;
alert("packagetype :- "+packagetype+" ----- package_price :-"+package_price);
});
}
});
});
});
</script>
<?php
include('database.php');
$id=$_POST['packageid'];
$query1 = mysqli_query($con,"select * from ayurveda_packagetypes where package_id = '$id'");
//$arr = mysqli_fetch_array($query1);
while( $strPackageResult=mysqli_fetch_array($query1,MYSQLI_ASSOC) )
{ $ArrPackage[]=$strPackageResult; }
if( isset($strPackageResult) ){ unset($strPackageResult); }
mysqli_free_result($query1);
if( isset($query1) ){ unset($query1); }
$myResultPackageArray=array();
if( isset($ArrPackage) && is_array($ArrPackage) && count($ArrPackage)>0 ) {
$tempArray=array();
foreach( $ArrPackage as $tempPackage )
{
$tempArray['package_type']=$tempPackage['$tempPackage'];
$tempArray['package_price']=$tempPackage['package_price'];
$myResultPackageArray['packageDetails'][] =$tempArray;
}
}
mysqli_close($con);
echo json_encode($myResultPackageArray);
?>
There are some basic things you should know first then you can easily rectify your errors.
Debuging javascript
Prepared Statements
PHP Error Handling
This is not you have asked but as a programmer i will suggest you to go through it.
As going through your code
var packagetype = package_type;
package_type is undefined. If you are using chrome inspect element and check the console you will see the error.
Hope this will work.

Uncaught ReferenceError: $_POST is not defined

I'm new in AJAX and want to send 2 IDs on an AJAX page here is my code
Click Here
<script>
$(document).ready(function() {
$(".edit3").click(function() {
var moduleID = $(this).attr('id');
var studentID = $_POST['studentAssignID']; //Problem is Here
$.ajax({
type: "POST",
url: 'assign-assignment-ajax.php',
data: "moduleID="+moduleID+"&studentID="+studentID,
success: function(data)
{
$("#editform2").html(data);
$("#editform2").show('slow');
}
});
});
});
I'm getting module ID by clicking on a href, while I also want student ID with it, which part i'm doing wrong?
try this :
var studentID = "<?php echo $_POST['studentAssignID']; ?>";
Declare 'StudentAssignID' in hidden field.
<input type='hidden' class="StudentAssignID" value="<?echo $_POST['studentAssignID'];?>">
Then Use This Value in Script.
<script>
.
.
var studentID = $('.StudentAssignID').val();
.
.
</script>

How do I get multiple textbox values that are dynamically created in jquery

How do I get multiple textbox values that are dynamically created in jquery
Sample code:
<?php
$i=1;
while($i<10)
{
echo "<input type='textbox' class='qty' id='qty_$id'>";
echo "<input type='textbox' class='item' id='item_$id'>";
$i++;
}
echo '<input class="btn_transaction" id="btn_update" type="button" style="width:auto" value="Update">';
?>
jquery
<script>
jQuery(document).ready(function(e){
$("#btn_update").click(function() {
$.each($('.qty'), function() {
var qty = $(this).val();
alert(qty); // im getting qty here. In the same way i want item also, how to get item value here
jQuery.post('../ajax/ajax_cart.php',{section:'update_tocart',qty:qty},function(data){
});
});
});
});
</script>
thanks in advance :)
DEMO
Try this
$("#btn_update").click(function() {
$(".qty").each(function() {
var qty = $(this).val();
alert(qty);
});
});
Hope this helps,Thank you
jQuery(document).ready(function(e){
$("#btn_update").click(function() {
var data = {};
$( "input" ).each(function() {
data[$(this).attr('id')] = $(this).val();
jQuery.post('../ajax/ajax_cart.php',{section:'update_tocart',data:data},function(data){
});
});
});
});
Try above!! You can find all input elements! You can then store it in a json object and pass to ajax call.
Try this
$.each($('.qty'), function() {
var qty = $(this).val();
var item = $(this).siblings('input.item').val();
});
Try with .next() like
var item = $(this).next('.item').val();
So it would be
$('.qty').each(function() { // Or $.each('.qty' , function() {
var qty = $(this).val();
var item = $(this).next('.item').val();
});
See the FIDDLE And FIDDLE2
try
$('.qty').each(function() {
var qty = $(this).val();
var item = $(this).closest('.item').attr('value'); })
add class to all your textbox e.g textbox
<script type="text/javascript">
var Work = [];
var textboxes = document.getElementsByClassName('name_list');
var boxlengt = document.getElementsByClassName('name_list').length;
for(var i=0;i<boxlengt;i++){
Work.push(textboxes[i].value)
}
Work = Work.toString();
</script>
All the values are being stored in the items array. Now its up to you how you want each values to be used.

Grabbing Two Values Javascript

I am grabbing the values from a listbox and passing it on to another listbox, I had everything working with one value $Lid, but now I need two $Lid and $Cid, is this the correct way to do this?
$(document).ready(function()
{
$(".Doggie").change(function()
{
var LocationString = $(this).find(":selected").val();
var CityString = $(this).find(":selected").val();
$.ajax({
type: "POST",
url: "ajax_city.php",
data: {Lid : LocationString, Cid : CityString},
cache: false,
success: function (html) {
$(".Kitty").html(html);
}
});
});
$('.Kitty').live("change",function(){
var LocationString = $(this).find(":selected").val();
var CityString = $(this).find(":selected").val();
$.ajax({
type: "POST",
url: "ajax_area.php",
data: {Lid : LocationString, Cid : CityString},
cache: false,
success: function (html) {
$(".Pig").html(html);
}
});
});
});
</script>
</head>
<body>
<div id="frame1">
<label>Place :</label>
<select name="Doggie" class="Doggie" id="Doggie">
<option selected="selected">--Select Place--</option>
<?php
$sql = mysql_query("SELECT tblLocations.RestID as Lid, tblLocations.CityID as Cid, tblRestaurants.RestName as name
FROM tblRestaurants INNER JOIN tblLocations ON tblRestaurants.RestID = tblLocations.RestID
GROUP BY tblLocations.RestID, tblRestaurants.RestName
ORDER BY tblRestaurants.RestName ASC");
while($row=mysql_fetch_array($sql))
{
echo '<option value="'.$row['Lid'].''.$row['Cid'].'">'.$row['name'].'</option>';
} ?>
</select>
<label>City :</label>
<select name="Kitty" class="Kitty" id="Kitty">
<option selected="selected">--Select City--</option>
</select>
<label>Area: :</label>
<select name="Pig" class="Pig" id="Pig">
<option selected="selected">--Select Area--</option>
</select>
</div>
</body>
</html>
And...
<?php
require ('config.php');
if ($_POST['Lid']) {
$Lid = $_POST['Lid'];
$sql = mysql_query("SELECT tblLocations.RestId as Lid, tblLocations.CityID as Cid, tblCities.CityName as name
FROM tblLocations INNER JOIN tblCities ON tblLocations.CityID = tblCities.CityID
WHERE tblLocations.RestID = $Lid
GROUP BY tblLocations.RestID, tblCities.CityName
ORDER BY tblCities.CityName ASC");
echo '<option selected="selected">--Select City--</option>';
while ($row = mysql_fetch_array($sql)) {
echo '<option value="' . $row['Lid'] . '' . $row['Cid'] . '">' . $row['name'] . '</option>';
}
}
?>
Right now its not returning anything so I have to assume its wrong. Thank you.
I would recommend making the changes below:
var LocationString = $(this).find(":selected").val();
var CityString = $(this).find(":selected").val();
$.ajax({
type: "POST",
url: "ajax_city.php",
data: {Lid : LocationString, Cid : CityString},
cache: false,
success: function (html) {
$(".Kitty").html(html);
}
});
You were adding two data values, which is not the right way of doing it. Simply pass a single literal object with your desired key and values and allow JQuery to do the formatting for you.
I don't understand why you store the same value in two vars:
var LocationString = 'Lid=' + $(this).val();
var CityString = 'Cid=' + $(this).val();
It can be simplified to:
var LocationString = $(this).val();
And then you only have one value, so data should be the following format
data: {
'Lid': LocationString
}
data should be the format
data: {Lid : LocationString, Cid : CityString},
and also check what is the result of your query
check it by
print_r(mysql_fetch_array($sql))
if your query does not have any results , echo inside while loop wil not work
This did it.
$(document).ready(function()
{
$(".Doggie").change(function()
{
var LocationString ='Rid='+ $(this).val();
$.ajax({
type: "POST",
url: "place_city.php",
data: LocationString,
cache: false,
success: function (html) {
$(".Kitty").html(html);
}
});
});
$('.Kitty').live("change",function(){
var Rid = $('#Doggie').val(), // This is the value of the id="Doggie" selected option
Cid = $(this).val(); // This is the value of the id="Kitty" selected option
//alert("Rid = " + Rid + " Cid = " + Cid);
$.ajax({
type: "POST",
url: "place_area.php",
data: {"Rid":Rid,"Cid":Cid},
cache: false,
success: function (html) {
//alert('This is what is returned from the php script: ' + html);
$(".Pig").html(html);
}});});});

Categories