TKJ365 Posted August 20, 2019 Share Posted August 20, 2019 I am trying to do age validation for a beverage site, and losing my mind. First time trying local storage (to persist across browser sessions) and I assume I have something screwed up. Anyone able to help me figure out how to make this work? <?php if( isset( $_POST['yes'] ) ) { localStorage.setItem('age_verification', 'true'); if( isset( $_GET['url'] ) ) { die( header('Location: ' . $_GET['url'] ) ); } else { die( header('Location: index.php') ); } } elseif( isset( $_POST['no'] ) ) { localStorage.setItem('age_verification', 'false'); } var age_verification = localStorage.getItem('age_verification'); //new if (age_verification = "false" || age_verification = "null") die( header('Location: http://www.bing.com') ); ?> Above is the code for the file checkagenew.php which I call from other pages using the below: <?php if (localStorage.age_verification) { localStorage.setItem('age_verification', 'null'); } var age_verification = localStorage.getItem('age_verification'); if (age_verification = "false" || age_verification = "null") die( header("Location: checkagenew.php?url=http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]") ); ?> Obviously there is a form included in the top part (not shown) that has a submit for yes/no which triggers the $_POST validation... I did not include that for brevity. Quote Link to comment https://forums.phpfreaks.com/topic/309112-help-wage-validation-local-storage/ Share on other sites More sharing options...
Barand Posted August 20, 2019 Share Posted August 20, 2019 You are trying to mix javascript code and PHP. You can't (they run in different places at different times). Why does your form have an input for "yes" and a separate one for "no"? Quote Link to comment https://forums.phpfreaks.com/topic/309112-help-wage-validation-local-storage/#findComment-1569093 Share on other sites More sharing options...
Barand Posted August 20, 2019 Share Posted August 20, 2019 P.S. I would recommend a checkbox which the user has to click (check) to verify age. Local storage would have to be set in the javascript. <!DOCTYPE html> <html> <head> <meta http-equiv="content-language" content="en"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> function ageVerified(btn) { if ($(btn).is(":checked") && $(btn).val() == 'Yes' ) { localStorage.setItem('age_verification', 'Yes') } else { localStorage.setItem('age_verification', 'No') } } </script> </head> <body> <form method="post"> I am over 18   <input type="checkbox" name="ageverify" onclick="ageVerified(this)" value="Yes"> <br> <input type="submit" name="btnSub" value="Submit"> </form> </body> </html> Alternatively, use a pair of radio buttons. Either way it's a single input... <form method="post"> I am over 18   <input type="radio" name="ageverify" onclick="ageVerified(this)" value="No" checked> No   <input type="radio" name="ageverify" onclick="ageVerified(this)" value="Yes"> Yes <br> <input type="submit" name="btnSub" value="Submit"> </form> Whichever above method you use, the PHP processing would be the same ... <?php if ( ($_POST['ageverify'] ?? 'No') == 'Yes') { echo "User is over 18<hr>"; } else { echo "Under age user<hr>" ; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/309112-help-wage-validation-local-storage/#findComment-1569094 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.