Ajax Jquery - simple request does not answer - php

It's been some time I am looking for an answer but as far as today, nobody found :
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...
In a precedent question, I was told to put some delimiters because of Smarty like so :
$('#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});
But, nothing changed,
Anybody has an idea ?
Best, Newben

Problem can be that you are passing data as javascript array, but it should be a map or string as mentioned here.
Try this :
<script >
$('#my-btn').click(function() {
var comment = $('#id1').val();
var m = {$id2};
$.post('<?php echo $postURL ?>', {0 : comment,1 : m}, function(callback_data){
alert('hello');
});
});
</script>

Related

passing an array to a .php file using post in jquery

I am trying to post an existing array in jquery using $.post to a evaluate.php page. I tried to serialize it and send it. But its not working. I get a internal server error. Can somebody suggest a method for me to post my array? Very much in need of help.
<?php
function radioevaluation($qnum) {
echo "<script> $(document).ready(function(){
var x= ".$qnum.";
$('#q'+x+'verify').click(function(){
var answerarray = [];
answerarray['answer'] =$('input[name=q'+x+']:checked').val();
answerarray['qnum']= x;
var answer =jQuery.param(answerarray);
$.post('evaluate.php',answer,function(data){
var a= data;
alert(a.result);
}, 'json');
});
});</script>";
}
?>
EDIT:
Sorry I missed your second comment.
I think you want to define answerarray not as an array but like so:
var answerarray = {};

Ajax jQuery post troubles

I am having trouble with a bit of code that uses the ajax $.post function to send a name to a php file which then does some stuff with it. I believe the issue to be in the ajax code, because I have found that the posted value never makes it to the $_POST array (i.e. the $_POST array is not set). However, I do not think that there are any syntactical mistakes on either end, so I am confused as to why it does not work.
Here's the jQuery.
if ($(e.target).hasClass('shootNames')) {
var shootName = $(e.target).attr('id');
var par = $('#' + shootName).parent().attr("id");
$.post("displayImages.php", {shoot: shootName}); //the information I would like to send
$('#' + par).load("displayImages.php").off('click', $('#' + par));
}//end if hasClass
And the relevant bit of php.
if (isset($_POST['shoot'])) {
$shootname = $_POST['shoot'][0]; //pick out just the first member of the $_POST array
$filepath = "images/u/$foo/$shootname";
$f->FilesAsImages($filepath);
}//end if
Thanks for any help.
You're hitting displayImages.php once in your $.post call and there it should return your image, but you don't have a success handler in there, so it does nothing with the result. You're then hitting displayImages.php again with .load and not passing it anything, so it does nothing as expected. Adding a success handler to your $.post call and doing the work in there, will do what you want.
See the $.post documentation for more: http://api.jquery.com/jquery.post/
Sample code:
if ($(e.target).hasClass('shootNames')) {
var shootName = $(e.target).attr('id');
var par = $('#' + shootName).parent().attr("id");
$.post("displayImages.php", {
shoot: shootName
}, function(data, textStatus, jqXHR) {
// this is the success function and the 3 arguments it gives you, remove the 2nd/3rd if you don't use them
$('#' + par).html(data).off('click', $('#' + par));
});
}
Try change from this
$.post("displayImages.php", {shoot: shootName}); //the information I would like to send
to this
$.post("displayImages.php", {'shoot[]': shootName}); //the information I would like to send
Source :
http://api.jquery.com/jquery.post/

Pass a variable from a get query in javascript

I am trying to pass the value of a parameter outside a $.get jQuery in javascript. I read that I should somehow do that in synchronous mode but I couldn't find a solution.
Here is my code:
var flag;
var t = $.get("mutalyzer.php?id="+variation.value, function(data) {
flag=data;
});
document.write(flag);
where I get undefined as a result.
Thanks!
write it inside the callback function
try this
var t = $.get("mutalyzer.php?id="+variation.value, function(data) {
//this is the callback function and runs when get gets completed
flag=data;
document.write(flag);
});

AJAX NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument jquery.js:7065

I'm new to jQuery and Ajax and I have run into a problem. Im getting the error on my console that:
NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument # http://localhost
/jquery.js:7065
Why am I receiving this error?
this is the code Im Using:
function upload_file(){
var file = document.form1.file_upload;
var date = document.form1.date_added;
var author = document.form1.author;
var user = document.form1.user;
var semester = document.form1.semester;
var class1 = document.form1.class;
var subject = document.form1.subject;
$.ajax({
type:"get",
url:"upload_file.php",
data:{
"file":file,
"date":date,
"author":author,
"user":user,
"semester":semester,
"class":class1,
"subject":subject
},
success:function(result){
$("#result").html(result);
}
});
}
Im waiting for your replies.
PS: I Did search the forum but did not get what i want, so if i missed something, sorry in advance.
I think the problem is you are trying to pass complete objects to the JSON.
You should use values instead of objects. For example, replace:
var subject = document.form1.subject;
with:
var subject = document.form1.subject.value;
Use this , i guess bracket mismatch --
$.ajax(
{
type:"get",
url:"upload_file.php",
data:{
"file":file,
"date":date,
"author":author,
"user":user,
"semester":semester,
"class":class1,
"subject":subject
},
success:function(result)
{
$("#result").html(result);
}
);
I ran into the same error, but my problem was different.
Turns out, I was passing a parameter in the ajax call that wasn't present in my DOM at all.
In #ZackValentine-s case (or for someone reading this in the future), please check the value of all the data items you are about to pass to the ajax call, BEFORE the actual call itself.
We had the same error.
Upgraded to the latest version of JQuery and problem solved.
This one seems to work for some people seen that solution here too

ajax jquery unsolved

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>

Categories