Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Using an anchor in onClick alert

    To order a customized product from my site, the user has to upload an image and then submit text (separate steps but part of the same php form) before adding to their cart. Currently, the user must scroll back down from the top of the page to the next part of the form after each step which is more confusing than I would like.

    The first step of the form uses a straight forward <form> and I was able to add an action="#anchor" to jump down to where I set the first anchor.

    The second step is regrettably not so straight forward and I'm having trouble adding an anchor. After entering text, the user clicks a submit button which triggers an alert letting them know the text was submitted successfully. When the user clicks the okay button on the popup alert, I would like the page to jump down to the 'add to cart' portion.

    This is the button:
    Code:
    echo '<tr>
        <td valign="bottom" colspan="2" align="center">
            <input id="delete_button" class="button" type="submit" value="Attach Text" onclick="return( confirm( this.form ) );" />
    	</td>
    </tr>';
    And this is the alert:
    PHP Code:
    echo vmCommonHTML::scriptTag(''"function confirm( form ) {
            form.task.value='confirm_text'; 
            alert('Your text was successfully attached.');
            return true;
            
    }" 
    ); 
    How can I prevent the page from returning to the top after the user submits text?

    Thank you,
    Charlotte

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,247
    Thanks
    203
    Thanked 2,556 Times in 2,534 Posts
    Quote Originally Posted by CharlottesWW View Post
    How can I prevent the page from returning to the top after the user submits text?
    You need to use a cookie with a short expiry period. If the cookie is set, scroll page to the anchor.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,189
    Thanks
    80
    Thanked 4,560 Times in 4,524 Posts
    If, by "the user submits text", you mean that the user has caused the <form> info to be sent to PHP, then why not control it all in the PHP, instead?

    At the spot in your page you want the user to be scrolled to, just add a name tag:
    Code:
    <a name="ScrollHere">&nbsp;</a>
    And then have your PHP code do something like
    Code:
    </body><!-- or at least some place near the bottom of the page -->
    <?php
    if ( ...data was posted here ) 
    {
    ?>
    <script type="text/javascript">
        location.href = "#ScrollHere";
    </script>
    <?php 
    }
    ?>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,189
    Thanks
    80
    Thanked 4,560 Times in 4,524 Posts
    Oh, yeah...just read more closely. That's an <input type=submit> so it *will* submit to the PHP code. So I think it makes most sense to do that in PHP, then. No?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,189
    Thanks
    80
    Thanked 4,560 Times in 4,524 Posts
    Hmmm...but for that matter...

    Why not just do this:
    Code:
    echo vmCommonHTML::scriptTag('', "function confirm( form ) {
            form.task.value='confirm_text'; 
            alert('Your text was successfully attached.');
            form.action = "#ScrollHere";
            return true;
            
    }" );
    Your alert there is kind of a lie. The text isn't really successfully attached until the <form> *does* submit and the PHP processes it, so it might be better to have the PHP generate that alert when it returns the submitted-to page. But that's maybe being too pedantic even for me.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    CharlottesWW (09-26-2011)

  • #6
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you, Old Pendant, this is exactly what I was hoping to learn.

    The fact that the alert is technically untrue bothered me too, but I was afraid my limited coding skills weren't up to the task. Is an alert particularly involved with PHP? Or perhaps you may know of another thread that explains it?

    Edit: adding the form.action to the alert caused the form not to submit properly. I guess your first thought was correct, I'll have to use PHP to control both.
    Last edited by CharlottesWW; 09-26-2011 at 03:30 PM. Reason: Unexpected result


  •  

    Tags for this Thread

    Posting Permissions

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