2

i have a javascript array with values like "correct", "wrong". I want to send this array to my php file using ajax. But im not familiar with how. This is what i have tried so far..and i would like to do this without jquery.

var hold=[];
    for(t = 0;t <= 10; t++){ 
    answers_Arr = document.getElementsByName("question"+t);
    for (k = 0; k < answers_Arr.length; k++){
        if(answers_Arr[k].checked){
            hold[t] = answers_Arr[k].value;
            //document.getElementById("test"+t).innerHTML = "Value: "+ hold[t];
            break;
        }else{
            hold[t] = "no";
            continue;

        }
    }
}
var jsonString = JSON.stringify(hold);
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
    if (xmlhttp.readyState==4  &&  xmlhttp.status==200)
    {
    document.getElementById("test1").innerHTML= xmlhttp.responseText;
    }
}

xmlhttp.open("GET","result.php?res="+hold[],true);
xmlhttp.send();

}

2 Answers 2

1

Here is an example of sending the json string with POST through AJAX:

var jsonString = JSON.stringify(hold);
var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {
    //
}

xmlhttp.open("POST","result.php",true);
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.send(jsonString);

Avoid sending json strings with GET method because if the request string gets too long, the browser will throw you an error.

Now in your php script you will have all the data in $_POST

Edit: Looks like on some versions of php, the requests that have other Content-type, like application/json, the $_POST has an empty value. To fix this you can do:

$_POST = json_decode(file_get_contents('php://input'));
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. Can u help me with the php script for retrieving this too?
You should have all the data in $_POST like you would send it through a normal form. If you are unsure what the variable contains, you can always print the contents of it with print_r($_POST);
i tried using print_r($_POST); but it only prints 'array()'. Thats it.
I am curious what version of php are you using? Can you also try this: $_POST = json_decode(file_get_contents('php://input'));
<?php include 'Connect.php'; $myarray= $_POST; print_r($_POST); This is what i tried
|
0

replace this line

xmlhttp.open("GET","result.php?res="+hold[],true);

with

xmlhttp.open("GET","result.php?res="+jsonString,true);

3 Comments

it still says undefined for 'res'. And also when i display the array in php., it displays null
you mean you are getting undefined in your php script?
This is what i used in my php script,'$res = json_decode(stripslashes($_GET['res'])); echo json_encode($res);'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.