I have problem with ajax response in prestahop.
I wrote module with method
cheaperproduct.php
public function GetData(){
if(Tools::getValue('method')) echo "asdasd";
echo "string";
}
and in main.js
jQuery(document).ready(function(){
$('#cheaper form').submit(function(){
var url = $("#cheaper .url").val();
var email = $("#cheaper .email").val();
$.ajax({
url: baseDir+'modules/cheaperproduct/cheaperproduct.php',
method:"post",
data:'method=GetData',
success: function(data){
alert(data);
}
})
return false;
})
})
And I have empty alert.
I checked many ways to resolve my problem, but no one help me.
Can somebody explain me what's wrong?
try this:
public function GetData(){
if(Tools::getValue('method')) die(json_encode('ciao'));;
echo "method not found!";
}
Related
i have an array like
[{"id_pelanggan":"1","id_tipe":"5","nama_pelanggan":"ASD","alamat_jalan":"DS","alamat_kota":"1","alamat_provinsi":"ATA","kontak_pelanggan":"45454"}]
and my ajax
$(function() {
$("#autocomplete").change(function(){
var namaagen = $("#autocomplete").val();
$.ajax({
url: '<?php echo site_url('Pelanggan/tampil_where'); ?>',
type: 'POST',
dataType: 'json',
data: {
'namaagen': namaagen
},
success: function (agen) {
$("#napem").val(agen['nama_pelanggan']);
}
});
});
});
});
});
controller :
public function tampil_where(){
$nama = $this->input->post('namaagen');
$query = $this->pelanggan_m->tampilwhere($nama);
echo json_encode($query);}
models:
public function tampilwhere($nama){
$this->db->select('*');
$this->db->from('nm_pelanggan');
$this->db->where('nama_pelanggan',$nama);
$this->db->order_by('id_pelanggan', 'ASC');
$query = $this->db->get()->result_array();
return $query;
}
but nothing happen and i already add alert(agen['nama_pelanggan']); in response success and show alert undefined pls help , thanks for advance
Please change your alert code, right now you don't access response array in the proper way:-
alert(agen[0]['nama_pelanggan']);
Instead of:-
alert(agen['nama_pelanggan']);
Thanks
I have been struggling for days. I am doing ajax callback by assigning an input field but I am unable to too. Below is the code:
Script
$('[name="acccode[]"]').each(function(idx,val){
var acccode = $('[name="acccode[]"]').eq(idx).val();
console.log(acccode);
$.ajax({
url : "<?php echo base_url(); ?>readacccode",
type: "POST",
data:"Acc_Code="+acccode,
dataType: "JSON",
success: function(data)
{
$.each(data.results,function(idx,val){
console.log(val);
$('[name="accname[]"]').val(val);
});
},
error: function (data)
{
swal("Oops", "We couldn't connect to the server!", "error");
}
});
});
I am successfully able to get each acc code. But not the name.
Here is the Controller code:
public function readacccode()
{
if (!$this->ion_auth->logged_in() || $this->ion_auth->is_store())
{
redirect('account/login');
}
$acccode = $this->input->post('acccode');
$data['results']=$this->Setting->get_acccode($acccode);
echo json_encode($data);
}
it is getting the acc name where the acc code matches.
Please advise.
Thanks, everyone the problem is resolved. For those wanting to know here is the thing I change.
public function readacccode()
{
if (!$this->ion_auth->logged_in() || $this->ion_auth->is_store())
{
redirect('account/login');
}
$acccode = $this->input->post('Acc_Code'); //All I did is replace acccode to Acc_Code
$data['results']=$this->Setting->get_acccode($acccode);
echo json_encode($data);
}
This is my OpenCart code I don't know what the problem in this code is, but it cant return the value from php ajax page. I tried many ways it wont work so please help me for this problem .
Here is my ajax in OpenCart:
<script type="text/javascript">
$('.list-group-item').on('click', function () {
var href = $(this).attr("href");
var arr = href.split('path=');
var cat_id = arr[1];
//alert("hai");
$.ajax({
type: 'post',
url: 'index.php?route=ajax/ajaxcat/index',
data: format_data,
dataType: 'json',
success: function (json) {
if (json['success']) {
//$('#messages').html(json['success']);
alert(json['success']);
}
}
});
});
</script>
Here is my php code:
<?php
class ControllerAjaxAjaxcat extends Controller {
function index() {
$json['success'] = 'This is the returned results color_adjust.php';
$this->response->setOutput(json_encode($json));
}
}
?>
I don't know what is wrong with that code it cant return the value.
i am using codeigniter. I have set a form validation via jquery and after validation the data /control is not moving on to the Controller.
Here is my jquery code
var data1 = {
username:$("#username").val(),
password:$("#password").val()
}
$.ajax({
type:'POST',
url:base_url+"site/chk_info",
data:data1,
success: function (response){
alert (response);
},
error:function (){
alert ("Sorry we are getting problems plz try again latter");
}
}); // end ajax
Here is my controller method.
public function chk_info(){
return true;
}
The problem is in jquery the controll in not entering into the success function it always getting into the error function.
chk_info() is only returning true IF PHP was listening and acting upon it.
You should be echo-ing true like this instead:
public function chk_info(){
echo 'true';
}
try this i think its url problem
$.ajax({
type:'POST',
url:'<?php echo base_url() ?>site/chk_info',
data:data1,
success: function (response){
alert (response);
},
error:function (){
alert ("Sorry we are getting problems plz try again latter");
}
});
I am having little problem with ajax. Here is my code what I have done so far:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
getLocation();
});
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(position)
{
latitude =position.coords.latitude;
longitude= position.coords.longitude;
$.post({ url: 'test.php',
data : ({lat :latitude,long:longitude}),
success: function(data){
alert('done');
}});
}
What I am trying to do is call the function on document ready and trying to retrieve the value of ajax call on the same file where I did call this script. Whenever I reload the page, I don't get any value of :
print_r($_POST['lat']);
I don't know what is the actual problem,I already checked in console and didn't get any error. Please help.
To get the value you need to print it in success callback
$.post({ url: 'test.php',
data : ({lat :latitude,long:longitude}),
success: function(data){
alert(data);
}
});
and if you put
if (isset($_POST['lat']) {
print_r($_POST['lat']);
}
you will get the value in alert box.
EDIT: You can check in php if the reqest came from Ajax using this:
if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
if (isset($_POST['lat']) {
print_r($_POST['lat']);
}
exit();
}
if you put this at the begining you will see only that print_r from ajax call.
The value you are printing is not an array. So you have to print it like,
print $_POST['lat'];
(OR)
echo $_POST['lat'];
AND NOT
print_r($_POST['lat']);
If you want to print all post values mean then you can print it as,
print_r($_POST);
I have solved my problem by own. Here what I have done. I made one more file called test2.php and put my php script over there and call that file in my ajax call. Here the code.
Test.php
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
getLocation();
});
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(position)
{
latitude =position.coords.latitude;
longitude= position.coords.longitude;
$.ajax({ url: 'test2.php',
data : ({lat :latitude,long:longitude}),
type: 'post',
success: function(data){
if(data=='true'){
window.location = "http://example.com";
}
}
});
}
</script>
Test2.php
//just for example
<?php
echo 'true';
?>
Hope this will help someone in future.