404 on post request

I developed an addon that asks if the user want to register to a newsletter and if he does send him a coupon code by email.



I don't want to reload the page and have two information to pass to different places so I'm trying to use ajax post requests.



The external newsletter site receives the post request correctly, but the internal php does not.



First I tried creating a controller but I received a 502 permission denied, since every other file in the addon folder also generated it I assumed it was a security thing for cs-cart and gave up on it.



Tried putting it with the hooks in the skin folder but also generated a permission denied, apparently you can't access php files there too.



So I tried just sending it to index.php and getting it with a hook in a controller, problem is I get “mywebshop.com/index.php 404 (error not found)” in my console. What the hell?!

If I click on the link and open it does open mywebshop.com/index.php correctly, because like, it is the shop address!



What is wrong? How can I send an ajax post request and get it in my addon without medling with the file structure?

Ok, to anyone having the same problem index.php is not able to handle post requests, send it to pages.php or anything else and get it with a pre_controller.



But I'm still not able to send a mail, please check if my code is correct:




function fn_form_pre_form1 () {
close_first_visit();

$.ajax({
type: "POST",

url: "index.php?dispatch=pages.first_visit",

data: { email_to: document.getElementById('person_email_address').value, email_username: document.getElementById('person_como_gostaria_de_ser_chamado').value },
contentType: "application/json; charset=utf-8",
dataType: "json"
}).done(function( data ) {
if ( console && console.log ) {
console.log( "Sample of data:", data );
}
});

return true;
}




if ( !defined('AREA') ) { die('Access denied'); }
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($mode == 'first_visit') {
$email_to = $_REQUEST['email_to'];
if(empty($email_to) || $email_to == "") {
die();
}
$email_username = $_REQUEST['email_username'];
if(empty($email_username) || $email_username == "") {
$email_username = "Buyer";
}
$coupon_code = Registry::get('addons.first_visit.coupon_code');
$email_sender = Registry::get('addons.first_visit.email_sender');
$email_subject = Registry::get('addons.first_visit.email_subject');
$email_body = Registry::get('addons.first_visit.email_body');
$email_body = html_entity_decode($email_body);
$email_body = str_replace("%cupon%", $coupon_code, $email_body);
$email_body = str_replace("%usuario%", $email_username, $email_body);
$result = fn_send_mail($email_to, $email_sender, $email_subject, $email_body, $attachments = array(), $lang_code = CART_LANGUAGE, $reply_to = $email_subject, $is_html = true, $company_id = null);
echo $result;

return array(CONTROLLER_STATUS_OK);
}
}




Data returned:



[list]

[][color=blue]XHR finished loading: “[url=“http://localhost/asiatronic/index.php?dispatch=pages.first_visit”]http://localhost/asi...ges.first_visit[/url]”. jquery.min.js:16[/color]

[list]

[
]

[color=blue]send[/color]jquery.min.js:16

[]

[color=blue]d.extend.ajax[/color]jquery.min.js:16

[
]

[color=blue]$.ajax[/color]ajax.js:550

[]

[color=blue]fn_form_pre_form1[/color]index.php:1589

[
]

[color=blue]check[/color]core.js:2425

[]

[color=blue]$.extend.dispatchEvent[/color]core.js:670

[
]

[color=blue](anonymous function)[/color]core.js:1163

[]

[color=blue]d.event.handle[/color]jquery.min.js:16

[
]

[color=blue]k.handle.m[/color]jquery.min.js:16

[/list]

[/list]

[indent=1][color=#000000]Sample of data:







Object {[color=#881391]notifications[/color]: Array[0]}[/color][/indent]





I tried implementing in the GET too so I could check if it was a problem with POST but it also doesn't work:




if ($mode == 'first_visit') {
$coupon_code = Registry::get('addons.first_visit.coupon_code');
$email_sender = Registry::get('addons.first_visit.email_sender');
$email_subject = Registry::get('addons.first_visit.email_subject');
$email_body = Registry::get('addons.first_visit.email_body');

$email_body = html_entity_decode($email_body);
$email_body = str_replace("%cupon%", $coupon_code, $email_body);
$email_body = str_replace("%usuario%", $email_username, $email_body);

$result = fn_send_mail("eric.calmon@globalb2c.net", Registry::get('settings.Company.company_users_department'), $email_subject, $email_body, '', CART_LANGUAGE);

//fn_send_mail($user_data['email'], Registry::get('settings.Company.company_users_department'), 'addons/affiliate/approved_subj.tpl', 'addons/affiliate/approved_body.tpl');

return array(CONTROLLER_STATUS_REDIRECT, "$index_script?dispatch=" . print_r($result, true));
}




The $result is equal to null.

All other variables are correct.





Edit: Okay, apparently fn_send_mail only accepts templates as it's body and subject