Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda