2

I have this:

 <form action="profiles.php" method="POST" name="SearchSimple" id="SearchSimple" >
<input name="search" id="s" style="width: 150px;" type="text">
 <a style="display: inline-block; width: 100px; font-weight: bold; cursor: pointer;" id="submitSearchSimple">Search </a>
 <script>
 $('#submitSearchSimple').click(function() {
 javascript:document.SearchSimple.submit();
 });
 </script>
</form>

It submits fine although when i do

if($_POST["submitSearchSimple"] && isset($_POST["submitSearchSimple"])) {
echo $_POST["s"] . " -TEST";
}

It doesnt show.. I get nothing

asked Sep 28, 2010 at 8:04
1
  • if ($_POST["submitSearchSimple"] && isset($_POST["submitSearchSimple"])) needs to be the other way around. You test for isset to figure out if the variable exists in the first place, to avoid trying to access a variable that doesn't exist (which results in a warning). Hence: if (isset($foo) && $foo) to test for "if variable exists and variable is true-ish". Which BTW is the same as if (!empty($foo)). Commented Sep 28, 2010 at 8:11

3 Answers 3

2

In PHP, POST variables work only for INPUT elements, SELECT elements & that too in a FORM, only when the form is submitted. Also you need to specify the "name" attribute of those elements to be catched / used by the POST superglobal array variable.

In your case, you can simply do this:-
if(isset($_POST["search"]) && !empty($_POST["search"])) {
 echo $_POST["search"] . " -TEST";
}

Always remember that there is one major difference in PHP with JavaScript / jQuery. In JavaScript / jQuery, you can use either the "id" attribute or the "name" attribute to validate / manipulate the fields. But in PHP, it is always the "name" attribute of the field that is important, so be careful in doing those.

Hope it helps.

answered Sep 28, 2010 at 8:07
Sign up to request clarification or add additional context in comments.

6 Comments

isset($_POST["search"]) && !empty($_POST["search"]) is halfway redundant. You can skip the isset check.
@deceze - There are specific situations where the "isset" check is necessary instead of just providing the variable in the "if" condition. For example, let's say we have a variable $abc = 0;. Now if we just provide the variable in the condition as if ($abc), it will not work, as if (0) doesn't satisfy the condition. So, in this case, we will need to use if (isset($abc)) to successfully pass the condition.
Huh? 0 won't satisfy !empty though, so it's entirely pointless.
That's why I normally use this pair - isset() && !empty(). :)
And that's completely pointless. :) !empty($foo) is equivalent to isset($foo) && $foo (empty($foo) is equivalent to !isset($foo) || !$foo). So you're really writing isset($foo) && isset($foo) && $foo, which also doesn't work if $foo is 0.
|
2

Your form input's name is "search" not "submitSearchSimple".

The id is not passed to the server and neither is anything that isn't a form control (like the anchor in your example).

answered Sep 28, 2010 at 8:06

Comments

0

A simple way to identify what variable you passed as POST : you could have done a *var_dump($_POST)* in your profiles.php. You would have seen

array(1) { ["search"]=> string(4) "test" }

Therefore, you could have seen that it wasn't $_POST["s"] but $_POST["search"] and concluded that it wasn't the id that gives the name of the index but the name.

I don't see the point of using javascript in this case... (well, i can imagine it's for css styling, but you can easily style a submit button anyway.

answered Sep 28, 2010 at 8:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.