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

226
Visualizações
form validaiton on form submit without page reload

I have very simple html form created with one button in it.

<form method="post" id="my_unique_form_id">
      <button type="submit" name="'.$name.'" value="'.get_the_ID().'" class="'.$class.'"></button>
</form>

Then I connected it with AJAX

jQuery(document).ready(function() {
    jQuery('#my_unique_form_id').on('submit', function(event) {
        event.preventDefault();
        jQuery.ajax({
            type: "post",
            url: "",
            data: jQuery('#my_unique_form_id').serialize(),
            success: function() {
                console.log('not reloading the page');
            }
        })
    });
});

So the code above works in a way, that on form submit page does not realod, which is great, but of course I don't get any results I need and nothing is happening.

If I don't use AJAX, everything works perfectly and I get the expected results, but then page is reloading, which I need to avoid. I don't really know how to combine it.

This is php function I am using:

  1. I stored name in a variable (coming from button)

     $name = my_function() ? 'remove' : 'add';
    
  2. my_function setup

     function my_function( $post_id = 0, $user_id = 0 ){
    
         if ( ! $post_id ) {
             $post_id = get_the_ID();
         }
         if ( ! $user_id ) {
             $user_id = get_current_user_id();
         }
         $meta_value = get_data( $user_id );
         return array_search( $post_id, $meta_value ) !== false;
     }
    
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

What you need to do is in your jQuery code, pass the data retrieved from the call to the success function like so:

success: function(data){
    //Do something
}

That data variable will then contain whatever PHP has echo'd. If you want to access that data in some meaningful way, it will be easiest if you prepare it as an array in PHP i.e. like so:

$dataForjQuery = array('status' => 'OK', 'message' => 'validation passed');
echo(json_encode($dataForjQuery));

this way in jQuery you would do:

success: function(data){
    console.log('the status is: ' + data.status); //Will log 'the status is: OK'
    console.log('message from server: ' + data.message); //Will log 'message from server: validation passed'
}
over 4 years ago · Santiago Trujillo Relatório
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