Pain Posted June 7, 2012 Share Posted June 7, 2012 Hello there. I was wondering how do people redirect users to a particular div after a submit button is clicked. Say i want to redirect someone to a div with an id of "line1". So normally user should click on a link of some sort: <a href='#line_1'>Click here</a> How do i do something like this without clicking any links, but rather after submitting information to the server. Can i do it by sending headers? Quote Link to comment https://forums.phpfreaks.com/topic/263800-automatically-moving-to-a-div/ Share on other sites More sharing options...
ejay Posted June 7, 2012 Share Posted June 7, 2012 headers don't have to do anything with it. You will not be using the div's ID but a named anchor just above the target div instead. Say you want to "redirect (as you say)" the user to a specific div element with id=line1, you do something like this //page test.php //... <a name="line1" /> <div id="line1>hello</div> and then from the form submit handling script, redirect user to test.php#line1 instead of just test.php The div's id is not bound to be same as named anchor's name. Quote Link to comment https://forums.phpfreaks.com/topic/263800-automatically-moving-to-a-div/#findComment-1351835 Share on other sites More sharing options...
Pain Posted June 7, 2012 Author Share Posted June 7, 2012 Thank you for your reply. I am using php_self, so the code that processes the form is in the same page. I've tried: <div id="hot_line"><form method="post" action="main.php#line1"> <input type="text" name="line" placeholder="Karstoji linija (-50 tasku)" id="input" /> <input type="submit" name="submit_line" value="Siusti" id="button"/> </form></div> Is this what you meant? Didn't work for me. Quote Link to comment https://forums.phpfreaks.com/topic/263800-automatically-moving-to-a-div/#findComment-1351839 Share on other sites More sharing options...
ejay Posted June 7, 2012 Share Posted June 7, 2012 No that's not what I meant. It seems that you're not applying PRG (Post-Redirect-Get) pattern, if I understand correctly. Please post the form handling code. Quote Link to comment https://forums.phpfreaks.com/topic/263800-automatically-moving-to-a-div/#findComment-1351842 Share on other sites More sharing options...
Pain Posted June 7, 2012 Author Share Posted June 7, 2012 <?php $submit_line = $_POST['submit_line']; if (isset($submit_line)) { if ($points >= 50) { if (!empty($_POST['line']) && strlen($_POST['line']) >= 3) { if (strlen($_POST['line']) <= 50) { mysql_query("INSERT INTO line VALUES ('', '$usr', '$_POST[line]', '$name')"); mysql_query("UPDATE members SET points = points - 50"); } else { ?> <script type="text/javascript"> window.alert("Simboliu skaicius negali virsyti 50"); </script> <?php } } else { ?> <script type="text/javascript"> window.alert("Turite ivesti bent 3 simbolius"); </script> <?php } } else { ?> <script type="text/javascript"> window.alert("Neuztenka tasku"); </script> <?php } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/263800-automatically-moving-to-a-div/#findComment-1351846 Share on other sites More sharing options...
Pain Posted June 7, 2012 Author Share Posted June 7, 2012 is it even possible to do it the way i want? Quote Link to comment https://forums.phpfreaks.com/topic/263800-automatically-moving-to-a-div/#findComment-1351940 Share on other sites More sharing options...
ejay Posted June 8, 2012 Share Posted June 8, 2012 It is. After having a look at your code (which is not complete, so the solution will be a guess), I think you'll need to do following 2 steps //1. add the named anchor above your div <a name="line1"></a> <div id="line1>hello</div> //2. add following code just above the closing body tag ( </body> ) <?php if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($submit_line)) { ?> <script type="text/javascript">window.location.hash = 'line1';</script> <?php } ?> I hope this works. Quote Link to comment https://forums.phpfreaks.com/topic/263800-automatically-moving-to-a-div/#findComment-1352155 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.