I have this code to display PHP value in HTML form text field:
<form method="post" action="">
<input type="text" name="firstname" maxlength="30" value="<?php echo $firstname;?>"><span class="error"> <?php echo $firstnameErr;?></span>
Problem is I see <?php echo $firstname;?> being displayed in the text area instead of the actual user-input name or a blank field.
I tried using <? php echo and <?= and neither work any different, my server supports PHP.
How do I fix this?
i believe what is happening is you're opening the file by double clicking rather than "running" the file by navigating to it in your web browser.
i tested your code and it works fine. however, if i just OPEN the file rather than navigating to it via http, then the php code displays inside the text box.
remember, php (and other server side script types like jsp) have to be run, you can't just open them, or drag them into a browser window to test them.
so if, for example, you're using xampp or apache, you would go place the file inside your htdocs folder, open your web browser, and type something like localhost/plc.php
btw, here is the code as i tested it:
<?php
$firstname = "bob";
$firstnameErr = "phil";
?>
<html>
<head>
</head>
<body>
<form method="post" action="">
<input type="text" name="firstname" maxlength="30" value="<?php echo $firstname;?>"><span class="error"> <?php echo $firstnameErr;?></span></form>
</body>
</html>
Related
I have two files, testpage.html and testpage.php in the same folder.
This is testpage.html
<!DOCTYPE HTML>
<html>
<body>
<form action="testpage.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
This is testpage.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?><br>
</body>
</html>
The form seems to work okay but when I hit submit nothing shows up on the next page. no matter what I enter int he form, it always reads "Welcome" "Your email address is:" with nothing entered after that like its supposed to.
Do I have something configured incorrectly? Am I using the wrong browser (firefox)?
Thanks!
"I am accessing it via file:///C:/... should I be accessing it some other way instead?"
Just as I thought.
There you go. A web browser will not parse PHP directives that way.
It must be accessed as http://localhost/yourfile.xxx
Plus, a webserver and PHP need to be installed in order to parse PHP directives.
"I don't think I have Xampp or any virtual server set up... I'll look into that, any tips on where to start?"
You need to install one. Here are a few links to get you started and depending on the platform you are using.
https://www.apachefriends.org/
http://www.wampserver.com/
https://www.mamp.info
http://www.easyphp.org/
Pick your flavour.
I created a contact form for my website. When I click the submit button, it just shows the PHP code instead of the message it is supposed to show, like the text information the user inputs into the form.
Here is the HTML code for the form:
<html>
<link href="contact.css" rel="stylesheet" type="text/css" />
<head>
<title>Contact Us</title>
</head>
<body>
<form action="contact.php" method="post">
<div id="ContactForm">
<fieldset>
<h4>Contact Parquest:</h4>
<label class="labelone" for="name">Name:</label>
<input name="name"/>
<label for="email">Email: </label>
<input name="email"email"/>
<label for="commens">Comments: </label>
<textarea name="commens"></textarea>
</fieldset>
<fieldset>
<input class="btn" type="submit" value="Send Email"/>
<input class="btn" type="reset" value="Reset Form"/>
</fieldset>
</form>
</div>
</html>
I couldn't post the PHP code, because it just doesn't show when I try, so here's a screenshot:
There is no problem to your code.. The problem is on your environment.. I guess you are not running the html file through a server..
If the url on your browser looks like these:
file:///c:/path/to/your/file/page.html
then you are doing it wrong.. in order to run .php scripts, you need a web server like apache or nginx... the url of should be like these
http://localhost/path/to/file/page.html
then the php file should run as expected..
php files are interpreted scripting language and thus it needs an interpreter in the server in order to run.. if it is just browsed in the browser without a server, it will just output the code inside..
You need a web server to run php scripts.
Try installing wamp and then run your page under localhost.
I have executed the given code. It's working fine on my local server. Conventionally it should print the value inputs posted by form on contact.php.
But the thing I noticed in your PHP code is the variable name is $commets, instead of $comments.
I faced the same problem and when I tried starting my own server it worked.
when you are using Windows, open cmd and type in this;
php -S localhost:4040;
you will see something like this
PHP 8.1.1 Development Server (http://localhost:4040) started
meaning you have started your own server, then you can copy the link found in the parenthesis
so you copy say this "http://localhost:4040" and paste it in your browser then you can now direct it to your html document like this
http://localhost:4040/Desktop/Projects/index.html
now your form should work well with your php script when you submit
i had the same issue, even if appache & mysql were running on my computer.
in the html file, i wrote :
form action="http://localhost/file.php" method="POST"
instead of :
form action="file.php" method="POST"
It seems like connecting HTML forms to php files only works when you download wampserver and turn on all services...
Here's how it worked for me:
You can download wampserver from here: http://www.wampserver.com/en/ (follow instructions)
I saved it to the C: drive on my computer.
Next, you can create a php file and an html file both in the 'www' folder of the wamp folder in whatever drive you put it in. Copy the form code into the HTML file, and the php code into the php file.
Finally, go to 'localhost/yourhtmlname.html' and fill out the form. It should work as normal.
When I submit my form, I get sent to the correct file as specified in the action attribute of my form, but the PHP in the file isn't printing the variables at all... I've combed through posts of other people having the same issue, but none of their solutions fix my problem. I've stripped my code down to a simple, textbox, button, and a php file that's supposed to print the textbox value.
If it matters, I'm running this locally in chrome, not using any servers or websites yet, I'd like to get my code working locally before I upload to my server.
HTML
<html>
<form action="Submit.php" method="post">
<input type="text" placeholder="First Name" name="firstName" id="firstName" required>
</br></br>
<input type="submit" name="submitted" value="Submit">
</form>
</html>
PHP
<html>
<body>
Name <?php echo $_POST["firstName"]; ?><br>
</body>
</html>
All I get when I click the button is a white page with "Name" printed.
Thanks!
Running scripts in response to HTTP requests is something that's done by the webserver. If you just use local files, the script will simply be loaded into the browser as a text or HTML file, it won't be executed. You can't do form processing like this.
You need to run a local server, then access the form as http://localhost/form.html
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?
I've been trying to use PHP to allow a user to put in articles onto a certain site via a form.
The site,my code and my skills as a programmer are primitive in nature.
The user enters the input here:
<form action="articlepro.php" method="post">
Title:<input type="text" name="title"><br>
Sub-Heading:<input type="text" name="subhead"><br>
Intro:<textarea name="intro" rows="10" cols="40"></textarea><br>
Heading-1:<input type="text" name="head1"><br>
Content-1:<textarea name="cont1" rows="10" cols="40"></textarea><br>
<input type="submit">
</form>
The date is processed as such by the file articepro.php:
<html>
<body>
<?php
$title=$_POST["title"] . ".php";
$createFile=fopen($title,"x+");
$content=file_get_contents("articletemplate.php");
echo $createFile;
echo file_put_contents($title,$content);
echo $createFile;
?>
</body>
</html>
The aim of the code above is to first create a variable that stores the title from the form with the .php extension.Then the code creates a file with the same name as the $title variable.$content stores the html and php code of the file "articletemplate.php" as a string and "file put contents" puts that template into the file created earlier.
It may be of of value to know that artictemplate.php contains the template I wish for all new articles to have with bits of php that put in data as such:
<p><h1><?php echo $_POST["head1"];?></h1>
<?php echo $_POST["cont1"];?>
</p>
Now the problem I face is the fact that my code both creates the required files with the required title(Permisson:644),it also contains the intended template.BUT,I am unable to display anything when I open the created files(.php's) via URL.I either see a blank page or "k????????????????????????????????????????????????????????????????????????????????????????ä???????????????ä??"
I can open them if I download the created files off the server and change the extension to .html(cant use PHP then) though but no luck with .php or .phtml.
Webserver:Apache/2.2.24 (Unix