Script give user token error on OpenCart 2.3.0.2 - php

This Script is not working on opencart 2.3.0.2 can any one help to translate this?
Is there a problem with user token?
<script type="text/javascript"><!--
function refudt(ele){
var pid = $(ele).data('pid');
var type= $(ele).data('type');
$.ajax({
url: 'index.php?route=sale/order/getprice&user_token=
{{ user_token }}',
dataType: 'html',
type: 'post',
data: 'order_id={{ order_id }}&pid=' + pid+'&type='+type,
success: function(html) {
$(ele).parent().append('<br>'+html);
}
});
}
function saver(ele){
var pid = $(ele).data('pid');
var price = $(ele).parent().find('input').val();
var type= $(ele).data('type');
confirm('Save new value: '+price +' ?');
$.ajax({
url: 'index.php?route=sale/order/getprice&user_token=
{{ user_token }}',
dataType: 'html',
type: 'post',
data: 'order_id={{ order_id }}&pid=' + pid+'&value='+price+'&type='+type,
success: function(html) {
$(ele).parent().remove();
if($('.rebus').length<1) {
location.reload();
}
}
});
}
//--></script>

you use twig function for OC2.3 version. This OC version does not suport twig. And instead user_token should be just token. Script you need change to:
<script type="text/javascript"><!--
function refudt(ele){
var pid = $(ele).data('pid');
var type= $(ele).data('type');
$.ajax({
url: 'index.php?route=sale/order/getprice&token=<?php echo $token; ?>,
dataType: 'html',
type: 'post',
data: 'order_id=<?php echo $order_id; ?>&pid=' + pid+'&type='+type,
success: function(html) {
$(ele).parent().append('<br>'+html);
}
});
}
function saver(ele){
var pid = $(ele).data('pid');
var price = $(ele).parent().find('input').val();
var type= $(ele).data('type');
confirm('Save new value: '+price +' ?');
$.ajax({
url: 'index.php?route=sale/order/getprice&token=<?php echo $token; ?>,
dataType: 'html',
type: 'post',
data: 'order_id=<?php echo $order_id; ?>&pid=' + pid+'&value='+price+'&type='+type,
success: function(html) {
$(ele).parent().remove();
if($('.rebus').length<1) {
location.reload();
}
}
});
}
//--></script>
Also do not forget that token should be defined on corresponding controller file like this: $data['token'] = $this->session->data['token'];

Related

Codeigniter 405 method not allowed AJAX JQuery

