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

152
Visualizações
after js validation and redir data is lost and not avalibale for php validation

Hi I cant figure out why it is not working I have simple form and want to validate in client side with simple xmlrequest redirect to other page, do there server validation and work with validated parametar. all in js vanila and pure php .

html:

<form  action=""  novalidate="novalidate" autocomplete="off"  id="name-group">
   <div class="form-group">
      <label for="number">*Number:</label>
      <input value="" type="text" name="number" id="number" class="form-control">
    </div>
     <input type="submit" value="Submit" name="submit" id="submit" class="btn btn-primary">
</form>

js validation:

document.getElementById("submit").addEventListener('click', function (event) {
    event.preventDefault();
    let number = document.getElementById("number").value;

    let numberRegex = "^[0-9]$";
    let errorMsg = '';
    if (number.match(numberRegex)) {
        let xhr = new XMLHttpRequest();
        let params = "number=" + number;
        xhr.open("POST", './uploadValidation.php',true)
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
        // xhr.onload = function () {
        // }
        xhr.send(encodeURI(params));
        window.location.assign('./uploadValidation.php')
    } else {
        errorMsg = 'required number field';
    }
    document.getElementById("errorMsg").innerHTML = errorMsg
});

after this I want to redirect to other page and do server validation

<?php

//errors
$errors = [
    'number' => '',
];
var_dump($_POST);
//validation
if (isset($_POST['submit'])) {
    $number = htmlentities(trim($_POST['number']), ENT_QUOTES, 'UTF-8');
    echo true;

    //check for error in userName field
    if (!$number || mb_strlen($number) < 1) {
        $form_valid = false;
        $errors['number'] = '*required field';
    }
}

?>

and the data is lost $_POST['number'] is not avalibale Please can some explain me why it is not working and how to fix?

EDIT: I Want to get input from user in first page and after js and php validation work with the input in second page.in my case add dynamicly inputs field (the number of fields = number from first user input).

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