PHP won't work inline with HTML elements - php

Can anybody help explain why I can't get PHP to work when using it inline with HTML elements?
My file is saved as .php
Yes, PHP is working on my server (I'm using XAMPP)
Yes, PHP works when I use it outside of an HTML element (in a regular PHP block of code) but won't work when using it inline in my HTML
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" />

Related

how to get meta tags with php, but with html form

I got this PHP src:
$tags = get_meta_tags('http://www.autostraddle.com');
echo "CMS is: ";
echo $tags['generator'];
This code is not user friendly; it's not ready for one online tool for my website, because I want create a simple service - CMS detector, and...
$tag = isset($_REQUEST['url']) ? get_meta_tags('url') : '';
<form action="index.php" method="post">
<input type="text" name="url" size="65" value="<?php echo $tag; ?>"/><br />
<input type="submit" value="Go!">
echo $tag['generator'];
I want create one online tool about detecting what CMS is, but I need this PHP script with HTML form because this will be used from users, and not only from me.
They must put any url, and then the script will perform as described to get the meta tag 'generator'; how do I get the meta tag 'generator'? I want only this meta tag.
Well, and how to do this with 'if else'? if there is meta tag generator do this, but if there is not such tag 'generator', then write any 'echo' message, e.g.
CMS is not known.
Well, this is simple PHP script, but I don't know how to create variables and how to get a URL; mayve with cURL?
What you're struggling with is actually getting the user-side input from the POST superglobals. This is something you can try, but there are many ways to implement this. You could also use method="SET" and capture the user parameters from URL of the action="" file.
Notice, in this case, the action="" parameter is empty. That means the PHP script will execute on the same page where the HTML form is. This might not be what you're trying to do, so if you need to redirect, add the PHP code to a separate file and add it to your website and to the action="" parameter.
<form action="" method="POST">
<input type="text" name="url" size="65" placeholder="http://yoururl"/>
<input type="submit" name="submit" value="Go!">
</form>
<?php
if(isset($_POST['submit']))
{
$tags = get_meta_tags($_POST['url']);
if($tags['generator'])
echo "CMS: <b>" . $tags['generator'] . "</b><br>";
else
echo "No CMS info available.<br>";
}
?>
Give this a shot.

Form handling in php

I'm trying to set the value in my input text using the get method. However, when I try to render the page, it keep showing my code in the text field instead. Can someone explain what I did wrong?
<form name="quotepage" action="search.php" method="get">
<b>Stock Symbol:</b>
<input type="text" size="8" name="sb" value="<?php echo $_GET["sb"]; ?>" />
<input type="submit" value="Quote" onClick="quotepage.action='search.php';"/>
</form>
When I try to render the page, it will show my code in the value tag in my text field.
If you are seeing an error/warning/notice in your text box, try changing:
<?php echo $_GET["sb"]; ?>
into:
<?php echo isset($_GET["sb"])?$_GET["sb"]:""; ?>
to avoid showing the content if there was no content yet.
And better yet, change it to:
<?php echo isset($_GET["sb"])?htmlspecialchars($_GET["sb"]):""; ?>
to also escape nasty characters such as " that otherwise will break your HTML.
If you are actually seeing <?php echo $_GET["sb"]; ?> inside your text box, then you are having problems running PHP. Check that your script file name ends with .php and check PHP is working on your system.
Do you have a LAMP stack or similar set up?
You need Apache running with PHP installed at the least for this. Also what is this 'onClick="quotepage.action='search.php';"' attribute for?

PHP Form with PHP variable

Im try to Programm some php.
i have a site : /index.php?go=newstep&callid=2
Where i put:
<form method="post" action="addnew.php"> <input type="text" name="user" /> <input type="text" name="text" /> <input type="hidden" value="<?php echo($_GET["callid"]); ?>" name="test" />
This is because the next site "addnew.php" Needs the value "callid" from the link to ?go=newstep&callid=2
Why isn't it working?
Is there another way?
Thank you
If You want to use GET method, You can simply put Your variable as part of the link in action attribute. You don't have to use hidden input. Something like that:
action="addnew.php?callid=<?php echo $_GET['callid']; ?>"
Additionally, '"' char in attribute "value" may cause problems, because HTML may interpret it as end of the attribute value.
EDIT:
Exactly, You are using POST method in form, thus You are sending You variable, callid, by POST method now and it will be available in $_POST global array in addnew.php script, not in $_GET global array.

PHP Script won't load

I'm new to web development and have been wrestling with this problem for several hours now, so I've decided to turn to your wisdom. I'm trying to design a little webpage with a database for my wife to store her recipes in, but I'm having trouble getting form submission to work. Here is the code for the webpage where I take the form information in:
<html><body>
Enter the information below to add a new ingredient for use in your recipes.
<form action="add_to_database.php" method="post">
Name: <input name="name" type="text" />
Serving: <input type="text" name="serving" />
Calories: <input type="text" name="calories" />
<input type="submit" />
</form>
</body></html>
And here is some silly code I've been trying to display on the page to see if I can even get the form submission to work:
<html><body>
<?php
$name = $_POST['name'];
echo $name."<br />";
?>
</body></html>
Unfortunately, the page comes back as completely back (after I hit the submit button). What's absolutely baffling to me is that I've copied and pasted the examples from this page into some files and everything seems to work fine. So it seems as though apache and php are working correctly, but I'm messing up somewhere along the way. My apologies in advance if this seems like a stupid question but for the life of me I can't figure it out.
Do you name the other file as add_to_database.php where the form is submitted. Instead you can test on teh same page by removing the add_to_database.php from the form action.
form action="" method="post">
Name: <input name="name" type="text" />
Serving: <input type="text" name="serving" />
Calories: <input type="text" name="calories" />
<input type="submit" />
</form>
and write the php code on the same page as
$name = $_POST['name'];
echo $name;
If this is not working for you. Create a php file named test.php and type phpinfo(); there.
Verify that your page is indeed being processed by PHP - obvious question, but does your PHP file have a .php extension? Do other things like phpinfo() or echo "Test"; work?
Check the error log in /var/log/apache2/error.log or similar (if on Linux, dunno where that'd be on Windows).
Try turning display_errors on in the PHP configuration (this is a good idea only for a development install, not a production server).
a bit of a longshot, but if you can verify that php is pro essing the page. Clean up your html, you are missing the dtd, head, and encoding. . hard to say how a browser is going to interpret a form (missing a name attribute) without these.

Minimum script to extract the content from a page at another site in PHP

Very simple thing. I sometimes want to access urban dictionary but it is blocked where I am.
Given a form like
<form method="get" action="<?PHP echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<input type="text" name="what" value="" />
<input type="submit" />
</form>
what do I need to add to have it return the content of the entry from urban dictionary for value entered? For example using CURL or getFile or something as simple as possible
Update:
This works!
<form method="get" action="">
<input type="text" name="what" value="" />
<input type="submit" />
</form>
<?PHP
$what = isSet($_GET["what"])?htmlentities($_GET["what"]):"";
echo file_get_contents("http://www.urbandictionary.com/define.php?term=".urlencode($what));
?>
very simple, very weird file_get_contents($_POST['what']);
You need to do some DOM parsing with PHP. You can use the native DOM parser in PHP5 (recommended, see here http://www.ibm.com/developerworks/library/os-xmldomphp/), or a library like Simple HTML DOM Parser (http://simplehtmldom.sourceforge.net/).
You can then use your post data to choose the URL.
Maybe you are looking for something like http://pici.picidae.net/ rather than programming something which will be blocked by your network anyway.

Categories