I am facing on problem, I have made a CRUD code with AJAX for my CI page, it is working fine on XAMPP server but when I uploaded it to Live Server (Godaddy), it's not fetching data from database and showing 405 Method not Allowed error.
http://www.fenxteksolutions.com/admin/metalinks
Here is my Code.
<script>
$(function(){
showAllEmployee();
//Add New
$('#btnAdd').click(function(){
$('#myModal').modal('show');
$('#myModal').find('.modal-title').text('Add New Meta Detail');
$('#myForm').attr('action', '<?php echo base_url() ?>admin/addEmployee');
});
$('#btnSave').click(function(){
var url = $('#myForm').attr('action');
var data = $('#myForm').serialize();
//validate form
var empoyeeName = $('input[name=txtEmployeeName]');
var address = $('textarea[name=txtAddress]');
var meta_tag = $('input[name=txtMetaTag]');
var meta_desc = $('input[name=txtMetaDesc]');
var result = '';
if(empoyeeName.val()==''){
empoyeeName.parent().parent().addClass('has-error');
}else{
empoyeeName.parent().parent().removeClass('has-error');
result +='1';
}
if(address.val()==''){
address.parent().parent().addClass('has-error');
}else{
address.parent().parent().removeClass('has-error');
result +='2';
}
if(meta_tag.val()==''){
meta_tag.parent().parent().addClass('has-error');
}else{
meta_tag.parent().parent().removeClass('has-error');
result +='3';
}
if(meta_desc.val()==''){
meta_desc.parent().parent().addClass('has-error');
}else{
meta_desc.parent().parent().removeClass('has-error');
result +='4';
}
if(result=='1234'){
$.ajax({
type: 'ajax',
method: 'post',
url: url,
data: data,
async: false,
dataType: 'json',
success: function(response){
if(response.success){
$('#myModal').modal('hide');
$('#myForm')[0].reset();
if(response.type=='add'){
var type = 'added'
}else if(response.type=='update'){
var type ="updated"
}
$('.alert-success').html('News '+type+' successfully').fadeIn().delay(4000).fadeOut('slow');
showAllEmployee();
}else{
alert('Error');
}
},
error: function(){
alert('Could not add data');
}
});
}
});
//edit
$('#showdata').on('click', '.item-edit', function(){
var id = $(this).attr('data');
$('#myModal').modal('show');
$('#myModal').find('.modal-title').text('Edit Employee');
$('#myForm').attr('action', '<?php echo base_url() ?>admin/updateEmployee');
$.ajax({
type: 'ajax',
method: 'get',
url: '<?php echo base_url() ?>admin/editEmployee',
data: {id: id},
async: false,
dataType: 'json',
success: function(data){
$('input[name=txtEmployeeName]').val(data.page);
$('textarea[name=txtAddress]').val(data.title);
$('input[name=txtMetaTag]').val(data.meta_tag);
$('input[name=txtMetaDesc]').val(data.meta_desc);
$('input[name=txtId]').val(data.id);
},
error: function(){
alert('Could not Edit Data');
}
});
});
//delete-
$('#showdata').on('click', '.item-delete', function(){
var id = $(this).attr('data');
$('#deleteModal').modal('show');
//prevent previous handler - unbind()
$('#btnDelete').unbind().click(function(){
$.ajax({
type: 'ajax',
method: 'get',
async: false,
url: '<?php echo base_url() ?>admin/deleteEmployee',
data:{id:id},
dataType: 'json',
success: function(response){
if(response.success){
$('#deleteModal').modal('hide');
$('.alert-success').html('Employee Deleted successfully').fadeIn().delay(4000).fadeOut('slow');
showAllEmployee();
}else{
alert('Error');
}
},
error: function(){
alert('Error deleting');
}
});
});
});
//function
function showAllEmployee(){
$.ajax({
type: 'ajax',
url: '<?php echo base_url() ?>admin/showAllEmployee',
async: false,
dataType: 'json',
success: function(data){
var html = '';
var i;
for(i=0; i<data.length; i++){
html +='<tr class="odd gradeX">'+
// '<td>'+data[i].id+'</td>'+
'<td>'+data[i].page+'</td>'+
'<td>'+data[i].title+'</td>'+
'<td>'+data[i].meta_tag+'</td>'+
'<td>'+data[i].meta_desc+'</td>'+
'<td>'+
'Edit'+
'</td>'+
'<td>'+
'Delete'+
'</td>'+
'</tr>';
}
$('#showdata').html(html);
},
error: function(){
alert('Could not get Data from Database');
}
});
}
});
</script>
This code is working very well in XAMPP Server and giving all data from database and all functions are working very well, but on hosting it is not fetching data and when I inspect the page in console it gave 405 method not allowed error.
Your error is in type parameter in ajax. Remove it or set it to GET. It is actually request method
$.ajax({
type: 'GET',
url: '<?php echo base_url() ?>admin/showAllEmployee',
async: false,
dataType: 'json',
success: function(data){
.......................

Pagin With Ajax, Post no working

I Use this code for paging
function changePagination(pageId,liId,modul,userid){
$(".flash").show();
var dataModul = 'modul='+modul;
var dataUser = 'userid='+userid;
var dataString = 'pageId='+ pageId;
$.ajax({
type: "POST",
url: "loadDataVerifikasi.php",
data: dataString,
data: dataModul,
data: dataUser,
cache: false,
success: function(result){
$(".flash").hide();
$(".link a").removeClass("In-active current") ;
$("#"+liId+" a").addClass( "In-active current" );
$("#pageData").html(result);
}
});}
And this for an action file with name loadDataVerifikasi.php
if(isset($_POST['pageId']) && !empty($_POST['pageId'])){
$id=$_POST['pageId'];
}
else{
$id='0';
}
$pageLimit=PAGE_PER_NO*$id;
$query="select * from pengajuan_".$_POST['modul']." join verifikasi on pengajuan_".$_POST['modul'].".ketkdpengajuan = verifikasi.ketkdpengajuan where kdadmin = '".$_POST['userid']."' group by kdverifikasi limit $pageLimit,".PAGE_PER_NO;
But $_POST['modul'] post nothing,
I am not familiar with your syntax.
Please try:
function changePagination(pageId,liId,modul,userid){
$(".flash").show();
var datastring = {
modul: modul,
userid: userid,
pageId: pageId
}
$.ajax({
type: "POST",
url: "loadDataVerifikasi.php",
data: dataString,
cache: false,
success: function(result){
$(".flash").hide();
$(".link a").removeClass("In-active current") ;
$("#"+liId+" a").addClass( "In-active current" );
$("#pageData").html(result);
}
});
}
The $.ajax data attribute accepts an object with key-value pairs
I believe it is because you overwrite the data with dataUser.
I haven't tested i but the code below should work.
Also check out this for more info
function changePagination(pageId,liId,modul,userid){
$(".flash").show();
var dataModul = 'modul='+modul;
var dataUser = 'userid='+userid;
var dataString = 'pageId='+ pageId;
$.ajax({
type: "POST",
url: "loadDataVerifikasi.php",
data: { 'dataString':dataString, 'dataModul':dataModul, 'dataUser':dataUser },
cache: false,
success: function(result){
$(".flash").hide();
$(".link a").removeClass("In-active current") ;
$("#"+liId+" a").addClass( "In-active current" );
$("#pageData").html(result);

Call Ajax Again in for loop

I want to request ajax many time in for loop . But it only return 1 value . result.ID. Can anyone help me?
$('#show').html('');
var min = parseInt($('#number_coupon_from').val());
var max = parseInt($('#number_coupon_to').val());
var total_time = parseInt($('#time_coupon').val())*1000;
var randcoupon = Math.floor(Math.random()*(max-min+1)+min);
var timetb = total_time/randcoupon;
var random = 0;
var i,j;
for(i=0;i<randcoupon;i++)
{
for(j=random;j>=0;j--)
{
if(j==0)
{
$.ajax({
url: 'backend/publish_auto/update',
type: 'post',
data: {},
dataType: 'json',
success: function (result)
{
if(result.st)
{
$('#show').append('Update Coupon ID: '+result.ID+'<br />');
}
}
});
}
}
}
});
Thank you !!
use async:False to get ajax response one by one
$.ajax({
url: 'backend/publish_auto/update',
async: false,
type: 'post',
data: {},
dataType: 'json',
success: function (result)
{
if(result.st)
{
$('#show').append('Update Coupon ID: '+result.ID+'<br />');
}
}
});

Using AJAX form submit to submit and retrieve data from MySQL

Basically, I'm trying to make it so that when a post is submitted to my site, it sends the post using AJAX so that they don't change page, and then if the AJAX post is successful, retrieve all the posts for said user from MySQL and write them onto the page.
My problem is that the browsers (Chrome, IE) are completely ignoring the AJAX request.
My form:
<div id="updatestatus">
<form action="" method="post" id="ps">
<textarea name="status" id="status"></textarea>
<input type="hidden" name="uid" id="uid" value="<?php echo $uid; ?>" />
<input type="submit" id="poststatus" name="poststatus" value="Share" />
</form>
</div>
My AJAX request:
$(function() {
$("#poststatus").click(function() {
var status = $("textarea#status").val();
if (status == "") {
return false;
}
var uid = $("input#uid").val();
var dataString = 'status='+ status + '&uid=' + uid;
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid=<?php echo $uid; ?>",
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>);
return false;
}
});
return false;
});
});
});
});
My ajax/query.php request
<?php
//connect stuff
$uid = strip_tags(stripslashes(htmlspecialchars(htmlentities(mysql_real_escape_string($_GET['uid'])))));
$result = mysql_query("SELECT * FROM mingle_status WHERE uid = '$uid' ORDER BY timestamp DESC"); //query
$array = mysql_fetch_row($result); //fetch result
echo json_encode($array);
?>
Thanks in advance for any help - Joe
In this section of JS code
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid=<?php echo $uid; ?>",
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>);
return false;
}
});
return false;
});
});
You need to remove the ending bracket after the curly brace which follows the last return false, such as...
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid=<?php echo $uid; ?>",
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>");
return false;
}
});
return false;
};
});
Try this
$(function() {
$("#poststatus").click(function() {
var status = $.trim($("#status").val());
if (status == "") {
return false;
}
var uid = $("#uid").val();
var dataString = 'status='+ status + '&uid=' + uid;
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid="+<?php echo $uid; ?>,
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>);
return false;
}
});
return false;
}
});
});
});

make jQuery post more than one query

i have a jQuery code that posts an ID to another php file
<script type="text/javascript">
$(function() {
//More Button
$('.more').live("click",function() {
var ID = $(this).attr("id");
if(ID){
$("#more"+ID).html('<img src="moreajax.gif" />');
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID,
cache: false,
success: function(html){
$("ol#updates").append(html);
$("#more"+ID).remove();
}
});
}else{
$(".morebox").html('The End');
}
return false;
});
});
</script>
How can I make it post another query along with "last message id"? I have a MySQL query that will output a value (pid). How can I post this along with "last message id"?
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID + "&pid=" + your_pid,
cache: false,
success: function(html){
$("ol#updates").append(html);
$("#more"+ID).remove();
}
});
Here is my solution:
var salji = {
rate: score,
image_id: img_id
};
$.ajax({
type: "POST",
url: "widgets/rate.php",
data: salji,
success: function(msg){
alert('wow');
}
});

Categories