I have this:
<form action="profiles.php" method="POST" name="SearchSimple" id="SearchSimple" >
&开发者_如何学JAVAlt;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
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.
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).
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.
精彩评论