Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'POST' variables not recognized if user hits "ENTER"

Status
Not open for further replies.

TheDust

Programmer
Aug 12, 2002
217
US
I have a form that is submitting to another PHP page using the POST method. It's as follows:

Code:
<form runat=&quot;server&quot; style=&quot;margin:0&quot; action=&quot;search.php&quot; method=&quot;post&quot;>
<input class=&quot;inputfield&quot; type=&quot;text&quot; name=&quot;formTypedSearch&quot; />
<select class=&quot;inputfield&quot; name=&quot;formSearchCategory&quot;>
  <option value=&quot;all&quot;>all</option>
  <option value=&quot;artists&quot;>artists</option>
  <option value=&quot;albums&quot;>albums</option>
</select>
<input class=&quot;submitfield&quot; type=&quot;submit&quot; name=&quot;formSearchSubmit&quot; value=&quot;GO!&quot; />
</form>

Then on the page this is being submitted to (search.php), I get the POST variable naturally with:

Code:
$srch = $_POST[&quot;formTypedSearch&quot;];

My problem is that when the user types in a search and hits &quot;ENTER&quot; (which most people are accustomed to doing), no search parameter is being submitted with the form. Everything works perfect if the user clicks the SUBMIT button, but not by hitting the ENTER key. Does anyone know what I can do fix this?? Thanks in advance for any help...
 
TheDust,

I'd like to help, but there is not enough info to give an answer or a guess.
What browser,version, OS are you experiencing this in?
The <form> tag name attribute is also missing.
 
The name attribute is unrelated to submission of the form. You need it to make referring to the form easier in JavaScript, but it's not required for submitting to a script.

TheDust:
I can't duplicate your error in IE or Opera.

Want the best answers? Ask the best questions: TANSTAAFL!
 
It is true that from the scripts prospective the name attribute is not necessary, however, it's required for the HTML to validate correctly. Adhering to W3C standards avoids quirks and abnomalities with browsers that depend on valid HTML or XHTML. There's a good use for standard compliance.

 
I can't duplicate such an error either... the only thing I can imagine that's happening is that you have another button or link that's actually being &quot;clicked&quot; when they hit enter.

-Rob
 
sleipnir214
I was pretty sure that the 'name' or 'id' attribute is required for <form> tags [according to W3C]. I looked it up, and, oops, it's not.
So, it will validate fine without. I'll continue naming my forms however, as there are many 'nice' things one can do with JavaScript to named elements.
 
Well upon examining things further, all that hitting enter in this case is doing is redirecting the browser to search.php, but no variables are posted whatsoever. Is Javascript my only way out here? To see what I'm talking about, check out the search bar on top at this URL:


Any other help is really appreciated...
 
Oh yeah, and let me know what other types of information is needed to figure this out, if any. Thanks...
 
Oh yes, and the browser this is happening to is IE6
 
I've had a similar problem.
Hitting Enter instead of submit button didn't send variables to php.
I'm also using IE6.
I found out that all variables are being sent except for submit button variable (which I was checking in php see if form has been sent).
I your case $formSearchSubmit won't be set in php. But both $formSearchCategory and $formTypedSearch should be ok.
As a solution to this in my script I used a hidden object for form to verify later in php if form has been sent.

I hope this will help!
 
OK, this is very strange... I took your advice and added a hidden field to verify that the form had been submitted instead of checking for the submit button variable. What I found is it is finding the hidden variable just fine, but still none of the form elements besides that seem to be carrying over- not the typed seach, the search category, or the submit button.

This is very odd I suppose...
 
Code:
$srch = $_POST[&quot;formTypedSearch&quot;];

This is only one of the POST variables, not the whole POST array. Remember, if no one hits the submit button, that only means the value of that form element (the submit button itself) is not being posted. But, the other form elements should still be posted, if you look for them.

Try putting this line in your form handler page (the one that receives the posted form):

Code:
var_dump($_POST);

This will give you a quick visual dump of the whole array of posted variables. See if that shows you the other form elements when you hit ENTER instead of click submit. See the difference when you click submit.

-------------------------------------------

My PostgreSQL FAQ --
 
TheDust:
I can verify rycamor's advice.

I used your HTML form on my site, submitting to my own script. That script consists of:

Code:
<?php
print '<pre>';
print_r ($_POST);
?>

In IE, if I enter data in the form and click submit, then $_POST has this data in it:
Code:
Array
(
    [formTypedSearch] => a
    [formSearchCategory] => all
    [formSearchSubmit] => GO!
)

If, however, I hit enter at an appropriate place, $_POST has this in it:
Code:
Array
(
    [formTypedSearch] => a
    [formSearchCategory] => all
)


Interestingly, of Opera 7.10, Mozilla 1.2.1, Netscape 4.79, Netscape 6.2, Netscape 7.0 and IE 6.0.2600.0000, only IE and Netscape 4.79 exhibit this behavior. All the rest send the submit element, too, regardless of how the form is submitted.




Want the best answers? Ask the best questions: TANSTAAFL!
 
This was all great information, guys. Thanks a lot. I figured it out. Couldn't have done it without you guys, thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top