SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    redirect to same spot on the page

    I'm guessing, but how you redirect to same spot on the page sounds like a js question. If so, please provide some tips to get started.

    I've created a long page that allows me to save form inputs as I go along, but it sends me back to the top of the page with each redirect (header('Location: http://localhost/acrobat7c.php')wink.gif. I'd like the redirect to send me back to where I was when I submitted inputs.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    How does your code work when it adds a new form to the DOM as i have a thought in mind of how to get this working for you.

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It's pretty simple. The form sends the input to this script:
    PHP Code:
    $key "'" key($_POST) . "'";
    $val "'" $_POST[$key] . "'";
    include_once 
    "connect_to_mysql.php";
    mysql_query("UPDATE stk SET width = " $val " WHERE id = " $key "") or die(mysql_error());
    header('Location: http://localhost/acrobat7c.php'); 
    Last edited by nichemtktg; Jun 14, 2011 at 19:46. Reason: left out a line

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Sorry for the delay, try the following idea.

    1. When you create your form add an anchor element like the example below before it
      HTML Code:
      <a name="form_' . $form_id . '"></a>
    2. Then in your PHP script change your header redirect to
      PHP Code:
      header('Location: http://localhost/acrobat7c.php#form_' $key); 

    Basically my idea is to use anchor tags and a URL anchor so the browser does all the hard calculations to get the page position where it needs to be. Let me know how you go as im hoping this idea works for you.

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    SgtLegend, worked like a dream. <a> is a tag that was waiting for me to use for the first time.

    Thanks SgtLegend,

    Niche

  6. #6
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    No problem, glad it worked for you

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by SgtLegend View Post
    HTML Code:
    <a name="form_' . $form_id . '"></a>
    If it helps, the id attribute is one that has been around since HTML 4, so that instead of using a named anchor:

    Code php:
    <a name="form_' . $form_id . '"></a>
    <form>

    You only now need to use the identifier by itself.

    Code php:
    <form id=" . $form_id . '">

    So that the very same identifier is used to reference that part of the page:

    Code php:
    header('Location: http://localhost/acrobat7c.php#' . $key);

    That helps to reduce confusion by relegating the name attribute to only inside of forms, for the purpose of naming form fields, whereas the id attribute is used to perform its proper job of uniquely identifying different parts of the page.
    Last edited by paul_wilkins; Jun 16, 2011 at 21:53.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Thanks for the info Paul, i actually didn't know that "id" could be used as an anchor but now i do

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by SgtLegend View Post
    Thanks for the info Paul, i actually didn't know that "id" could be used as an anchor but now i do
    Just to finish this off, here's how it's all connected.

    The HTML 4.01 spec has several sections that bring this all together.

    Target Anchor? Anchor Element?

    The fragment identifer section gives a brief example of how the URI is used to refer to a certain location. Even though it says that it's pointing to an anchor, that doesn't mean to say that it's pointing to an anchor element. Keep in mind that by "anchor element" they don't mean the <a> element. Instead, a much broader meaning is meant.

    The section on the id attribute helps to clarify this, as the spec says that id is used for several tasks, including: "As a target anchor for hypertext links" which links to the introduction page.

    It's called a "target anchor" not because the id attribute is on hypertext links, it can be on virtually any HTML element at all. The target anchor allows some other page to use a hypertext link to link directly to an element within the target page. The id attribute serves as a target anchor.

    The introduction to links and anchors section provides much fuller details on how fragment identifiers work. It also links through to the name section which includes a link to the Anchors with the id attribute section.

    How is the Name attribute affected?

    At the time of the HTML 4.01 spec in 1999, they were transitioning over from using the name attribute on a limited number of HTML elements, to using the id attribute on virtually everything. 12 years ago they still had to provide consideration for web browsers that didn't support HTML4. Unthinkable now, but that was back in the day.

    As time moved on and they developed the spec for XHTML (there's only one year between HTML 4.01 and XHTML 1.0), they had formally deprecated the use of name attribute on all elements except for those used to provide form data. In the XHTML fragment identifiers section they talk about completely removing the name attribute from all other elements.

    Why would they do that? Because if we look back at the HTML 4.01 specs for forms, we find that in the form controls section that the name attribute has a special task to perform within forms themself, that has nothing to do with fragment identifiers.

    In effect, the purpose of the name attribute is now solely meant for data fields within forms. And in fact, with the HTML5 spec we now find that the name is exclusively used only for forms now.

    Summary

    Now more than 10 years on from HTML 4.01 and XHTML 1.0, the id attribute remains as the favored method by which to uniquely identify different parts of a web page.
    Last edited by paul_wilkins; Jun 27, 2011 at 20:38.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •