Passing third data field using Ajax is not working - ajax

I'm using a rating script that passes values to the database using Ajax, everything works up until I decided to pass the variable Make. I'm not sure why its not working. I have also tried using data: 'postID='+postID+'&ratingNum='+ratingNum +'&Make='+Make,
But that is also not working.
$(function() {
$('.rate input').on('click', function(){
var postID = <?php echo $postData['id']; ?>;
var ratingNum = $(this).val();
var Make = <?php echo $postData['Make']; ?>;
$.ajax({
type: 'POST',
url: 'https://techhum.com/rating.php',
data: {
'postID': postID,
'ratingNum': ratingNum ,
'Make': Make
},
// data: 'postID='+postID+'&ratingNum='+ratingNum +'&Make='+Make,
dataType: 'json',
success : function(resp) {
if(resp.status == 1){
$('#avgrat').text(resp.data.average_rating);
$('#totalrat').text(resp.data.rating_num);
alert('Thanks! You have rated '+ratingNum+' to "<?php echo $postData['Name']; ?>"');
}else if(resp.status == 2){
$('#show_notification').html('<div class="alert alert-success alert-dismissible">×You have already rated this product.</div>');
$('#subject').val('');
$('#comment').val('');
$('#error_div').html('');
}
$( ".rate input" ).each(function() {
if($(this).val() <= parseInt(resp.data.average_rating)){
$(this).attr('checked', 'checked');
}else{
$(this).prop( "checked", false );
}
});
}
});
});
});

Related

Ajax alert inside function not works

First alert : alert('<?php echo t_lang('M_FRM_PLEASE_WAIT_WHILE_FILE_OR_IMAGE_IS_BEING_UPLOADED_._IF_FILE_SIZE_IS_BIG_IT_CAN_TAKE_SOME_TIME_._PLEASE_DO_NOT_CLOSE_TAB');?>'); does not work unless I put second alert out of function call: alert();
$('input[name="btn_submit"]').click(function second(){
var step = <?php echo $_REQUEST['step']?>;
var dealid= <?php echo $_GET['edit']?>;
var ur="imagecheck.php?dealid="+dealid;
if (step == 1) {
$.ajax({
url: ur,
type: "GET",
header: {'Access-Control-Allow-Origin': '*'},
success: function(y) {
if(y=='true'){
alert('<?php echo t_lang('M_FRM_PLEASE_WAIT_WHILE_FILE_OR_IMAGE_IS_BEING_UPLOADED_._IF_FILE_SIZE_IS_BIG_IT_CAN_TAKE_SOME_TIME_._PLEASE_DO_NOT_CLOSE_TAB');?>');
} else {
alert(dealid);
}
}
});
alert(); // if I don't add this first alert is not showed up.
}
});
I tried almost everything but still can not understand why first alert relies on second alert. Any help would be highly appreciated.
I am adding the codes below which is all about ajax in my file.
<script type="text/javascript">
$(document).ready(function(){
$('input[name="btn_submit"]').click(function first(){
var com_id=<?php echo $_SESSION['logged_user']['company_id']?>;
var co_type=$('#co_type').val();
var ur="check_no_of_deals.php?company_id="+com_id;
if(co_type==0){
$.ajax({
url: ur,
type: "GET",
header: {'Access-Control-Allow-Origin': '*'},
success: function(t) {
if(t == 'true'){
} else {
alert('<?php echo t_lang('M_FRM_YOU_HAVE_ALREADY_ADDED_DEAL_TODAY');?>');
window.location.href='http://deals.az/merchant/company-deals.php';
}
}
});
}
});
//bizim kod basladi
$('input[name="btn_submit"]').click(function second(){
var step = <?php echo $_REQUEST['step']?>;
var dealid= <?php echo $_GET['edit']?>;
var ur="imagecheck.php?dealid="+dealid;
if (step == 1) {
$.ajax({
url: ur,
type: "GET",
header: {'Access-Control-Allow-Origin': '*'},
success: function(t)(y) => {
if(t=='true'){
alert('<?php echo t_lang('M_FRM_PLEASE_WAIT_WHILE_FILE_OR_IMAGE_IS_BEING_UPLOADED_._IF_FILE_SIZE_IS_BIG_IT_CAN_TAKE_SOME_TIME_._PLEASE_DO_NOT_CLOSE_TAB');?>');
} else {
alert(dealid);
}
}
});
// alert();
}
});
//bizim kod bitti
});
</script>
try replacing to arrow function
$.ajax({
...
success: (y) => {
if(y=='true'){
...
}
}
...
})
the difference between an arrow function and a regular function is by their this
so when you pass the regular function it becomes part of the ajax call or the Jquery object.
but the arrow function stays where you see it.

Ajax: I need to pass the value from php script file to html file

I need to get the value from the PHP script file to HTML in ajax. Need to append the response condition wise.
Here is my code:
$(document).ready(function(){
var collectionarrayValues = <?php echo json_encode($subcatId); ?>;
$.each(collectionarrayValues, function(index, value) {
var ajaxurl = '<?php echo $block->getBaseUrl(); ?>ajax/index/shopallnew/collection/'+value;
$.ajax({
//async: false,
url:ajaxurl,
type:'POST',
dataType:'HTML',
beforeSend: function () { showLoader(); },
success:function(response){
if (response != '')
{
hideLoader();
jQuery(".appendCollection").removeClass("load");
jQuery('.appendCollection').append(response);
}
}
});
});
});
I want to add the condition like this
if(data ==test1){
jQuery('.appendCollection1').append(response);
}else if(data ==test2){
jQuery('.appendCollection2').append(response);
}
Test1 and test2 will come from PHP file
Thanks

How can we check data in controller sent by ajax (not in response) in codeigniter?

I want to send array data from javascript to a view page..But the array isn't getting passed to the view page. The codes are described below:
//array getting passed from this page
<input type="button" name="b">
<script src="<?php echo base_url().'assets/js/' ?>jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("input[type='button']").click(function(){
question_array=[];
question_array['what is our nationality?']='Bangladeshi';
question_array['what is our national fish?']='Ilish';
number=10;
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "admin/test",
dataType: 'json',
data: {question_array: question_array,number:number},
success: function(result) {
if (result) {
window.location="<?php echo base_url().'admin/test_page'; ?>";
}
}
});
});
});
</script>
`
In my controller::
public function test()
{
$question_array = $this->input->post('question_array');
$number = $this->input->post('number');
$this->session->set_flashdata('number', $number);
$this->session->set_flashdata('question_array', $question_array);
echo json_encode($number, $question_array);
}
function test_page()
{
$this->load->view('admin/test_page');
}
//The page where I'm sending the array(Question_array not found)
$number = $this->session->flashdata('number');
$question_array = $this->session->flashdata('question_array');
echo $number;
print_r($question_array);
Hope this will help you :
Convert array to object and remove spaces between array keys, like this :
<script type="text/javascript">
$(function(){
$("input[type='button']").click(function(){
var question_array = [];
question_array['what-is-our-nationality'] = 'Bangladeshi';
question_array['what-is-our-national-fish'] = 'Ilish';
var number = 10;
var obj = $.extend({}, question_array);
jQuery.ajax({
type: "POST",
url: "<?php echo base_url('admin/test'); ?>",
dataType: 'json',
data: {question_array : obj, number : number},
success: function(result) {
if (result)
{
window.location="<?php echo base_url().'admin/test_page';?>";
}
}
});
});
});
</script>
You may better try JSON.stringify to solve this,
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "admin/test",
dataType: 'json',
data: JSON.stringify({question_array: question_array,number:number}),
success: function(result) {
if (result){
window.location="<?php echo base_url().'admin/test_page'; ?>";
}
}
});
I hope the code below can help you. You need post the string i correct JSON format by using stringify. The server will receive the json and decode it to object.
function send(){
var url = "http://localhost/some_url_here";
var data = JSON.stringify({
Bangladeshi: 'what is our nationality?',
Ilish: 'what is our national fish?'
});
$.ajax({
url: url,
type: "POST",
dataType: "json",
data: data,
}).done(function(output){
alert('output is '+ JSON.stringify(output));
});
}
function receive()
{
$result = '';
$stream_clean = $this->security->xss_clean($this->input->raw_input_stream);
$request = json_decode($stream_clean);
$Bangladeshi = $request->Bangladeshi;
$Ilish = $request->Ilish;
$result = array("status" => 200, "id" => $Bangladeshi+$Ilish);
header('Content-Type: application/json');
echo json_encode($result);
}

how to use ajax in magento backend

why ajax call from magento backend(from .phtml) redirects to magento dashboard despite of sending form key properly with form? please help.
in ajax call :
var dataRecord = j('#newForm').serialize();
var url = "<?php echo $this->getUrl('*/*/addNewColumn') ?>";
j.ajax({
type: "POST",
url: url,
data: {data1: dataRecord}
})
.done(function( msg ) {
alert(msg);
});
It worked when i sent the form_key in 'data' of ajax call.
var dataRecord = jQuery('#newForm').serialize();
var url = "<?php echo $this->getUrl('*/*/addNewColumn') ?>";
<?php $k = Mage::getSingleton('core/session')->getFormKey(); ?>
jQuery.ajax({
type: "POST",
url: url,
data: {data1: dataRecord,form_key:'<?php echo $k ?>'}
})
.done(function( msg ) {
alert(msg);
});
Below are the code for ajax in admin panel.
jQuery(".btn_save_email").click(function(){
var data1val = jQuery('.data1val').val();
var data2val = jQuery('.data2val').val();
url = '<?php echo $this->getUrl('moduleName/adminhtml_controllerName/functionName') ?>';
new Ajax.Request(url, {
parameters: {isAjax: 1, method: 'POST',data1:data1val,data2:data2val},
onSuccess: function(transport) {
jQuery('.class').html(transport['responseText']);
}
});
});
var aurl = '<?php echo $this->getBaseurl(); ?>/films/test/ajax';
var data = {};
data['lsize'] = jQuery('#mySlider').slider("values", 0);
data['rsize'] = jQuery('#mySlider').slider("values", 1);
jQuery.ajax({
type: "POST",
url:aurl,
dataType :'json',
data:{ Data },
success:function(response){
jQuery('#output').html(response);
}
});

Yii: get result for ajax request

I have a anchor;
When I click on it, the following code executes:
<script>
$('.viewcnp').click(function() {
event.preventDefault();
var r = prompt("Enter your password:");
if (r) {
$.ajax({
type: "POST",
url: '<?php echo Yii::app()->baseUrl; ?>' + '/index.php/admin/user/viewCnp/id/' + '<?php echo $model->id; ?>',
data: {'password': r},
success: function(data) {
$('.cnpdecrypted').text(data);
},
dataType: 'text'
});
}
});
</script>
this code makes a request to this action:
public function actionViewCnp($id) {
$model = User::model()->findByPk($id);
if ($model) {
return '{"data":"' . Utils::decrypt_cnp($model->cnp) . '"}';
}
else
return false;
}
If in the action I echo the cnp decripted, its there, but i can't set the value because I don't receive it on success.
You should simply use echo instead of return :
if ($model) {
echo Utils::decrypt_cnp($model->cnp);
}

Resources