I have a page called myform.php with a large form, which may have several "submit" buttons, for example submit1, submit2, submit3. click these buttons, the form will be submit to the same page and old data will fill in the fields plus new data may be grabbed from database and also refresh certain fields. Such as country (usa or canda), states or province list will re-created depends on the submit results of this form.

Also due to this form is pretty long, i want to redirect user to the position of the button he clicked. to do so, i put named anchor nearby each submit button. For example, named anchor submit3_position is added next to submit3 button. if submit3 is clicked, in php code, i can detect that submit3 is clicked, and then redirect the user to named anchor next to submit3.

To do that I have the codes like that

<?php
//detect which button is clicked
//and redirect user to the named anchor by that button

if (isset(_POST['submit1']))
{
?>
<script language="javascript">
document.location.href="#submit1_position"
</script>

<?php
}
?>
<?php
if (isset(_POST['submit2']))
{
?>
<script language="javascript">
document.location.href="#submit2_position"
</script>

<?php
}
?>
<?php
if (isset(_POST['submit3']))
{
?>
<script language="javascript">
document.location.href="#submit3_position"
</script>

<?php
}
?>

Note:

I have to use, document.location.href="#submit3_position";
and I cannot use, header("Location: myform.php#submit3_position") because the page is created by a large form's post values sent to this page itself, i cannot attach all the large post values as a get parameters here.

It seems no problem.

But this approach only works for the set up that browser only get the whole page from the server when full html page generated by php done.

The browser get the full page, and then when brower sees

document.location.href="#submit3_position";

At the client side, the browser will refresh the page to that position.

But for the set up that if browser will get part of the page generated by php and then get other parts of the page generated by php later on. This approach is not going to work.

because anything below the line

document.location.href="#submit3_position";

may not be able to sent to the browser.

Why? because, when the browser saw this line,

document.location.href="#submit3_position";

it begin to refresh before the rest part of the page sent to the brower. the rest part of the page will not get chance to be sent to that browser.

So what is the best solution for this problem?

Thanks!

    Use a string and print it out after processing.

    <?php
    //detect which button is clicked
    //and redirect user to the named anchor by that button
    
    if (isset(_POST['submit1']))
    {
    $redirect = '<script language="javascript">
    document.location.href="#submit1_position"
    </script>';
    
    }
    if (isset(_POST['submit2']))
    {
    
    $redirect = '<script language="javascript">
    document.location.href="#submit2_position"
    </script>';
    }
    if (isset(_POST['submit3']))
    {
    $redirect = '<script language="javascript">
    document.location.href="#submit3_position"
    </script>';
    
    }
    
    //Execute code here
    
    // Then do the redirect code.
    print $redirect;
    ?>
    

    I hope this is what you were looking for.

    Kinda hard to understand what you were saying.

    --FrosT

      Yes. It is exactly what I was asking for.

      I was thinking about a solution could be put all these following codes at the bottom of the page.

      It is similiar as what you suggest. But your suggestion will make my codes looks clean.

      Thanks!

      if (isset(_POST['submit1']))
      {
      ?>
      <script language="javascript">
      document.location.href="#submit1_position"
      </script>

      <?php
      }
      ?>
      <?php
      if (isset(_POST['submit2']))
      {
      ?>
      <script language="javascript">
      document.location.href="#submit2_position"
      </script>

      <?php
      }
      ?>
      <?php
      if (isset(_POST['submit3']))
      {
      ?>
      <script language="javascript">
      document.location.href="#submit3_position"
      </script>

      <?php
      }
      ?>

        Write a Reply...