Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda