Sending variable with ajax - php

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/

Related

Opencart - Way to call ajax request in Admin Panel

I want to fetch some data using ajax request in Opencart Admin panel. I have simply created a controller which only returns data in json format. Using below code:
$.ajax({
type: 'post',
url: 'index.php?route=common/ajaxdata/functionName&token=<?php echo $token; ?>',
data: data,
dataType: 'json',
success: function(json) {
if (json['success']) {
alert(json['success']);
}
}
});
Can anybody please let know whether this is correct or any other built in functionality available in Opencart for ajax requests? So that I can proceed and implement this in my rest of the pages.
Thanks
$.ajax({
type: "POST",
url: "http://your url write hare",
contentType: "application/json",
dataType: "json",
data: JSON.stringify({
"propertytypename": property,
"propertytypecreatedby": property_name
}),
success: alert("success"),
error: alert("Fail")
});

$.ajax and method post not work correctly

I have a simple code-form like this:
$.ajax({
url: "http://myurl/test.php",
type: "POST",
dataType: "html",
data: {
action: "login"
},
success: function (data) {
console.log(data)
}
});
When on PHP server page, I try to print $_POST["action"] yet this field is blank. Why does it work fine if I use $.post(url,data,function)?
$.ajax({
url: "http://myurl/test.php",
type: "POST",
dataType: "html",
data: {
method: "login"
},
success: function (data) {
console.log(data);
}
});
login is a php function
You may use method instead of type.
$.ajax({
url: "http://myurl/test.php",
method: "POST", // <-- Use method, not type
dataType: "html",
data: {
action: "login"
},
success: function (data) {
console.log(data)
}
});
Problem is solved.....
There is a problem with the callback on success event...and yesterday I focused my attention only on the request php in the developers tool....when I used Curl (with the right request) all work correctly and I forgot to check client code in success callback....
Never use post method with developers console,it's very easy to lose more time :=)

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

Display the result after it submit through Ajax

When submitted the results from my form and insert the values in sql after that i want message to display without refresh the page. Or probably refresh automated without user hit refresh.
on succes part you can display result.
$.ajax({
url: "file.php",
data: "a=" + id,
type: "POST",
success: function(data) {
$("#anyid").html(data);
}
});
$(document).ready(function(){
$('#button').click(function(){
$.ajax({
type: "POST",
url: 'form.php',
// your data - this gets id="title" and put it on $_POST['val1']
data: {val1:$('#title').val()},
// .done sends returned data to div id="bs"; you can use .append instead of .html for overwriting the data.
}).done(function(data){
$('#bs').html(data);
})
});
}
);
you can use different function of jquery to show message if your require is success
$.ajax({
url: "test.php",
data: "user_id=" + user_id,
type: "POST",
success: function(data) {
$('#message_div_id').text("Your message").show();
}
});

Jquery - Send variables to a controller Action via GET in AJAX

All,
I want to send a variable "itemId" via GET to a controller action through AJAX. In the Controller Action, I should be able to retrieve the value using $_GET["itemId"];
Can I send the querystring with "data" tag instead of appending it to the "url"?
I have the following code:
$.ajax({
type: 'GET',
url: "/controller/controlleraction",
data: itemId,
cache: false,
dataType: "html",
success: function(html_input)
{
alert(html_input);
}
});
How can I do this?
data: {itemId: itemId},
$.ajax({
type: 'GET',
url: "/controller/controlleraction",
data: ({itemId: itemId}),<------change it to this
cache: false,
dataType: "html",
success: function(html_input)
{
alert(html_input);
}
});
Make itemId a JavaScript object before making the AJAX request. For example:
var itemId = {'itemId': 1000};
data: {itemId: "you info"},
or
data: "itemId=you info",

Categories