Pass PHP variable in onclick="history.go(-1)"

Asked

Viewed 1,448 times

0

I’m trying to create a button that returns to the form to fix it, thus keeping all the content typed in the HTML for the user to correct the wrong information, but sending a variable PHP so I can do the UPDATE in the Mysql, without entering a new record. I thought I’d send a ID:

<!DOCTYPE html>          
<html>
<body>
    <form>
        <input type="hidden" name="ID" value="<?php echo $ID; ?>"/>
        <input type="submit" formaction="cancela_cadastro.php" 
               value="Cancelar pedido">
        <input type="submit" formaction="../index.php" value="novo pedido">
        <input type="button" value="Corrigir" 
            onClick="(history.go(-1), '<?php echo $ID; ?>')"/>
    </form>

but without success. The ID is inserted into "cancela_cadastro.php?ID=XX" and in the "../index.php?ID=XX" - The latter unintentionally, but it makes no difference. I wonder if there is a way to send this variable on history.go(-1), or if there is a way to send this variable back to the completed form.

PS. The history.go(-1) stopped working when I assigned this variable PHP.

  • if Voce wants it to return because it does not validate the form ?

  • Thank you for the answer, I think I had an idea with her. Instead of returning to the submission, I show the fields filled in and ask the user to correct or actually submit the form... Of course! Thank you very much!

  • Just to complement, it is not possible to pass other parameters in the method go() of window.history. He only accepts one int or a string containing a URL.

  • Thank you for the answers. I think I will have an "intermediary" form, between the sending and the confirmation form.

2 answers

1

Instead of:

<input type="button" value="Corrigir" onClick="(history.go(-1), '<?php echo $ID; ?>')">

I suggest:

<input type="button" value="Corrigir" onClick="parent.location.href='pagina_destino.php?id=<?php echo $ID; ?>'">
  • I tested your suggestion, but the original form came back blank. I would like to pass the "ID" keeping what was typed earlier, but less like the "back" button of the browser.

  • On the previous screen the files were not saved in the database?

0


Instead of doing history.go(-1) create a function where you pass the ID and redirect it to the page you need for example:

<script>
function redirect(id){

    window.location.href = 'suapagina.php?id='+id;

}
</script>

If you want to add the page parameter in the method, you can reuse it in other places if necessary.

  • I’m going to test it here. Thank you for the answer!

  • You are welcome, if you are useful and can vote and accept

Browser other questions tagged

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