Forum Moderators: coopster
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>
<?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?
<?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;
}
?>
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
?>
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?
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.
So I've come up with a varation on it, talked about here:
[webmasterworld.com...]
<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>
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.
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.
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 ideaI'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. :)
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 & 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 & 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.
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.