简体   繁体   中英

How to pass a GET value when using history.back()?

I have a variable to check if the data is present already in the database. If the data is already present it would go back to the form page to input a new data. Here is what I have

<script type="text/javascript">
        window.history.back();
        window.location = 'register.php?msg='<?php echo 1;?>
</script>

Also I have tried this but I don't know how to pass it in the URL:

<script type="text/javascript">
        window.history.back();
        window.alert('<?php $msg = 1; echo $msg;?>')
</script>

You can use History API to change the url:

history.back();
history.replaceState({}, "Registration", "register.php?msg=1");
location.reload();

You can use:

window.location = "baseurl/route?get=var"

instead of

window.history.back()


You need to be able to have baseurl as your global variable. Have it sent from your server and set in javascript as a global variable.

using browser back is fragile since you cannot predict (or check) where would that lead the user. instead I would recommend that upon the relevant user action or business logic the app would explicitly navigate to register.php page with your desired parameter

// some code handling 
var dataAlreadyPresent = checkIt();
if (dataAlreadyPresent) {
   window.location = "/register.php?msg=yourMsg";
}

BTW, keep in mind that this URL is saved and users might bookmark it or forward it along, so may I suggest that the register.php server logic better not act automatically based on that input. you might want to clean the history state using replaceState (as per jcubic answer) once the register.php has loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM