Ajax response from controller

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>
```<br />
<br />
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.<br />
<br />
I am kindly thankfull for a explaination how to do it!<br />
<br />
Kind regards

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

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.

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

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.

Great thx for this info, putting the 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!

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.

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 ?

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.

  • 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:[list]

    []SMS-Number => Input Textbox

    [
    ]SMS-Text => Textarea

    [*]Send-Button => a href with class “cm-ajax”

    [/list]

    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 ?

I've never worked with microformats, but I know how I'd solve this:[list=1]

[]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.

[/list]

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…

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

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




Ship From:











[b]Controller (get_qoute.php) :[/b]

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;
}

[b]View (get_rates.tpl) :[/b]

{php}echo get_rates; {/php}

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



[font=courier new,courier,monospace][color=#282828]Registry::get('ajax')->assign('get_rates', $val);[/color][/font]



[color=#282828][font=arial, verdana, tahoma, sans-serif]but if you want set a Smarty variable, that you will use in your template, you should write:[/font][/color]



[font=courier new,courier,monospace]Registry::get('view')->assign([color=#282828]'get_rates', $val);[/color][/font]



[color=#282828][font=arial, verdana, tahoma, sans-serif]and then in get_rates.tpl just output it this way:[/font][/color]



[font=courier new,courier,monospace][color=#282828]{$get_rates}[/color][/font]



[color=#282828][font=arial, verdana, tahoma, sans-serif]Regards,

2LM[/font][/color]

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



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) :




Quick Quote!






Width:







Height:











[b]Controller code:[/b]


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





Thanks in advance.

Hi aine,

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

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:-

{_("State")}:
Select state {foreach from=$dym_states item="states"} {$states.state_name} {/foreach}

{_(“District”)}:



Select district
{foreach from=$dym_districts item=“districts”}
{$districts.district_name nofilter}
{/foreach}

ajax code

If any errors and modifications,Please help them.

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..

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).

hi ,ia m try something like this topic.

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