Forum Moderators: coopster

Message Too Old, No Replies

how to go to specific place on page after processing a form's data

feedback form is at bottom of page. How to return to form after validation?

         

zollerwagner

8:50 am on Feb 2, 2004 (gmt 0)

10+ Year Member



Hi,

I have a long page with a feedback form at the bottom. After I process the form's data using PHP, I want to show the form again--or actually the <h3>title</h3> above the form, becuase I display error messages right below that heading.

I've tried using a conditional <body> tag, as shown below, but it doesn't start the display at the <h3 name='feedback'> tag, so I have to scroll down to see the error messages and data in the form.

I also tried using header ("location: #feedback");
That does display the form, but it also clears the form data, so that didn't work.

Any other ideas on how to do this better? Is it possible?

<?php
if ((bool)$_POST) // if the form was submitted, show the part of page with form
{
echo '<body onload="document.feedback.focus()">';
}
else // if the form was not submitted
{
echo '<body>';
}
?>

....and....

<h3 name="feedback">Feedback Form</h3>

mykel79

9:43 am on Feb 2, 2004 (gmt 0)

10+ Year Member



Have you tried:
<body onLoad="window.document.location='#feedback'">

?
Seems to work.

zollerwagner

10:55 am on Feb 2, 2004 (gmt 0)

10+ Year Member



Thanks for the idea. I wish I could get it to work! Maybe I'm doing something wrong, but in IE6, the page still displays the top of the page.

<?php 
if ((bool)$_POST) // if the form was submitted, show the part of page with form
{
echo '<body onload="window.document.location=#feedback">';
}
else // if the form was not submitted
{
echo '<body>';
}
?>

Interestingly, in Opera 7.23, the page loads and then goes to:
h**p://myurl/[object%20HTMLHeadingElement]
and that immediately calls up my error page.

Well, in truth, I've not been able to get the quotes around "#feedback" to work because Symantec rewrites my body tag to insert its own onload and the second quote gets dropped. I was backslashing/escaping those quotes.

without quotes on #feedback:

<body onload="var SymTmpWinOpen = window.open; window.open = SymWinOpen; window.document.location=#feedback; window.open = SymTmpWinOpen;">

with quotes on #feedback (notice that the last " is missing):
<body onload="var SymTmpWinOpen = window.open; window.open = SymWinOpen; window.document.location="#feedback; window.open = SymTmpWinOpen;">

So I tried this, too, to see if I could get the quotes in by feeding a variable into the body tag:


<?php
if ((bool)$_POST) // if the form was submitted, show the part of page with form
{
$loc = ' onload=\'window.document.location="#feedback"\'';
}
else // if the form was not submitted
{
$loc = "";
}
?>
<body<?=$loc?>>

I get similar results, no matter whether I use double or single quotes around #feedback (except that the first quote does match the kind I use).

This version does work in Opera, for what that's worth.

I also tried this, with exactly the same results:


$locId = "'#feedback'";
$loc = ' onload="window.document.location=' . $locId . '"';

Do you see anything buggy about what I'm doing? Or does anyone have any other ideas?

zollerwagner

11:30 am on Feb 2, 2004 (gmt 0)

10+ Year Member



I tried another, much messier method, which I thought might be simpler, but got the same results.


<?php
$beginHtmlComment = "<!--";
$endHtmlComment = "//-->";

if(!$_POST) // if the form was NOT submitted, don't use body tag that goes to the part of page with form
{
echo $beginHtmlComment;
}
?>

<body id="six" onLoad="window.document.location='#feedback'">

<?php
if (!$_POST) // same as above
{
echo $endHtmlComment;
}
?>

<?php
if ($_POST) // if the form was submitted, hide plain body tag
{
echo $beginHtmlComment;
}
?>

<body id="six">

<?php
if ($_POST) // same as above
{
echo $endHtmlComment;
}
?>

zollerwagner

12:50 am on Feb 3, 2004 (gmt 0)

10+ Year Member



I'm considering using CSS to display or not display the material above the form. Has anyone used this method? Are there any usability issues to be aware of?

zollerwagner

9:14 am on Feb 4, 2004 (gmt 0)

