Response not received from ajax request in php - php

I have a page where there is table and each row has a icon for detailed information. When the icon is clicked, I get the unique identifier for that row into datastring and send a AJAX request. I get a response but cannot catch the correct response. Why is that I am doing wrong? I need to access the $desc in home.php , how to catch that in response ?
home.php
$.ajax({
type: "POST",
url: "actionone.php",
data: dataString,
cache: true,
success:function() {
alert("success");
}
});
actionone.php
if(isset($_POST['content']))
{
$content=(int)$_POST['content'];
$fetch = mysql_query("SELECT ISSUEDESCRIPTION FROM issues WHERE KBID =$content");
while($rowe = mysql_fetch_array($fetch))
{
$desc = $rowe['ISSUEDESCRIPTION'];
}

For ajax success work you output some from php code
ie Change php code to
if(isset($_POST['content']))
{
$content=(int)$_POST['content'];
$fetch = mysql_query("SELECT ISSUEDESCRIPTION FROM issues WHERE KBID =$content");
while($rowe = mysql_fetch_array($fetch))
{
$desc = $rowe['ISSUEDESCRIPTION'];
}
echo $desc;
}
JQUERY
$.ajax({
type: "POST",
url: "actionone.php",
data: dataString,
cache: true,
success:function(data) {
alert(data.trim());
}
});

Related

how to return data from php to ajax success call

nali.php
$tagId = mysqli_insert_id($link);
echo $tagId;
at ajax success call
$.ajax({
url: "nail.php",
type: "post",
data: {Tag:tag},
success: function(data) {
$enter_tag_id = data.tagId;
alert(data);
}
});
i get the correct echo at php page . but not get any value at ajax success.i want to save tagId to a variable at ajax success
Update your ajax code:
success: function(data) {
//$enter_tag_id = data;
alert(data);
}

Add wishlist button to Virtuamart product list

I'm using
1. Joomla 3.4.4
2. Virtuamart 3.0.9
and I want to use send data from products sublayout in Virtuamart and send data with ajax to a controller.
var url = "?";
jQuery(document).ready(function() {
jQuery( ".wishlist-btn" ).click(function() {
var productid = this.id;
var userid = jQuery('input#user_id').val();
var fav = {
Productid: productid,
Userid:userid
}
jQuery.ajax({
url: url,
type: "POST",
data: { json: JSON.stringify(fav) },
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function(data) {
alert(data);
}
});
});
but I don't know where should write PHP code,and what is the URL in my AJAX request.
I want to send profile id and user id to add in database for wishlist.
I created a PHP file in
com_virtuamar/controller/ajax.php
$json = $_POST['json'];
$person = json_encode($json);
echo $person->Userid;
and for URL in my AJAX request I used
/components/com_virtuemart/controllers/ajax.php
but I think it's completely incorrect for address and usage in controller and also I don't know why userid doesn't return
You are an error in your Ajax request. For send to PHP file, remove data: { json: JSON.stringify(fav) } and replace by:
jQuery.ajax({
url: url,
type: "POST",
data: JSON.stringify(fav),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function(data) {
alert(data);
}
});
and on your php file replace:
$person = json_encode($json);
by
$person = json_decode($json);
If you want access directly on Joomla session for get ID or Password user, you can check for
JFactory::getUser();
please see more: https://docs.joomla.org/JFactory/getUser

how do i get a response from a jquery ajax request

I am working on my site and i have a jquery request to the server
$.ajax(
// do an ajax to send value to the database...
{
url:"pages/welcome_get.php",
type: "POST",
dataType: "json",
cache: false,
data: { wm_val: wel}
})
How can I get a response as a json data from which is not a html data and how do I parse the json response from the server to the html file?
You write the PHP to emit JSON.
<?php
# Some code to populate $some_associative_or_non_associative_array
header("Content-Type: application/json");
echo json_encode($some_associative_or_non_associative_array);
?>
You need to use the parseJSON function in the js.
Here is the Php code:
function send_reply(){
echo json_encode(array('reply'=>'here is my reply'));
exit;
}
Here is the js code:
$.ajax({
url:'myajax.php',
data:{'func':send_reply},
type:'POST',
dateType:'json'
}).success(function(data){
data=$.parseJSON(data);
alert(data.reply);
}).error(function(jqxhr,error,status){
alert('Error sending reply');
});
As #Quentin said, you need to output JSON in your PHP result. Then you need to use done() to receive the data on the client side and work with it from there:
$.ajax({
url:"pages/welcome_get.php",
type: "POST",
dataType: "json",
cache: false,
data: { wm_val: wel}
}).done(function( json ) {
// do something with json here
});
You need to add the "JSON" header to your "pages/welcome_get.php" file:
header("Content-Type: application/json");
And also in your AJAX call remember to add the "success" and "error" callback:
jQuery.ajax({
url:"pages/welcome_get.php",
type: "POST",
dataType: "json",
cache: false,
data: { wm_val: wel}
success: function(response) {
//Do stuff with response here
}
error: function(){
//Display error msg or something like that
}
});
lets say you are checking user in your welcome_get.php
then in your welcome_get.php
use this
if(isset($_GET['wm_val'])){
$wm_val = $mysqli->real_escape_string($_GET['wm_val']);
$check_user = $mysqli->prepare("SELECT email FROM members WHERE username = ? LIMIT 1 ");
$check_user->bind_param('s', $wm_val);
$check_user->execute();
$check_user->store_result();
$check_user->bind_result( $email);
$check_user->fetch() ;
if ($check_user->num_rows == 1) { $datas['msg']= "failed" ;}
else{$datas['msg']= "success" ;}
$check_user->close() ;
echo json_encode($datas);
}
and your ajax
$.ajax(
// do an ajax to send value to the database...
{
url:"pages/welcome_get.php",
type: "POST",
dataType: "json",
cache: false,
data: { wm_val: wel},
success: function(msg) {
if (data.msg == 'success'){
// do what you like here example
$('#mydata').html("<span >you welcome ! </span>").delay(4000).fadeOut('fast');
}else{
//do something else example
$('#mydata').html("<span >failed ! </span>").delay(4000).fadeOut('fast');
}
})

Access Data from Database with Ajax

I have tried just about every solution and I know my database returns values if i hard code in the php so the issues is returning the values or determining if it is actually sent as post. It gets to the success function and alerts Worked but I am getting undefined for Item ID.
$(document).ready(function() {
$('.check').click(function(){
alert("Step1");
var thisID = $(this).attr('id');
alert(thisID);
$.ajax({
type: "GET",
url: "XXX.PHP",
data: { "ID": thisID},
cache: false,
async:true,
datatype: "json",
success: function(data)
{
alert("WORKED");
alert(data.ItemID);
}
});
});
});
Here is the PHP
if(isset($_POST['ID']))
{
$ID = $_POST['ID'];
function retrieve($ID)
{
$stmt = $mysqli->query("SELECT * FROM database1.menu WHERE ItemID = $ID");
if($stmt->num_rows) //if there is an ID of this name
{
$row = $stmt->fetch_assoc();
echo $row;
print json_encode($row);
}
}
You could try something like this. Not sure if this is what you need.
$(".check").click(function(event){
event.preventDefault();
var $this = $(this);
var thisID = $this.attr('id');
$.ajax({
type: "POST",
url: base_url + "url.php",
data: {
id: thisID
},
dataType: "text",
cache:false,
success:
function(data){
alert(data);
}
});
return false;
});

jQuery ajax fetch data from PHP into a jQuery variable

I need to receive the status of vote using ajax and php and jquery. Following is my code :
var VoteStatus= GetStatus() ;
var ID = $('#ID').val();
function GetStatus() {
var res = '';
$.ajax({
type: "POST",
data: {VoteID:ID} ,
url: "/vote_status.php",
async: false,
success: function(result) { res=result; }
});
return res;
}
alert('Vote Status= ' + VoteStatus);
In my php file:
$VoteID = $_POST['VoteID'];
$Property = $_POST['Property'];
if ( $VoteID == 0 )
echo 'No Vote provided - Property = '. $Property;
exit;
The alert box shows: Vote Status = No Vote Provided
Please help.
I have posted the VoteID, but the php file doesn't seem to receive it.
Try the alert in here and check if its working
$.ajax({
type: "POST",
data: {"VoteID":ID} ,
url: "/vote_status.php",
async: false,
success: function(result) {
alert(result); }
});
The name of the POST variable needs to be in quotes, as in
data: {"VoteID":ID}
Try this and check jquery ajax manuals
$.ajax({
type: "POST",
data:"VoteID=" + ID +"&secondparam=" + secondvalue,
url: "/vote_status.php",
async: false,
success: function(result) { alert(result); }
});

Categories