Reputation: 307
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="search" action="" method="post">
<p>I am looking for</p>
<input type="text" value="Any keyword" name="searchlist">
<input type="submit" value="Find Job">
Upvotes: 3
Views: 9343
Reputation: 19380
<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');
Upvotes: 3
Reputation: 22947
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.
Upvotes: 5
Reputation: 19309
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.
Upvotes: 0
Reputation: 4048
Can you not just look for:
if(isset($_POST['searchlist'])) {
include_once('layouts/layout_2.php');
}
Upvotes: 3
Reputation: 943591
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.
Upvotes: 1
Reputation: 360702
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'])) {
...
}
Upvotes: 3
Reputation: 32532
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.
Upvotes: 0