Jump to content

automatically moving to a div


Pain

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/263800-automatically-moving-to-a-div/
Share on other sites

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.

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.

 

<?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
}
}
?>

 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.