Jump to content

  • You cannot start a new topic
  • You cannot reply to this topic

Ajax response from controller Rate Topic   - - - - -

 
  • lesan
  • Member
  • Members
  • Join Date: 06-Sep 13
  • 29 posts

Posted 24 November 2013 - 04:13 AM #1

Hello there,

I am trying to get a html response from a controller with a link cm-ajax request. My attempts failed :(

I created a new addon and orders.post.php to extend the controller with a new mode for the backend.
This mode gets called with a link in backend:

<a class="cm-ajax" data-ca-target-id="div_container" href="{'orders.new_mode'|fn_url}">ajax link</a>

If i execute fn_set_notification in the controller it works (it created a message popup), but I dont get the controller to give a response to the page it has been clicked for using it as html for the specified div_container id.

I am kindly thankfull for a explaination how to do it!

Kind regards

 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11291 posts

Posted 24 November 2013 - 09:53 PM #2

You don't have any form data to submit nor do you identify any result_ids.

EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • lesan
  • Member
  • Members
  • Join Date: 06-Sep 13
  • 29 posts

Posted 25 November 2013 - 02:39 AM #3

Hello tbirnseth,

actually the Orders.details allready have a form, thats why I didnt build one because html doesnt allow to put a from into a form.

data-ca-target-id="div_container
" will result in the query adding result_ids=div_container, so result id is added.
I have a input text field with id=my_input_id which I added with:

href="{'orders.new_mode?var1=my_input_id'|fn_url}

However this doesnt change anything to the fact I dont know how to get a response from controller.

 
  • lesan
  • Member
  • Members
  • Join Date: 06-Sep 13
  • 29 posts

Posted 25 November 2013 - 02:42 AM #4

how can I use the response_id element to receive data than ? The controller need to push data to the result_id ?

 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11291 posts

Posted 25 November 2013 - 09:17 PM #5

The way ajax works in cs-cart is that it renders the page identified in the 'mode' of the request. The javascript for cm-ajax will take this response and strip it to all but the result_ids (but you need a comment before the closing tag of the id of the tag). Those elements are then put into the DOM.

What is the XHR "response" in your browser? You really have to look at examples from existing code in the cart. Can't write a tutorial on the various options based on class names.

EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • lesan
  • Member
  • Members
  • Join Date: 06-Sep 13
  • 29 posts

Posted 03 December 2013 - 12:37 AM #6

Great thx for this info, putting the <!-- tag --> did gave me a html response into my div id container which was defined in the related controller view.

There is one big problem for sending form data: in the backend orders.manage I want to send some input textbox values + textarea values, but it is not possible to add them to a form due to the whole page allready is in a form (putting a form into a form does not work in html), is there a solution for it ?

This form is listed in the Addon Tab. The cm-ajax link should auto grab the current input values and send them by ajax.

Thank you very much tbirnseth!

 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11291 posts

Posted 03 December 2013 - 05:25 AM #7

Just add your fields and then use a checkout.pre.php controller to read the values and do what you need to do with them.
If you put them in the 'extra' array (like name="extra[my_fields][field1]") then they should be stored with the order data.

EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • lesan
  • Member
  • Members
  • Join Date: 06-Sep 13
  • 29 posts

Posted 05 December 2013 - 01:54 AM #8

this is orders.manage for backend. I just want to send the values to a custom created controller which executes a function with it.

The Problem is: how to auto add the input values (must be live-data as the text inputfield can be changed) to the cm-ajax request.
The Form-POST method doesnt work as the backend orders.manage allready is a form. Can I give a special name to the input fields so they get automaticly send with clicking the cm-ajax link => GET request ?

 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11291 posts

Posted 05 December 2013 - 04:06 AM #9

sorry, I must be thick. What you are wanting to do and your approach to doing are not making sense to me. Ajax and GET?
Why not just add your fields via a hook and then use a checkout.pre.php controller to capture their values when the checkout is posted in it's normal way? I think you are over complicating the problem. But maybe I'm missing something.

EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • lesan
  • Member
  • Members
  • Join Date: 06-Sep 13
  • 29 posts

Posted 08 December 2013 - 03:45 AM #10

- In Backend Dispatch: "orders.manage" I have added a new Addon to the the Addons Tab which shows 2 input fields and a send button:
  • SMS-Number => Input Textbox
  • SMS-Text => Textarea
  • Send-Button => a href with class "cm-ajax"
When clicking the send Button the SMS-Number & SMS-Text need to be given by ajax to the new Controller "mycontroller.sendsms". Which than executes a function defined in func.php and returns "OK" to the backend page.

Indeed it is possible to create a GET request in the cm-ajax link with onclick jquery fetching the values of the SMS-Number & SMS-Button. But this is not the easy way so I wonder if there is a fast solution ?

 
  • sserena
  • Junior Member
  • Members
  • Join Date: 15-Sep 07
  • 28 posts

Posted 08 December 2013 - 04:15 AM #11

I've never worked with microformats, but I know how I'd solve this:
  • Add your fields via hook to the admin
  • Add some javascript that makes use of $.ceAjax to send whatever you wanna send to PHP (see below)
  • Do whatever you want in PHP and attach your response to
    Registry::get('ajax')->assign('key', 'value');
    and
    exit;
    so it won't look for a template.
  • Use the callback of $.ceAjax to display the response in your template.
Here's a basic $.ceAjax function:

$.ceAjax('request', fn_url('dispatch.mode'), {
cache: false,
data: {'key': 'value','add': 'some more'},
callback: function(data) {
  // Do something with your data, such as $('#attach_me_here').append(data);
}
});

Hope this helps...

 

Posted 19 March 2015 - 07:46 AM #12

I always see Warning Access denied in backend page

$.ceAjax('request', fn_url('discussion.check_communication'), {
cache: false,
data: {'key': 'value'},
callback: function(data) {
console.log(data);
}
});

i donot know why like that can all of you show me about this problem because i just learn about cs cart.

Thanks

 
  • aine
  • Newbie
  • Trial users
  • Join Date: 14-May 15
  • 2 posts

Posted 14 May 2015 - 11:20 AM #13

Hi,I am new in cs-cart i want to create a form on front end and want to get data from ajax , I have added block from Design->Layout of home page.

I have created separate controller for this named get_qoute.php in and add view in

view/get_qoute/get_rates.tpl .

result is returning in console but I donot know how to show it on "quick-qoute" block which i added on home page layout. here is my code :

BLOCK

<form class="cm-ajax" action="index.php" method="post" id="quick_quote_form">
<div id="quick-quote">
<label style="width:73px;" >Ship From:</label>

<div class="input">
<input type="text" placeholder="enter zip code" name="f_company_name" id="f_company_name" class="required form-control">
</div>

<input type="submit" class="get-quote-btn" value="Get Quote" name="dispatch[get_qoute.get_rates]" />
</div>
</div>
</form>


Controller (get_qoute.php) :

<?php
use Tygh\Registry;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

if ($mode == 'get_rates') {

$to=$_POST["t_company_name"];
///all proceesses

$val="abc";

Registry::get('view');

Registry::get('ajax')->assign('get_rates', $val);


}
exit;
}

