Command not redirecting

Asked

Viewed 24 times

0

I am placing a command inside a script so that when the User presses the "Submit" button, it is recorded given in the database and soon after this happens a page redirection:

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

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

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

    }
    </script>

I save the data in the database, but in return there is no redirection.

  • Try header('Location:altCompras.php'); see if it meets the problem of redirecting. Remembering that this should be placed just below the insert php.

  • I tried that, but it didn’t work either

  • Here’s everything about PHP Directs: http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php

  • how "saved right" is mixing javascript function with php.?

1 answer

1

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

You have some 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 Place inside 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 php Header

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.