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
Related
I'm having some troubles trying to send an id through ajax script,
I'm trying to create an address book, when you select a contact it load all his information in another page and the ajax script load this page inside the main. Everything is working except to send the ID of the contact. There's a bit of the code :
$.ajax({
url: "select.php",
dataType: "html",
data: {id: id},
success: function(Result) {
$('#result').html(Result);
}
});
PHP
$id = $_POST['id'];
echo $id;
$sql = "SELECT * FROM ca11 WHERE contact_id= $id";
does anyone have an idea ?
you need to add which type of request you are making, as your question, you can call post request as below
$.ajax({
type: "POST",
url: "select.php",
dataType: "html",
data: {id: id},
success: function(Result) {
$('#result').html(Result);
}
});
For more information you can refer this link http://api.jquery.com/jQuery.post/
I´ve tried retrive some info from mysql for put it in a form,and update the info.
I use ajax call in the same page,I call ajax passing the id and php return all info.
if(isset($_POST["retriveForm"])) {
$data_json =array();
$id = $_POST['retriveForm'];
$sql = "SELECT * FROM mytable WHERE Id = $id";
while ($row = mysqli_fetch_array($db->consulta($sql)) {
$data_json = array('item1' => $row['item1'],'item2' => $row['item2']) ;
}
$data_json['item_array'] = call_a_function_return_array();//this works
echo json_encode($data_json);
}
ajax call shows like this
$(document.body).on('click', '.edit' ,function(){
var id = $(this).data('id');
$.ajax({
type: "POST",
url: "is_the_same_page.php",
data: {retriveForm : id},
success: function(response) {
$('#myForm').find('input').eq(1).val(response.item1);
}
});
});
When put .val(response.item1) get anything,but if I put response the input take all values
$('#myForm').find('input').eq(1).val(response);
shows
{"item1":"item1","item2":"item2","item_array":["item_in_array1","item_in_array2"]}
I also tried
var obj = jQuery.parseJSON(response);
Try setting $.ajax() dataType options to "json"
"json": Evaluates the response as JSON and returns a JavaScript
object.
$(document.body).on("click", ".edit", function() {
var id = $(this).data("id");
$.ajax({
type: "POST",
url: "is_the_same_page.php",
dataType: "json", // set `dataType` to `"json"`
data: {
retriveForm: id
},
success: function(response) {
$("#myForm").find("input").eq(1).val(response.item1);
}
});
});
I am trying to get my search bar working, however I am having issues with an ajax post.
For whatever reason, none of the data is being appended to the URL. I have tried various things with no success. I am attempting to send the data to the same page (index.php).
Here is my jquery:
$(function(){
$(document).on({
click: function () {
var tables = document.getElementsByTagName("TABLE");
for (var i = tables.length-1; i >= 0; i-= 1) {
if (tables[i]) tables[i].parentNode.removeChild(tables[i]);
}
var text = $('#searchBar').val();
var postData = JSON.stringify({ searchTerm: text });
$.ajax({
type: 'POST',
url: 'index.php',
dataType: 'json',
data: postData,
success: function() {
alert("Test");
}
});
}
}, "#searchButton");
});
And here is the php which I have with index.php:
<?php
require('course.php');
if(isset($_POST['searchTerm'])) {
echo $_POST['searchTerm'];
}
?>
No matter what I try, I am unable to get anything to post. I have checked the network tab in chrome, and I'm not seeing anything that indicates it's working correctly.
Any ideas?
EDIT:
I've changed my code to this, and it seems I'm getting closer:
$(document).on({
click: function () {
$("TABLE").remove()
var text = $('#searchBar').val();
$.ajax({
type: 'GET',
url: 'index.php',
dataType: 'text',
data: { searchTerm: text },
success: function() {
alert("Test");
}
});
}
}, "#searchButton");
And:
<?php
require('course.php');
if(isset($_GET['searchTerm'])) {
echo $_GET['searchTerm'];
}
?>
Now I am getting ?searchTerm=theTextIEnter as expected, however it's still not being echoed in index.php.
Do not use JSON.stringify() to convert object to string. data passed to $.ajax must be an object and not JSON.
For whatever reason, none of the data is being appended to the URL.
You are making a POST request. POST data is sent in the request body, not in the query string component of the URL.
If you change it to a GET request (and inspect it in the correct place, i.e. the Network tab of your browser's developer tools and not the address bar for the browser) then you would see the data in the query string.
This will work for you use data: { postData } on place of data:postData and you will receive your data in $_POST['postData']
$(function(){
$(document).on({
click: function () {
var tables = document.getElementsByTagName("TABLE");
for (var i = tables.length-1; i >= 0; i-= 1) {
if (tables[i]) tables[i].parentNode.removeChild(tables[i]);
}
var text = $('#searchBar').val();
var postData = JSON.stringify({ 'searchTerm' : text });
$.ajax({
type: 'POST',
url: 'index.php',
dataType: 'json',
data: { postData },
success: function(data) {
alert(data.searchTerm);
}
});
}
}, "#searchButton");
});
In index.php
<?php
if(isset($_POST['postData'])) {
echo $_POST['postData'];
die;
}
?>
If you want to send data via the URL you have to use a GET request. To do this, change the type of the request to GET and give the object directly to the data parameter in your jQuery, and use $_GET instead of $_POST in your PHP.
Finally note that you're not returning JSON - you're returning text. If you want to return JSON, use json_encode and get the value in the parameter of the success handler function.
Try this:
$(document).on({
click: function () {
$('table').remove();
$.ajax({
type: 'GET',
url: 'index.php',
dataType: 'json',
data: { searchTerm: $('#searchBar').val() },
success: function(response) {
console.log(response.searchTerm);
}
});
}
}, "#searchButton");
<?php
require('course.php');
if(isset($_GET['searchTerm'])) {
echo json_encode(array('searchTerm' => $_GET['searchTerm']));
}
?>
Remove dataType: 'json', from your AJAX. That is all.
Your response type is not JSON, yet by setting dataType: 'json' you're implying that it is. So when no JSON is detected in the response, nothing gets sent back to the method handler. By removing dataType it allows the API to make an educated decision on what the response type is going to be, based on the data you're returning in the response. Otherwise, you can set dataType to 'text' or 'html' and it will work.
From the manual:
dataType: The type of data that you're expecting back from the server.
This is NOT the type of data you're sending/posting, it's what you're expecting back. And in your index.php file you're not sending back any JSON. This is why the success() method is not satisfying. Always set the dataType to the type of data you're expecting back in the response.
With POST Request:
Please comment below line in your code:
//var postData = JSON.stringify({ searchTerm: text });
And use below ajax code to get the post-data:
$.ajax({
type: 'POST',
url: 'index.php',
dataType: 'json',
data: { searchTerm: text },
success: function() {
alert("Test");
}
});
With GET Request:
You can convert your data to query string parameters and pass them along to the server that way.
$.ajax({
type: 'GET',
url: 'index.php?searchTerm='+text,
dataType: 'json',
success: function(response) {
alert(response);
}
});
In response, You can get the data with alert, so you may get idea about the same.
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());
}
});
I am sending and retrieving some data with this ajax call and saving result in localstorage like this:
$.ajax({
url: "list.php",
data: {values: values},
dataType: "json",
success: function(result){
var dataToStore = JSON.stringify(result);
localStorage.setItem('key', dataToStore);
}
});
Then I am retrieving it in a separate PHP document like this and trying to add it to a PHP variable. I think the problem occurs because when I console.log, it logs 10 times or so. And I can't echo it in PHP. How do I pass it correctly?
<script>
var localData = JSON.parse(localStorage.getItem('key'));
$.each(localData, function(key, value){
console.log("This is the data that is stored", localData)
$.ajax({
type: 'post',
data: {localData},
dataType: "json",
success: function(result){
console.log(result)
}
});
</script>
<?php
$user_id = isset($_POST['localData'])?$_POST['localData']:"";
$verdier = implode(", ", $user_id);
?>
You are causing an asynchronous functionality of ajax() real pain by calling it in a loop
Why dont you use join() to join items with ", " in js like what you do in php
<script>
var localData = JSON.parse(localStorage.getItem('key')).join(", ");
$.ajax({
type: 'post',
data: {localData},
dataType: "json",
success: function(result){
console.log(result)
}
});
</script>
<?php
$user_id = isset($_POST['localData'])?$_POST['localData']:"";
$verdier = $user_id;
?>
You Should also cast user_id as an (int) if user_id need to be a single value and int
$verdier = (int) $user_id;