This a question that synthesizes two questions that remain unlsoved :
I am trying to implement some ajax method to post some comments on a webpage. I use the ajax method like such :
<button id="my-btn">Make an Ajax request!</button>
<script >
$('#my-btn').click(function() {
var comment = $('#id1').val();
var m = {$id2};
var data = new Array();
data[0]= comment;
data[1]= m;
$.post('{$postURL}', data, function(callback_data){
alert('hello');
});
});
</script>
where m = {$id2}; is due to a smarty variable.
The alert('hello') works, but the php code is not processed : {$postURL} requires a method comment(){$comment = $_POST[$data[0]]; $m = $_POST[$data[1]];...}. So, postURL is like : "index.php?post=comment", and the method is comment(). Of course, when I replace {$postURL} by "index.php?post=comment", nothing happens in the sense that I still have the alert('hello') message but the method comment() doesn't process anything. Is this method evenc called ? Or is there a wrong syntax such that $_POST[$data[0]] and $_POST[$data[1]] aren(t recognized bt the comment() method.
The way index.php works is to redirect : to a another php page, call it mypage.php where we can find the comment() method.
Moreover, something very weird : when I corrupt $.post("{$postURL}" by $.post("{$whatever}", I still have a alert('hello') message ! And weirder, when I put an alert(callback_data); inside the callback function, I get a huge alert message, which consists of my whole php code...
Best, Newben
Smarty uses those curly braces so you need to pop them in using {ldelim} and {rdelim}, also you aren't sending post data as an assoc array so $_POST['comment'] etc won't be set - try this instead:
<script>
$('#my-btn').click(function() {ldelim}
var comment = $('#id1').val();
var m = {$id2};
var data = {ldelim}
comment: $('#id1').val(),
m: {$id2}
{rdelim};
$.post("{$postURL}", data, function(callback_data){ldelim}
alert('hello');
{rdelim});
{rdelim});
</script>