...

View Full Version : Using an anchor in onClick alert



CharlottesWW
09-25-2011, 06:19 PM
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:

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

Philip M
09-25-2011, 06:58 PM
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.

Old Pedant
09-26-2011, 01:58 AM
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:


<a name="ScrollHere">&nbsp;</a>

And then have your PHP code do something like

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

Old Pedant
09-26-2011, 02:01 AM
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?

Old Pedant
09-26-2011, 02:04 AM
Hmmm...but for that matter...

Why not just do this:


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.

CharlottesWW
09-26-2011, 02:17 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum