开发者

PHP post doesn't work with a form name

I have a form with the name="search" and I was hoping this script would work but it doesn't seem to be working.

if (isset($_POST['search']))
    include_once('layouts/layout_2.php');

Here is my markup

<form name="sear开发者_C百科ch" action="" method="post">
<p>I am looking for</p>
<input type="text" value="Any keyword" name="searchlist">
<input type="submit" value="Find Job">


The name of the form is not submitted in the $_POST variable.You could check if the name of the submit button was post'ed instead.

<form>
    ...
    <input type="submit" name="submit" value="Submit" />
</form>

<?php
if($_POST['submit']) {
    //Code here
}
?>

This method doesn't require you to put an extra, hidden variable just to check if the form was submitted. Also, you don't have to check the request method and if all your other variables were post'ed as well. If the submit button was clicked, the form was posted.


<form name="search" action="" method="post">
<input type="hidden" name="search" value="1"/>
<p>I am looking for</p>
<input type="text" value="Any keyword" name="searchlist">
<input type="submit" value="Find Job">

PHP

if($_POST['search'] == 1) include_once('layouts/layout_2.php');


The form's name is not sent as part of the form submission. If you want to detect if the form's been submitted, then do:

if ($_SERVER['REQUEST_METHOD'] == 'POST') && (isset($_POST['searchlist']) && (!empty($_POST['searchlist'])) {
   ...
}


Can you not just look for:

if(isset($_POST['searchlist'])) {
  include_once('layouts/layout_2.php');
}


The only data that will appear in a form submission is the values of the successful form controls (and possibly data encoded in the action attribute, but don't do that).

If you want to include arbitrary extra data, then use a hidden input.

<input type="hidden" name="foo" value="bar">

Never use a name attribute for a form. It is a way to identifying the form for client-side scripting that was superseded over a decade ago with the id attribute.


you can try

print_r($_POST)

to see what the value


php doesn't store the name of the form. only form element values are found in $_POST. If you want to figure out which form it was that was submitted, pass a hidden field value or append a var to the action=".." url (and look in $_GET) or similar.


Nope that won't work but you could use a hidden input:

<form  action="" method="post">
<input type="hidden" name="search" value="1" />
<p>I am looking for</p>
<input type="text" value="Any keyword" name="searchlist">
<input type="submit" value="Find Job">

Then your PHP code would work as expected.


Try this:

$keyword = $_POST["searchlist"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