View (get_rates.tpl) :

{php}echo get_rates; {/php}

 
  • 2lm
  • Newbie
  • Members
  • Join Date: 30-Nov 14
  • 14 posts

Posted 14 May 2015 - 03:58 PM #14

@aine, if you want to set an Ajax response in your controller, you can call this method:

Registry::get('ajax')->assign('get_rates', $val);

but if you want set a Smarty variable, that you will use in your template, you should write:

Registry::get('view')->assign('get_rates', $val);

and then in get_rates.tpl just output it this way:

{$get_rates}

Regards,
2LM

2LM Effective E-commerce Websites
visit us: www.2lm.pl, our shop: shop.2lm.pl
contact us: contact@2lm.pl

 
  • aine
  • Newbie
  • Trial users
  • Join Date: 14-May 15
  • 2 posts

Posted 15 May 2015 - 11:33 AM #15

Hi, @2lm Thanks for your reply ,yes I set the same in controller Registry::get('ajax')->assign('get_rates', $val);

Actually I am trying to show my result set into html/smarty block which contains the form code which I added from backend

Design->Layout but it only shows response in console ,I am pasting of some code here

Block code (added from backend Design->Layouts) :

<div class="quick-quote">

<h3>Quick Quote!</h3>
<form class="cm-ajax" action="index.php" method="post" id="quick_quote_form">
<div id="quick-quote">

<div class="clearfix">
<label style="width:73px;" >Width:</label>
</div>

<div class="input">
<input type="text" placeholder="enter width" name="d_width" id="d_width" class="required form-control" >
</div>

<div class="clearfix">
<label style="width:73px;" >Height:</label>
</div>

<input type="submit" class="get-quote-btn" value="Get Quote" name="dispatch[get_qoute.get_rates]" />
</div>

<div class="result"></div>
<!-- tag -->
<input type="hidden" name="result_ids" value="result" />
</form>
</div>

Controller code:


<?php
use Tygh\Registry;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

if ($mode == 'get_rates') {


$height= $_POST["d_height"];
$width=$_POST["d_width"];


$val = $height+$width;


Registry::get('view');

Registry::get('ajax')->assign('get_rates', $val);


}
exit;
}
?>

Result set is coming correctly , Now I don't know how to show response in block div

<div class="result"></div>
<!-- tag -->
<input type="hidden" name="result_ids" value="result" />

Thanks in advance.

 
  • sam612
  • Newbie
  • Trial users
  • Join Date: 08-Aug 15
  • 3 posts

Posted 10 August 2015 - 05:28 AM #16

Hi aine,
Same issue with me I cant display response on my view .tpl

 
  • nagamahesh22
  • Advanced Member
  • Trial users
  • Join Date: 26-Mar 16
  • 50 posts

Posted 16 August 2016 - 01:13 PM #17

Hi

 

I want to dynamic drop down list of states,districts and centers using database. I have seleted one state and next drop-down displayed to related districts.My problem is "How to call controller using ajax". Please shown following code.

 

code:- 

<div class="control-group">
	<label class="control-label cm-required" for="state">{_("State")}:</label>
	<div class="controls">
				<select id="state" name="state" onChange="getState(this.value);">
						<option>Select state</option>
					 {foreach from=$dym_states item="states"} 
						<option  value="{$states.state_id}"> {$states.state_name} </option>
					 {/foreach}
				</select>
	</div>
</div>

<div class="control-group">

<label class="control-label cm-required" for="district">{_("District")}:</label>
	<div class="controls">
				<select id="district" name="district" onChange="getcity(this.value);">
						<option val=''>Select district</option>
				 {foreach from=$dym_districts item="districts"}
					      	<option value="{$districts.district_id}"> {$districts.district_name nofilter} </option>
				  {/foreach}
				</select>
	</div>
</div>

ajax code
<script>
function getState(val) {
       
$.ajax({
type: "POST",
url: "dmadmin.php?dispatch=profiles.main_centers",
data1:'state_id='+val,
success: function(data1){
alert(data1);
$("#district").html(data1);
}
});
 
}
</script>

 

If any errors and modifications,Please help them.



 
  • abcd
  • Advanced Member
  • Trial users
  • Join Date: 10-Sep 16
  • 52 posts

Posted 23 March 2017 - 06:07 AM #18

on the order detail page in admin panel there is a button new shipment, on clicking it popup opens.. i have added one button on it. on clicking that button it should go to controller and return back on the same popup with some value.. to do so what should i write in the return url?? or should i return the controller back to the popup??

Please help..



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11291 posts

Posted 23 March 2017 - 06:20 PM #19

Generally you set the result_ids variable in the POSTed data to be the id of the div you want to redraw when the AJAX request responds with html (template).


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • teosu
  • Senior Member
  • Members
  • Join Date: 09-Oct 14
  • 513 posts

Posted 08 November 2017 - 03:26 PM #20

hi ,ia m try something like this topic.

 

i cant get work keyup functions on input what can i do ?