Can anybody help explain why I can't get PHP to work when using it inline with HTML elements?
The following code is how I'm trying to implement the PHP code into my HTML. It can be found inside the first input
of the form.
<li id="navHeaderItem" class="navHeaderItem headerSearchBar">
<form action="./results.php" method="get" id="headerSearch" class="headerSearch">
<input type="text" name="input" size="30" value="<?php echo htmlspecialchars($_GET ['input']); ?>" placeholder="Search E-Shout!" id="headerSearchBar" class="headerSearchBar" />
<input type="submit" value="Search" id="headerSearchButton" class="headerSearchButton" />
</form>
</li>
Just to clarify my primary objective, I'm trying to create a basic search bar which will allow users to search the content of a site I'm working on. I'm watching a YouTube video (which can be found here) and the guy in the video is doing exactly the same thing with his PHP script.... (If you skip to 8:35 by clicking here then you can see exactly how his code looks with the PHP inline.) But his works.....so I don't understand what's going on.
It doesn't help that I know very little about PHP....
Thanks in advance!
EDIT: Here is what it looks like as of right now....
Use this:
<input type="text" value="<?php echo htmlspecialchars($_GET ['input']); ?>" name="input" size="30" placeholder="Search E-Shout!" id="headerSearchBar" class="headerSearchBar" />
You need htmlspecialchars()
as well because if you're not using it, a double quote in the input may break the html sytax
To resolve the "Undefined index: input..." error, you have to check if $_GET['input'] exists. If exists, print its value. If not, print an empty string:
<input type="text"
name="input"
size="30"
value="<?php echo isset($_GET ['input']) ? htmlspecialchars($_GET ['input']) : ''; ?>"
placeholder="Search E-Shout!"
id="headerSearchBar"
class="headerSearchBar" />