This is my ajax code to pass two variables to php file.
var developer = $('#developerSelect').val();
var level = $('#userLevelSelect').val();
console.log(developer);
console.log(level);
$.ajax({
url: 'ajax-GenerateUserbyLevelReport.php',
type: 'post',
data: {developerSelected : developer,userlevel : level },
dataType: 'json',
success:function(response){
console.log(response);
}
})
I pass two variable to php file with the syntax like this : -
data: {developerSelected : developer,userlevel : level },
ajax-GenerateUserbyLevelReport.php :-
<?php
require_once 'dbconfig.php';
$developer = $_POST['developerSelected'];
$userLevel = $_POST['userlevel'];
// this query is to get the developer id
$DevQuery="SELECT id AS `ID` FROM pams_developer WHERE developer_name=:name";
$Dev_ID = ($GetReport->GetID($DevQuery,$developer));
//this query is the get all the user level id
$UserLevelQuery="SELECT id AS `ID` FROM pams_user_levels WHERE description=:name";
$Level_ID = ($GetReport->GetID($UserLevelQuery,$userLevel));
$UserLevelQuery = "SELECT `pams_users`.display_name AS Name ,`pams_user_levels`.description,`pams_users`.datecreated AS DateCreated,last_date AS LastLogin FROM pams_users
JOIN pams_developer ON `pams_users`.developer_id=`pams_developer`.id AND `pams_developer`.id=:dev_id
JOIN pams_user_levels ON `pams_user_levels`.id=`pams_users`.user_level AND `pams_user_levels`.id=$Level_ID
ORDER BY `pams_user_levels`.description ASC";
$UserbyUserLevel= $GetReport->GetData($UserLevelQuery,$Dev_ID);
echo json_encode($UserbyUserLevel,JSON_HEX_APOS);
//json_encode data echo here
?>
But the problem is the console.log(response) return nothing.
Anyone can help me figure out my problem ?
Use curl for debug, for example
Curl -v -X POST 'http://server/server.script.php' -d '{"developerSelected":"Jhon","user level":"24"}'
Your dataType is set to json, but your data is not in json format,so just remove the dataType and it will work
$.ajax({
url: 'ajax-GenerateUserbyLevelReport.php',
type: 'post',
data: {developerSelected : developer,userlevel : level },
//dataType: 'json', // just remove this line
success:function(response){
console.log(response);
}
});
If you still want to use json, then you need to use JSON.stringify() to change your data parameter:
var dataObj = {developerSelected : developer,userlevel : level };
$.ajax({
url: 'ajax-GenerateUserbyLevelReport.php',
type: 'post',
data: JSON.stringify(dataObj),
dataType: 'json',
success:function(response){
console.log(response);
}
});
Related
I am trying to pass parameter in below ajax url
function endprogress(){
$("#surfbar").html("Please wait...");
var dataString = 'action=validate&t='+adtk;
$.ajax({
type: "POST",
url: 'yes_manage.php?view=surf&track='+user_name&, /*this line giving error*/
data: dataString,
success: function(msg){
}
});
}
above url field is giving error Expected and identifier instead saw ','
how can I resolve this
Change user_name& => user_name
look like
function endprogress(){
$("#surfbar").html("Please wait...");
var dataString = 'action=validate&t='+adtk;
$.ajax({
type: "POST",
url: 'yes_manage.php?view=surf&track='+user_name, /*remove & at the end user_name*/
data: dataString,
success: function(msg){
}
});
}
I am fetching values from data using where in() mysql query and I got the correct result, but I don't know how to display the result in ajax success.
How do I display company name and email id from result data set?
my ajax code
<script type="text/javascript">
$('#ok').on('click', function() {
var vals = $('#show').val();
$.ajax({
type: "POST",
url: "<?php echo base_url();?>email/get_company",
data: { vals:vals },
datatype: 'json',
success: function (data) {
alert(data);
$("#result").html(data);
var result = JSON.parse(data);
}
});
});
</script>
my controller code:
function get_company()
{
$vals = $this->input->post('vals');
$query = $this->db->query("SELECT * FROM customer` where company_id IN ($vals) ")->result();
echo json_encode($query);
}
my result:
[{"company_name":"xyz Ltd","company_email":"123#gmail.com"},{"company_name":"wer Jit","company_email":"2222#gmail.com"}]
assuming you get this json in your ajax success:
const json = '[ {
"company_name": "xyz Ltd",
"company_email": "123#gmail.com"
},
{
"company_name": "wer Jit",
"company_email": "2222#gmail.com"
}]
';
const obj = JSON.parse(json);
// you don't need this line, if you have set ajax datatype:'json'
you can get results for a single data set:
console.log(obj[0].company_name);
// this is how to get the first company name of the data set
// expected output: "xyz Ltd"
or you can loop through the whole data set
obj.forEach(element => $("#result").append(element.company_name + '<br>'));
see a working example
conclusion: your ajax function could look just simply like this:
$.ajax({
type: "POST",
url: "<?php echo base_url();?>email/get_company",
data: {
vals: vals
},
datatype: 'json',
success: function(data) {
obj.forEach(data => $("#result").append(data.company_name + '<br>'));
}
})
Currently, the searchform hit, the form get submitted. Then it will fetch data from specified URL which is search/ajax2.php and return data here.
All I want to add is, to include another URL beside the above mentioned one, so that two actions can be performed at the same time.
Now, in the search/ajax2.php it runs a select query. Whereas in additional page that I want to include, which could be writedb.php it inserts data taken from this jason into database. It doesn't have to return anything back to ajax page though!
How to achieve this?
$("#searchform").on("submit", function () {
//$(this).find(':submit').attr('disabled','disabled');
var data = {
"action": "test"
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "POST",
dataType: "json",
url: "search/ajax2.php",
data: data,
success: function (data) {
}
});
Try adding your second url in sucess function like this :
$.ajax({
type: 'POST',
url: 'some_url1',
data: 'some data',
success: function(data){
$.ajax ({
type: 'POST',
url: 'some_url2',
data: 'some data',
success: function(data){}
});
}
});
I have the below that uploads an image using JQuery's AJAX function
var id = <?php echo $id; ?>;
var img_data = new FormData($("form")[0]);
$.ajax({
url: 'add.php',
data: img_data,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
alert(data);
}
});
I'd like to include a string with the FormData sent. I tried the following, but no luck
data: img_data {id: id},
What's the correct syntax here?
Use append
var img_data = new FormData($("form")[0]);
img_data.append('id', id);
You could create a JSON data object and pass that as application/json and process the data within add.php:
var data = {
id : <?php echo !empty($id) ? $id : "''",
img_data : new FormData($("form")[0])
};
$.ajax({
url: 'add.php',
data: data,
contentType: "application/json",
type: 'POST',
success: function(data){
alert(data);
}
});
Although unconventional, you could also append the data as a query string to the URL with the POST. You'd also need to edit add.php to get this parameter.
$.ajax({
url: 'add.php?id=' + id,
data: img_data,
processData: false,
type: 'POST',
success: function(data){
alert(data);
}
});
I am trying to send some information to my php page via an ajax call. However I am getting an issue that a } missing after property list. Below is my code:
function article(Article){
var surl = "http://www.webapp-testing.com/includes/article_desc.php";
var id = 1;
$.ajax({
type: "POST",
url: surl,
data: '"Article="+Article';
dataType: "jsonp",
cache : false,
jsonp : "onJSONPLoad",
jsonpCallback: "articlecallback",
crossDomain: "true",
success: function(response) {
alert("Success");
},
error: function (xhr, status) {
alert('Unknown error ' + status);
}
});
}
Any help will be greatly appreciated
Change the ; after your data attribute to a ,.
data: '"Article="+Article',
This string should end with ,:
data: '"Article="+Article';
You have a semicolon after the "data" element. It should be a comma.
replace the the semicolon on line data: '"Article="+Article'; with ,