HTML submit with only one text input

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paladaxar
Forum Commoner
Posts: 85
Joined: Fri Jun 18, 2004 11:50 pm

HTML submit with only one text input

Post by paladaxar »

I have made a page that has a form and a single input (type='text') on it. The form's action is the same page (that the form is on). When the page re-loads, a PHP script runs that checks to see if the form has been submitted (if($submitForm)). If the form has been submitted, this line is supposed to run: header("Location: http://www.myurl.com...").

Now, the problem is that when I enter something into the text field and press enter, it just reloads the same page with an empty text box. It never goes to the new page that I have specified. The strange thing about it is that if I add another text box (we'll call it txt2) then I enter something into txt1 and press enter, everything works fine (even if txt2 is empty). The "if" script runs and the next page is loaded.

Even stranger yet, the script written the way that I want it to be written (with just one text field) runs fine in Firefox (a Mozilla browser) but it does not run correctly in IE (unless I add the second text field). I have already tried adding a "hidden" field, but that didn't fix the problem. Is there any way that i can have just one text field, enter text, press enter and get to the next page? What is going on here?
User avatar
SystemWisdom
Forum Commoner
Posts: 69
Joined: Sat Mar 26, 2005 5:54 pm
Location: A Canadian South of the 49th Parallel

Post by SystemWisdom »

Can you show us an example of your code that is doing this??

Hard to provide coding help without code...
paladaxar
Forum Commoner
Posts: 85
Joined: Fri Jun 18, 2004 11:50 pm

Post by paladaxar »

Code: Select all

<?php
	session_start();
	if($submitForm)
	{
		header("Location: http://www.myurl.com/page2.php");
	} 
	$page_type='home'; 
	include("header.php");
?>
<table>
	<tr>
		<td>
		<form action='http://www.myurl.com/page1.php' method='post' name='searchForm'>
				<input type='text' name='searchField' value='' size=16>
				<input type='submit' name='submitForm' value='Search'>
		</form> 
		</td>
	</tr>
</table>
				
<?php
	include("footer.php"); 
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

IE does not send the submit button if you press enter in a field that isn't that button. Firefox does.

You should look for the field, not the button.
User avatar
SystemWisdom
Forum Commoner
Posts: 69
Joined: Sat Mar 26, 2005 5:54 pm
Location: A Canadian South of the 49th Parallel

Post by SystemWisdom »

From the code you have there, how is page2.php ever going to know what the value entered in the textbox was? You're not passing the text to page2.php in any way as is..

Oh, and try not to rely on AutoGlobals, instead use:

Code: Select all

if(isset($_POST['submitForm']))
or as feyd suggested:

Code: Select all

if(isset($_POST['searchField']))

Maybe that helps?
paladaxar
Forum Commoner
Posts: 85
Joined: Fri Jun 18, 2004 11:50 pm

Post by paladaxar »

In response to Feyd: IE DOES send the submit even if you hit enter from a text field. I have many pages with more than one text field and i can enter text into any number of them, and press enter at any time, and the form will be submitted. I just dont know why it wont work with only one text field. (P.S. I have also looked for the field instead of the button and it still does not work)

In response to SystemWisdom: I left part of my code out to keep it simple when I posted it in this forum. There is a place (inside the 'if' statement) that makes the value of the text field into a session variable. Sorry if that was confusing, but that's not the part that I am having problems with.

Still looking for answers....
User avatar
SystemWisdom
Forum Commoner
Posts: 69
Joined: Sat Mar 26, 2005 5:54 pm
Location: A Canadian South of the 49th Parallel

Post by SystemWisdom »

Hmm.. okay, that works... But I still don't see why it wouldn't send the form..

The only thing I can think of now, is maybe your page headers are already sent, and the call to header() is failing??
It is really just a shot in the dark though, maybe try something like this:

Code: Select all

if($submitForm)
{
    if( !headers_sent() )
        header("Location: http://www.myurl.com/page2.php");
    else
        echo "Failed to Redirect page! Page Headers Already Sent!";
}
Like I said, a shot in the dark, but I really don't see anything wrong with the code you posted.. sounds weird!

paladaxar
Forum Commoner
Posts: 85
Joined: Fri Jun 18, 2004 11:50 pm

Post by paladaxar »

everything in the code is fine because if I add a second text field, the form submits just the way it should and goes to the next page...but why wont it do it with just one text field? And if I DO need another field, is there any way that I can make it look like (to the client) that there is only one text field? I know that's not the best fix, but if it works, I will use it.
User avatar
oceglok
Forum Newbie
Posts: 5
Joined: Sun Mar 27, 2005 2:03 pm
Location: Nagoya-City, Japan

Post by oceglok »

You can stick a hidden field into your form and check on its value after the submit. This should work:

Code: Select all

&lt;input type=&quote;hidden&quote; name=&quote;hidden&quote; value=&quote;1&quote;&gt;
then:

Code: Select all

if (isset($_POST['hidden']) && $_POST['hidden'] == "1") {
   // your redirection code here
}
Hope, this helps
oceglok
Post Reply