I am creating an online order form for a wholesale nursery. I am trying to test the PHP locally and have successfully set up WAMP. When I test basic scripts, they run just fine but when I try using _POST to send data to variable from HTML input fields, nothing gets transferred to my PHP variables as noted when I try echoing them to a page. Is there something I need to do to test _POST and _GET methods locally that I don't know about?
I tried testing my ability to do so using a simple example I found at W3 schools:
<html>
<body>
<form action="test.php" method="post">
Name: <input type="text" name="fname"><br>
Age: <input type="text" name="age"><br>
<input type="submit">
</form>
</body>
</html>
Here is the PHP:
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
Like I said, I used WAMP and both files are located in the wamp/www/ folder.
First of all all forms need a method (get, post) and an action (where to send the data) in the html.
<form action='your/location.php' method='post'>
<input type='text' name='username'/>
<input type='submit' value='submit'/>
</form>
All inputs must have a name attribute this is for referencing data in the php script
<input type='text' name='username'/>
In your/location.php you will receive an array with the data under the name:
$_GET or $_POST depending on your method set in the form tag.
To access specific data use $_POST['username'] (replace username with any names set on any of the submitted form inputs)
To print out on the screen use
echo $_POST['username'];
Check if your file is under the extension .php and has the name test
If you get partial output and by partial i mean only the html part start you php server Apache or Nginx or whatever wamp uses, it might not start with all the functionality without specified to do so.
Make sure you open the page under localhost if you are testing it separately.
If for some reason you can't seem to get wamp to work try xampp which is similar and in my opinion better and never failed me.
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.
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 am trying to implement a simple form in my Github page to learn how to use POST in html forms. The code I am using right now contains:
2014-07-05-post1.markdown:
<form action="/scripts/1.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
1.php:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
However this returns an error:
How do you correctly use POST requests in Markdown and Jekyll.
I think you can't.
Jekyll in the end generates just a static site, i.e. a bunch of HTML and CSS files. The server does nothing but serving those files, the server doesn't do any processing. But when you do a HTTP POST, you send data to the server and it would have to do something with the data (e.g. saving it to a database or sending an email).
So your form is correct but there's nothing on the server-side that can receive it. However, you could send the form data to a third-party server or use a PHP or node.js server for that.
Hi I'm learning php of a tutorial and these are my two files. I browse to my apache2 server via http://myservers-ip/form2.php
fill out the forms and hit the submit button, it calls my result.php page, but all it displays is "Hi ." where it should be like "Hi (userentry)."
Please help :-(
form2.php:
<html>
<head>
<title>Form</title>
</head>
<body>
<h1>Enter your name</h1>
<form method="post" action="result.php">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
and my result.php
Hi <?php print $username; ?>.
Using apache2 and mysql running on my box.
I'm not sure if the source code is correct or if there might be a misconfiguration? if so which config files would you need?
Thanks
Data sent via a form with POST action will be in the superglobal $_POST array. You would want to sanitize it before trying to use it for anything, but just starting with $_POST['username'] will get you closer to your end goal.
Edit: Whatever tutorial you are using, abandon it. It's clearly waaaaay outdated.
You're sending data from form via post so you need to get them in your result.php file from POST superglobal like so:
Hi <?php print $_POST['username']; ?>.
The data is being sent to results.php via POST method.
All post params are stored in $_POST param. So to get user name you need to get it from $_POST, in results.php. E.g:
<?php
// file: results.php
if (isset($_POST['username']){
echo "Hi, {$_POST['username']}.";
} else {
echo "No user name."
}
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.