I am using jquery to call a PHP script when a select box changes. I am able to get the ID of the select item that changes, however I get no output from the PHP script. The Javascript is
function initagents() {
$('select.agent').on('change', function() {
var id = $(this).attr("id");
console.log(id);
var data = { id: id };
$.ajax({
type: "POST",
dataType: "json",
url: "updateagent.php",
data: data
}).done(function(msg) {
console.log(msg);
});
});
}
and the PHP is
print_r ( $_POST);
Any ideas?
Please use
echo json_encode($_POST['id']);
instead of
print_r($_POST);
You have made a mistake by placing the onChange event of the select box inside function initagents().
fixed code:
$('select.agent').on('change', function() {
var id = $(this).attr("id");
console.log(id);
var data = {
id: id
};
$.ajax({
type: "POST",
dataType: "json",
url: "updateagent.php",
data: data
}).done(function(msg) {
console.log(msg);
});
});
Just try with the following code:
function getID(val) {
alert(val);
$.ajax({
type: 'POST',
url: 'post.php',
data: {
id: val
},
success: function(data) {
alert(data);
}
});
}
<select name="pks" onchange='getID(this.value);'>
<option value="1">Option1</option>
<option value="2">Option2</option>
</select>
Seems your code is correct and it should send the data to php file. But also I see that, you are not sending selected value to php script. You are posting selected element ID.
Are you trying this in a sub folder ? make sure the php file path is correct in javascript. Better if you can try with the full path. For example
url: "http://localhost/myproject/updateagent.php",
Related
I would like to pass a php variable onto another php page (category-get-secondary.php) through Ajax. I have the following code:
function getSecondaryCat(val) {
$.ajax({
type: "POST",
url: "category-get-secondary.php",
data: 'primary_cat='+val,
success: function(data){
$("#secondary_cat").html(data);
$("#tertiary_cat").html('<option value="">Select specific category</option>')
}
});
}
On category-get-secondary.php I want to get the value: -
$postPrimaryCat = $_POST['primary_cat'];
$categoryType = $_POST['category-select'];
$postPrimaryCat is transferred. Now I want to transfer the value for $categoryType.
I would like to pass the PHP variable 'category-select'. This is what I tried to transfer the value:
function getSecondaryCat(val,cat_val) {
$.ajax({
type: "POST",
url: "category-get-secondary.php",
data:'primary_cat='+val+'&category-select='+cat_val,
success: function(data){
$("#secondary_cat").html(data);
$("#tertiary_cat").html('<option value="">Select specific category</option>')
}
});
}
and to get the value I'm using
$getCategorySet = $_POST['category-select'];
but the value doesn't seem to be transfereed
Updated with full code:
main-page.php
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
</script>
<script>
function getSecondaryCat(val, catval) {
$.ajax({
type: "POST",
url: "category-get-secondary.php",
data:'primary_cat='+val+'&categoryselect='+catval,
success: function(data){
$("#secondary_cat").html(data);
$("#tertiary_cat").html('<option value="">Select specific category</option>')
}
});
}
function selectCountry(val) {
$("#search-box").val(val);
$("#suggesstion-box").hide();
}
</script>
<?php
//Get category set from URL bar
$_POST['categoryselect'] = 'premium';
category-get-secondary.php
<?php
//Insert value to ajax
$postCatType = $_POST['categoryselect'];
Valid Characters
In general JavaScript, variable/function names can't contain -. They can only contain letters, $, and _ (Underscore)
So... change data:'primary_cat='+val+'&category-select='+cat_val, to this data:'primary_cat='+val+'&category_select='+cat_val,
Hope it solves your issue.
function getSecondaryCat(val) {
$.ajax({
type: "POST",
url: "category-get-secondary.php",
data: {primary_cat:val},
success: function(data){
$("#secondary_cat").html(data);
$("#tertiary_cat").html('<option value="">Select specific category</option>')
}
});
}
Change data and add below data line in your code.
Ajax
function getSecondaryCat(val,cat_val) {
$.ajax({
type: "POST",
url: "category-get-secondary.php",
data:{primary_cat:val,category_select:cat_val} ,
success: function(data){
$("#secondary_cat").html(data);
$("#tertiary_cat").html('<option value="">Select specific category</option>')
}
});
}
PHP
$postPrimaryCat = $_POST['primary_cat'];
$categoryType = $_POST['category-select'];
I write this code, but it doesn't work. I want to show an array in php using ajax.
It's an html select that chooses every option of this select list value of the option take in the variable and sends it to ajax. Ajax should then post data to php then php select the received data from database and show all of them.
But I can't show this data in ajax. :(
$(function(){
$("#topic").change(function(){
var str = "";
$( "select option:selected" ).each(function() {
str += $( this ).text() + " ";
options(str);
});
});
});
function options(option){
$.ajax({
type: "POST",
dataType: 'json',
url: "/Register/checkSelect", //Relative or absolute path to response.php file
data: {
option:option
}).done(function(){
$("#content").html(data);
alert("ok");
});
});
}
You have an error in your ajax request.
Here the right code:
function options(option){
$.ajax({
type: "POST",
dataType: 'json',
url: "/Register/checkSelect", //Relative or absolute path to response.php file
data: {
option:option
}
}).done(function(data){
$("#content").html(data);
alert("ok");
});
}
<script type="text/javascript">
$(document).ready(function () {
$("#select-dept").change(function () {
var id = $("#select-dept").val();
$.ajax({
type: "POST",
url: "<?=base_url()?>.index.php/sms/get_dept_employee",
//url: baseurl + 'sms/get_dept_employee',
data: "id",
dataType = "json",
cache: "false",
success: function (emp_list) {
$("#dept-emp").html(emp_list);
}
});
});
});
</script>
I am unable to send view data to controller function
In view their is select box with departmenr values from mysql database
<select class="select-dept" id="select-dept" name="select-dept">
<option>Select Department</option>
<?foreach ($departments as $dt):?>
<option value="<?=$dt['id']?>">
<?=$dt[ 'name']?>
</option>
<?endforeach;?>
</select>
i need to get refresh when user select department and that need to call controller function
get_dept_employee
And need to display datagrid of employee list
you need to send data option as object
try this
.....
url: "<?=base_url()?>.index.php/sms/get_dept_employee",
data: {"id":id},
dataType:"json",
....
and get the posted value as id in your controller..
$id=$this->input->post('id');
....
var id = $("#select-dept").val();
data:"id", //Here you are sending string as id
should be
data:id, //No double quotes surrounded
Try:
data: {'id':$(this).val()},
i saw few errors
use data: {'id':id}, instead of data: "id",
use dataType:"json", instead of dataType="json",
use cache:false instead of cache: "false",
<script type="text/javascript">
$(document).ready(function () {
var base_url = "<?php echo $this->config->item('base_url'); ?>";
$("#select-dept").change(function () {
var id = $(this).val();
$.ajax({
type: "POST",
url: base_url+"index.php/sms/get_dept_employee";
data: {'id':id},
dataType:"json",
cache: false
success: function (emp_list) {
$("#dept-emp").html(emp_list);
}
});
});
});
</script>
I want to pass values to a PHP script so i am using AJAX to pass those, and in the same function I am using another AJAX to retrieve those values.
The problem is that the second AJAX is not retrieving any value from the PHP file. Why is this? How can I store the variable passed on to the PHP script so that the second AJAX can retrieve it?
My code is as follows:
AJAX CODE:
$(document).ready(function() {
$("#raaagh").click(function(){
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data){
console.log(data);
}
});
$.ajax({
url:'ajax.php',
data:"",
dataType:'json',
success:function(data1){
var y1=data1;
console.log(data1);
}
});
});
});
PHP CODE:
<?php
$userAnswer = $_POST['name'];
echo json_encode($userAnswer);
?>
Use dataType:"json" for json data
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
dataType:'json', // add json datatype to get json
data: ({name: 145}),
success: function(data){
console.log(data);
}
});
Read Docs http://api.jquery.com/jQuery.ajax/
Also in PHP
<?php
$userAnswer = $_POST['name'];
$sql="SELECT * FROM <tablename> where color='".$userAnswer."'" ;
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
// for first row only and suppose table having data
echo json_encode($row); // pass array in json_encode
?>
No need to use second ajax function, you can get it back on success inside a function, another issue here is you don't know when the first ajax call finished, then, even if you use SESSION you may not get it within second AJAX call.
SO, I recommend using one AJAX call and get the value with success.
example: in first ajax call
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data){
console.log(data);
alert(data);
//or if the data is JSON
var jdata = jQuery.parseJSON(data);
}
});
$(document).ready(function() {
$("#raaagh").click(function() {
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data) {
console.log(data);
$.ajax({
url:'ajax.php',
data: data,
dataType:'json',
success:function(data1) {
var y1=data1;
console.log(data1);
}
});
}
});
});
});
Use like this, first make a ajax call to get data, then your php function will return u the result which u wil get in data and pass that data to the new ajax call
In your PhP file there's going to be a variable called $_REQUEST and it contains an array with all the data send from Javascript to PhP using AJAX.
Try this: var_dump($_REQUEST); and check if you're receiving the values.
you have to pass values with the single quotes
$(document).ready(function() {
$("#raaagh").click(function(){
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: '145'}), //variables should be pass like this
success: function(data){
console.log(data);
}
});
$.ajax({
url:'ajax.php',
data:"",
dataType:'json',
success:function(data1){
var y1=data1;
console.log(data1);
}
});
});
});
try it it may work.......
I am trying to pass a value of a button using some ajax to a separate file.
Here is my code.
$("input#downloadSingle").click(function() {
var myData = $("input#downloadSingle").val();
$.ajax({
type: 'post',
url:'singleDownload.php',
data: myData,
success: function(results) {
alert('works');
}
});
});
However, when I test out the next page by doing a var_dump on $_POST. I don't get any data back. Thoughts?
You're not specifying the name of the $_POST variable you're gonna get on the singleDownload.php file (unless that's part of the button's value), so you should try something like:
$("input#downloadSingle").click(function() {
var myData = "whatever=" + $("input#downloadSingle").val();
$.ajax({
type: 'post',
url:'singleDownload.php',
data: myData,
success: function(results) {
alert('works');
}
});
});
And make sure $_POST in your php file is the same whatever variable