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
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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.