10+ Year Member



I've realized that I needed an anchor tag as a home for name="whatever", but I'm still not getting the browser to display the right place.

Any ideas?

mykel79

11:56 am on Feb 4, 2004 (gmt 0)

10+ Year Member



Are you sure you tried with the quotes right?
It should be:
<body onload="window.document.location='#feedback'>"

Or

<body onload='window.document.location="#feedback">'

That is the inside quotes have to be different than the outside ones.

When you print this out, using
echo 'javascript code here'
might be tricky, so it might be easier to jump out of php
<?
normal php code
?>
html or javascript
<?
some more php code
?>

zollerwagner

8:19 pm on Feb 4, 2004 (gmt 0)

10+ Year Member



Yeah, that's an interesting idea, jumping outside of PHP. Thanks for the idea!

I wonder if you can you have an if/else that goes outside of PHP like this:

<?php 
if ((bool)$_POST) // if the form was submitted, show the part of page with form
{
?>

<body onload="window.document.location='#signuptop'" id="top">

<?php
}
else // if the form was not submitted
{
?>

<body id="top">

<?php
}
?>

I've tried it, and it looks like it works in the sense of drawing the html correctly. It just doesn't go to the right place on the page.

When I try that, I get:

<body onload="var SymTmpWinOpen = window.open; window.open = SymWinOpen; window.document.location='#signuptop; window.open = SymTmpWinOpen;" id="top">

The last single quote around #signuptop is missing (other times it is moved to after the SymTmpWinOpen).

Hm... I guess what this tells me is that Symantec just isn't going to let me do the location thing.

Maybe there's a simpler way to do this with CSS hiding display of the top of the page or something like that?

mykel79

9:43 pm on Feb 4, 2004 (gmt 0)

10+ Year Member



Yes, you can use IF statements and in the middle jump out of php, very useful. Also, if you have lots of text to output, use heredocs.
echo <<<END
any text you want
any symbols ' "
also $variables
END;

Instead of END you can use any word you like,just the same at the beginning and end. And make sure END; starts from the first character in a new line (lost lots of time figuring that one out).

As for the location, Symantec will mess things up when it sees a "onLoad" command. But you could try just putting the location command in a normal script tag in the body:
<script language="JavaScript">
<!--
document.location="#form1";
//-->
</script>

I tried it on a simple html and it works fine, the document opens at the location #form1.

zollerwagner

10:33 pm on Feb 4, 2004 (gmt 0)

10+ Year Member



YES! That does it! I'm so pleased.

I ended up with this:


<body id="top">

<?php
if (isset($_POST['submit'])) // if form was submitted
{
?>

<script language="JavaScript">
<!--
document.location="#signup";
//-->
</script>

<?php
}
?>

That works well. Thanks so much! Whew...what a struggle!

zollerwagner

8:06 am on Feb 14, 2004 (gmt 0)

10+ Year Member



Well, that worked well in Internet Explorer. Some versions of Opera and Netscape couldn't deal with it.

So I've come up with a varation on it, talked about here:

[webmasterworld.com...]

mep00

3:04 am on Feb 15, 2004 (gmt 0)

10+ Year Member



Forget the javascript and the HTML comments, there is a much simpler answer (though the comment idea was close).

<body>
<?php if ($bad_input == 0) echo '<a name="here"></a>';?>
<form action="<?php echo $PHP_SELF;?>#here" ....>
<?php if ($bad_input == 1) echo '<a name="here"></a>';?>
<input... />
<?php if ($bad_input == 2) echo '<a name="here"></a>';?>
<input... />
</form>
</body>

