问题
Below is the code, I am new in cs cart and unable to figure out how can I get the ajax call from tpl in php file. I want to implement a file upload feature into the admin panel of cs cart. hooks/order_management/upload_data.tpl `
$(document).ready(function(){
alert("doc ready");
$("#btn_ok").click(function( event ) {
event.preventDefault();
var val = $("#myfile").val();
var data = "data=" + val;
$.ajax({
type: "POST",
url: '../../../app/addons/ugw_upload_form/controllers/backend/order_management.post.php',
data: data,
cache: false,
success: function(response)
{ console.log(response);}
});
});
});
</script>
<div class="control-group">
<input type="text" name="myfile" id="myfile" />
<input type="button" id="btn_ok" name="submit" value="upload" >
</div>`
controller/backend/order_management.post.php
`
if (!defined('BOOTSTRAP')) { die('Access denied'); }
use Tygh\Registry;
Registry::get('view')->assign('test_var', "Test");
Registry::get('view')->assign('uploadhere', "upload the file here");
if(isset($_POST['data'])){
$textvalue = $_POST['data'];
$handle = fopen("Z:\members.txt", "w");
fwrite($handle, $textvalue);
fclose($handle);
}
?>`
回答1:
The reason for the redirection of url in case of ajax request is the fn_get_route() method in app\function\fn.control.php
function fn_get_route(&$req, $area = AREA){
:::
Registry::set('runtime.controller', !empty($parts[0]) ? basename($parts[0]) : 'index');
Registry::set('runtime.mode', !empty($parts[1]) ? basename($parts[1]) : 'index');
::
}
fn.commom.php
function fn_redirect(){
::
if (defined('AJAX_REQUEST')) {
// make in-script redirect during ajax request
$_purl = parse_url($location);
$_GET = array();
$_POST = array();
if (!empty($_purl['query'])) {
parse_str($_purl['query'], $_GET);}
$_REQUEST = Bootstrap::safeInput($_GET);
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = $_purl['path'];
$_SERVER['QUERY_STRING'] = !empty($_purl['query']) ? $_purl['query'] : '';
fn_get_route($_REQUEST);
::
}
回答2:
For ajax request in cs-cart you can see: http://docs.cs-cart.com/4.3.x/core/front-end/ajax.html
for example you want to send data over ajax:
Your demo.tpl
file:
<form class="cm-ajax" method="post">
<input id="elm_id" type="submit" name="dispatch[your_controller.your_mode]" value="submit" />
</form>
your_controller.php
:
if($mode == your_mode){
if (defined('AJAX_REQUEST')) {
// your operation
// return or exit;
}
}
来源:https://stackoverflow.com/questions/32823964/how-can-i-send-an-ajax-request-to-controller-backend-php-file-from-tpl-file-in-c