user1
user1

Reputation: 15

php code inside the search box

I am trying to create a search bar for website which searches the database but I find that the code it self is displaying inside the search box

<form method="get" action="chalo_search.php">
            <input type="search" name="k" size = "20" placeholder="Enter college name" value = "<?php echo $_GET['k'];?>" autofocus/>
            <input type= "submit" value="Search"/>
        </form>

is there any error in this

Upvotes: 1

Views: 4537

Answers (3)

bowlerae
bowlerae

Reputation: 944

try

<?php

$k = $_GET['k'];

echo "<form method='get' action='chalo_search.php'>
<input type='search' name='k' size='20' placeholder='Enter college name' value='$k' autofocus/>
<input type='submit' value='Search'/>
</form>";

?>

Upvotes: 0

IsisCode
IsisCode

Reputation: 2490

You're echoing $_GET['k'] into the input box. If $_GET['k'] isn't defined, you will get this error:


Notice: Undefined index: k in ... yourscript.php on line 2

(That's what's appearing in your search box)

Check that $_GET['k'] is defined first:

<?php if(isset($_GET['k'])){echo $_GET['k'];}?>

Upvotes: 4

wwwroth
wwwroth

Reputation: 434

Remove the extra spaces when declaring the "value" attribute inside the input element. See below:

<input type="search" name="k" size = "20" placeholder="Enter college name" value="<?php echo $_GET['k'];?>" autofocus/>

Upvotes: 0

Related Questions