There are other ways to do it (e.g., a
switch
stament), but the general idea should be the same. The key is have the form point to the anchor, and use php to put the anchor in the right place. (FWIW, I'd rather stay in php and either echo my HTML or use a template; I hate escaping from my nice, safe, warm, and cosy php. :))

A different approach all together might be to list all the errors in the form in one place and put each in a <label>. Then you can list multiple errors and all the user needs to do is click on the error message. With CSS you could even make them behave sort of like links by underlining them and changing the pointer with a :hover. Since IE won't accept the :hover on anything but an anchor, here you might want to browser sniff and add the mouseover js for IE, or better yet, use VBScript so non-IE will ignore it, or just leave the pointer as-is for those who don't understand (it's only cosmedtic, after all).

Often, the simpler the solution, the better. When ever you rely on client side scripting, you're asking from trouble.

zollerwagner

4:24 am on Feb 15, 2004 (gmt 0)

10+ Year Member



That's an interesting approach. I see two different parts to your idea:

1--move the anchor instead of changing the body tag, and
2--add the hash after the action in the form tag.

If the second one works, I wouldn't even need the first one.

I'll have to play with those. Hey, where were you last week when I was suffering?!

Thanks for the novel solutions.

mep00

5:59 am on Feb 15, 2004 (gmt 0)

10+ Year Member



That's an interesting approach.
Thank you. If it helps, I aproach Web development from a programer point of view.

I see two different parts to your idea
I'm assuming you mean my first idea.

If the second one works, I wouldn't even need the first one.
If you mean the second idea (which is what I think you mean here), NS4.x and IE3 don't support <label>s (if you care; I don't). The second idea isn't my own; I read somewhere about using mutiple
for="[i]input-id[/i]"
, and somewhere else about using a OO-form validater which generates multiple error messages.

The first idea was just if you want to jump to a point on the page, use an anchor. Don't know where in advance? When you dynamicly generate the page, put the target in the proper place. Like I said, "from a programers point of view."

Hey, where were you last week when I was suffering?!
Sorry, I guess I missed the question. If you like I can give you an answer base on the Bible (Book of Judges), but I'm not sure if it would make you feel any better (but look on the bright side, at least you didn't sacrifice your daughter when you didn't have to).

Thanks for the novel solutions.
We aim to please. :)

zollerwagner

9:00 am on Feb 15, 2004 (gmt 0)

10+ Year Member



Well, I've tried both methods and they both worked.

Well, with Netscape after a refresh, even if nothing had been touched in the form, the page scrolled down to the form. When I made the id and name attributes different, that was fixed. Netscape seemed to use id="contact" to satisfy the "location.href="#contact". (In the second example below, I've included the different attributes.)

Without JavaScript:

<h3 id="contact"><a name="contact">Contact &amp; Feedback Form</a></h3>
<form action="<?=$_SERVER['PHP_SELF']?>#contact" method="post" name="contactForm" id="contactForm" onsubmit="return validate(this);">

With JavaScript:

<body id="sectionfour" onload="location.href='#contactF';">

used with:

<?php 
if (isset($_POST['submit'])) // if form was submitted show anchor
{
echo '<a name="contactF"></a>';
}
?>

<h3 id="contact">Contact &amp; Feedback Form</h3>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="contactForm" id="contactForm" onsubmit="return validate(this);">

I like the fact that the first one avoids javascript. It's ridiculously simple! Too bad it wasn't obvious to me.

Thanks again for the help.

mep00

10:28 am on Feb 15, 2004 (gmt 0)

10+ Year Member



I like the fact that the first one avoids javascript.
They both do.

It's ridiculously simple! Too bad it wasn't obvious to me.
I had the advantage of it being "not my problem," which always makes it easier to solve. One of the many reasons why this site is so valuable.

The horror stories I could tell about "ridiculously simple" problems which took hours to soulve because I was too close to the problems. How many times I couldn't see the problem with:

if ($var="") {...}
.

One of the hardest steps to being a better programer is being able to take a step back and see whats's actually there, and not what you "know" is there.

zollerwagner

7:28 pm on Feb 15, 2004 (gmt 0)

10+ Year Member



Hi, again,

I'm pretty sure that this line contains a JavaScript event:

With JavaScript: 
<body id="sectionfour" onload="location.href='#contactF';">

When I turn off JavaScript in the browser it doesn't work.

Perhaps we were talking about different things.

mep00

5:23 am on Feb 16, 2004 (gmt 0)

10+ Year Member



I'm pretty sure that this line contains a JavaScript event:
onload="location.href='#contactF';"
Looks like js to me.
onload
is an event handler. So when js is on,
location.href
gets set; when it's off, it doesn't.