Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
Passing values from JS to PHP and Echo back HTML

I am studying a code and need help to learn further.

The JS code below is from a Q/A html form that sends data to server. It crosschecks answer and on server and scores the user.

I need help writing a PHP code that receives the inputs using GET method and echo "ok" back to the html.

JS:

var xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function() {
                        if(this.readyState == 4 && this.status == 200) {
                            if(this.responseText == "ok") {
                                document.getElementById("messagefont").innerHTML = checkans;
                                document.getElementById("answer").value = "";
                                document.getElementById('submit-btn').value = continuebutton;
                                checkans.focus();

PHP:

<?php
header('Content-Type: application/json');
$errors = array ();

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' );
if (isset ($_GET['answer'])) {
    $answer = $_GET["answer"];
}
else {
    array_push ($errors, no);
}
$response = "";
if (sizeof ($errors) > 0) {
    $response = implode (",", $errors);
}
else {
$response = ok;
}
echo json_encode($response);
?>

I seem not to be getting something right with the PHP, please i need assistance/guide to fix the PHP code rightly.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!