Command not redirecting

0

I'm putting a command inside a script so that when the user presses the "submit" button, the given data is written to the database and soon after that a page redirect happens:

<script type="text/javascript">
    function cadAlterar (){
        <?php 
              $idComp = (@$_POST['id']);  

              $insert = mysql_query("INSERT INTO alterar(idComp) VALUES ('$idComp')");

            ?>
         location.href="altCompras.php";

    }
    </script>

Save the data right in the Database, but in return there is no redirection.

    
asked by anonymous 16.11.2015 / 23:09

1 answer

1

You put the redirect inside a function, but did not execute it.

You have a few options:

1 - Put out of function:

<script type="text/javascript">
    <?php 
        $idComp = (@$_POST['id']);  
        $insert = mysql_query("INSERT INTO alterar(idComp) VALUES ('$idComp')");
    ?>
    document.location.href="altCompras.php";
</script>

2 Put in the function and execute it (redundant)

<script type="text/javascript">
    function cadAlterar (){
    <?php 
        $idComp = (@$_POST['id']);  
        $insert = mysql_query("INSERT INTO alterar(idComp) VALUES ('$idComp')");
    ?>
    document.location.href="altCompras.php";
}

    cadAlterar();
</script>

3 - Using the php Header

<?php 
    $idComp = (@$_POST['id']);  
    $insert = mysql_query("INSERT INTO alterar(idComp) VALUES ('$idComp')");   
    header("Location:altCompras.php");
?>
    
17.11.2015 / 02:49