Avoid redirecting with JavaScript

0

I have this js and this query

    $("#sub").click( function(event) {

     $.post( $("#myForm").attr("action"), 
     $("#myForm :input").serializeArray(), 
     function(info){ $("#result").html(info); 

        });
      });

      $('#myForm').submit(function() {
      return false;
      });

-processAppoint.php

           <?php
         session_start();
        require_once "includes/config.php";

    $app = Aplicacion::getSingleton();
    $mysqli = $app->conexionBd();

     $id = $_POST['idOcio'];
     $rn = $_POST['idUser'];


        $sql = "INSERT INTO partecipantes (ID_Ocio, ID_Usuario, 
        Nombre_Publico)
                VALUES ('$id', '$rn', '1')";
       // $mysqli->query($sql) or die ($mysqli->error. " en la línea ". 
       (__LINE__));
    //header('Location: ../ocio.php');
    if($mysqli->query($sql)) echo "Successfully Inserted";
    else
      echo "Insertion Failed";
       ?>

But every time I insert redirects me to processApuntarse.php, when I want to stay on the page where I clicked the button. This page is this (the important part) -the_located.php

          <form id="myForm" action="codesincss/procesarApuntarse.php" 
        method="post">
            <input type="hidden" name="idOcio" value='<?php echo "$id";?>'/> 
            <input type="hidden" name="idUser" value='<?php echo "$rn";?>'/> 
            <button id="sub" class='w3-button w3-dark- 
            blue'>Apuntarse</button>
            </form>
    
asked by junemadrid 11.06.2018 в 17:50
source

1 answer

1

Remove the event $ ("# sub"). click () and only use the submit event

$('#myForm').submit(function(e) {
    e.preventDefault(); //Evita que la página actual vaya a la url del action
    $.post(this.action, $(this).serializeArray(), function(info) {
        $("#result").html(info); 
    });
});
    
answered by 11.06.2018 в 22:00