Form handling in php - 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?

Related

PHP won't work inline with HTML elements

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" />

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.

Can't get POST value when submitting form

So here's my HTML form
<form method="POST" action="/process_forgot">
<input type="text" name="name" value="test">
<input type="submit">
</form>
And /process_forgot
if(isset($_POST['name'])){
echo "good";
}
else{
echo "string";
}
And all I get back is string. Which is weird because I'm posting the value and I set the name. I've done this tons of times, this is the only time i've ever had an issue. Any ideas?
Add the .php extension to the file location process_forgot...this should fix the issue because without that you have a redirect and all the POST data are lost; for this reason it always runs echo "string".
1) It may sound dumb, but try to see if you require a .php extension in the end of process_forgot in your action.
2) Try directly going to [YourWebsite]/[YourDirectory]/process_forgot.php and see if the file is accessible. You will, depending on your code, probably get a blank page, but that is expected.
3) Check if there is any other problem in your code that is preventing the script from running properly.

Setting HTML Attribute via PHP File

I'm new to PHP and seem to have ran into a problem I can't seem to get around.
I have a form on a secure page that creates a PHP file to store a text value. I named this variable $text.
The Form HTML Code:
<form action="upload_title.php" method="post">
<label for="text">Title 1:</label>
<input type="text" name="text" id="text"><br>
<input type="submit" name="submit" value="Submit">
</form>
The upload_title.php then seems to store the text input as $text in filename.php:
<?php
$var_str = var_export($_POST['text'], true);
$var = "<?php\n\n\$text = $var_str;\n\n?>";
file_put_contents('filename.php', $var);
?>
This seems to be functional as the form will generate filename.php, below is an example if I typed Store into the form input and submitted on the webpage.
<?php
$text = 'Store';
?>
Now the issue I'm encountering is not being able to retrieve this stored as a attribute in separate html document, the index.html in my case.
This was my best approach to changing the title attribute of an image:
<a href="upload/1.jpg">
<img src="upload/thumb.jpg" title="<?php include 'filename.php'; echo htmlspecialchars($text); ?>" alt="" class="image0">
</a>
This does not work, but I can see my JQuery detects that this is trying to be populated but does not extract the data from filename.php on the `index.htm' page.
Thank those in advance for your advice and insight, it is sincerely appreciated.
Your issue is probably the fact that you are using an html file instead of a php file, in this case index.html.
Your server is likely not set up by default to process .html files as php so the php does not get executed.
Apart from that it is not a very good way to store your value as when the php does get executed, you introduce a security risk and you use a lot more storage than necessary. You'd better store the value in a database or text file.

How do I add php if statement into my form

I have an update form where users can update their information, but some of the information should not be able to be updated if it already has a value.
From what I understand you must put php code outside the form tags so how would the form know if the database field has a value or not? Would this work below im guessing it won't
<form>
<?php
if (empty($user_data['fieldName'])) {
echo '<label>field name</label>
<input type="text" name="fieldName">';
}
?>
</form>
Does anyone know a way around this? Or how would i check before the field is displayed in the update area?
This part of the code is correct, you may have a problem when assigning the user data in the array $user_data.
An alternative would be to simply disable the input text, that way you could display information to the user without letting him change it.
<form>
<?php
$disabled = "";
if (empty($user_data['fieldName'])) {
$disabled = 'disabled="disabled"'
} ?>
<label>field name</label>
<input type="text" name="fieldName" <?php echo $disabled; ?>/>
</form>
Of course you can do in that way but you should check on the server too if not update exist
Also the most readable way of do this is using alternate php syntax like this
<form>
<?php if (empty($user_data['fieldName'])): ?>
<label>field name</label>
<input type="text" name="fieldName">
<?php endif; ?>
</form>
in that way you can see colors of your html code
see this link,
Alternative php syntax
the doc is for codeigniter but is applicable on clean php
Sorry for my bad english :)
You should have a presentation code, like this :
<form>
<label>field name</label>
<input type="text" name="fieldName" <?=!empty($user_data['fieldName'])?'disabled="disabled"':''?>>
</form>
And a script to update, where you must check if value is set before updating.
As mentioned in a comment, you can put PHP anywhere in a *.php file, so long as it is enclosed in the correct opening and closing tags.
That said, this is a two-sided process:
In the form I would suggest simply disabling the input, so the data is shown, but is not modifiable:
<form>
<label>field name</label>
<input type="text" value="<?php echo addslashes($user_data['fieldName']) ?>" name="fieldName" <?php if(!empty($user_data['fieldName'])) { echo 'disabled="disabled"; } ?> />
</form>
<?php /* Off the top of my head I can not remember how exactly to escape this
form value, it may be `htmlspecialchars`, but it eludes me in the immediate
moment. Sorry. */ ?>
And on your server-side, where you handle the form post data, you will want to double-check that someone didn't forge the data:
if(!empty($user_data['fieldName']) && !empty($_POST['fieldName'])) {
unset($_POST['fieldName']);
}
This way you avoid people modifying the form structure on the client to forge data into your system. You should always do similar checks.

Categories