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

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

Related

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

Jquery set PHP $_GET array

Greetings Stackoverflow
How do I set the php $_GET[] array from Jquery? I have a string looking simmilar to this: $sample_string = "Hi there, this text contains space and the character: &";. I also have a variable containing the name of the destination: $saveAs = "SomeVariable";. In PHP it would look following: $_GET["$SomeVariable"] = $sample_string;.
How do I do this in Jquery?
Thanks in advance, Rasmus
If you're using jQuery, you'll have to set up an AJAX request on the client side that sends a GET request to the server. You can then pull the data you supplied in the request from the $_GET[] array on the server side.
$(function() {
var data = {
sample_string: "hi",
saveAs: "something"
};
$.get('/path/to/script.php', data, function(response) {
alert(response); // should alert "some response"
});
});
And in script.php:
<?php
$sample = $_GET['sample_string']; // == "hi"
$saveAs = $_GET['saveAs']; // == "something"
// do work
echo "some response";
?>
Can't tell if you're looking to grab a GET param from javascript or set a GET param from jQuery. If it's the former, I like to use this code (stolen a while back from I can't remember where):
var urlParams = {};
(function () {
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
})();
Then you can call
var cake = urlParams['cake'];
To get the $_GET param specified by http://someurl.com?cake=delicious
If you want to send a $_GET parameter, you can use either jQuery's $.get() or $.ajax() functions. The $.get function is more straightforward and there's documentation on it here http://api.jquery.com/jQuery.get/
For $.ajax you would do something like this:
var trickystring = "Hi there, this text contains space and the character: &";
$.ajax({
url:'path/to/your/php/script.php',
data: {
'getParam1':trickystring,
'getParam2':'pie!'
},
type:'GET'
});
Now in PHP you should be able to get these by:
$trickystring = $_GET['getParam1'];
$pie = $_GET['getParam2'];
Hope these examples GET what you're looking for. (Get it?)
if $sample_string is what you want in jquery, you can do
var sample_str = '<?php echo $sample_string; ?>'; and then use the js variable sample_str wherever you want.
Now, if you want to do set $_GET in jquery, an ajax function would be way to go.
$.ajax({
url:'path/to/your/php_script.php',
data: {
'param1': 'pqr',
'param2': 'abc'
},
type:'GET'
});
Do you mean that would look like $_GET[$saveAs] = $sample_string y think.
$_GET is a variable for sending information from a page to another by URL. Its nosense to do it in jQuery that is not server side. If you want to dynamically set the $_GET variable to send it to another page you must include it in the URL like:
/index.php?'+javascriptVariable_name+'='+javascriptVariable_value+';
$_GET is just a URL parameter. So you can access get like /index.php?id=1:
echo $_GET['id'];
Look at this article, it shows all the ways to load stuff with ajax:
http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/

Ajax Jquery - simple request does not answer

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>

passing parameter with js/ajax to a php document

$(document).ready(function()
{
$(":input").focusout(function () {
var a= $(this).closest("tr").attr('id');
var b= $(this).closest("td").attr('id');
var data = $(this).attr("value");
$.post("database.php",
{trAdress: a, tdAdress: b }, function(data){ alert("Data Loaded: " + data); }); });});
I want to pass trAdress and tdAdress also data parameters to a php document... Can anyone help me how can I get these parameters in a php document_?
They will be set in the $_POST array as $_POST['trAdress'] and $_POST['tdAdress'].
If you sent them using POST, you can access the values sent in the form using $_POST superglobal variable:
<?php
$trAddress = $_POST['trAddress'];
$tdAddress = $_POST['tdAddress'];
// your code here
http://php.net/manual/en/reserved.variables.post.php
$_POST["trAdress"] and $_POST["tdAdress"] will get you the values

Categories