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.
Related
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 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.
I have a desktop application passing POST data (text and an image file) onto a PHP file on an Apache Webserver. For some reason the POST data is coming through empty. Does anyone have any idea what could be causing this?
All I am trying for now is to display the POST data using:
<?php print_r($_POST); ?>
But I'm getting nothing. No errors and no data. The strange thing is this works perfectly on my old server - but with my new host the $_POST var is coming through empty so I'm confident it isn't an issue with the app.
The desktop app was built by someone else using Poco to send the HTML Form data so I don't know too much about it but I know if definitely works as it is currently working on my current other webserver.
Weirder still, when I don't attach an image everything works as expected. As soon as an image is attached (<100kb) I get no data whatsoever.
The Poco HTML Form is using multipart/form-data as the encoding type when an file is attached and application/x-www-form-urlencoded when there is no attachment (default).
I'm stumped here and wondering if anyone knows what could be going on or at least what I should be looking to try at?
Thanks in advance.
Edit: I also forgot to mention that it works if I submit it from another webpage without issue.
Double edit: Here is the equivalent of the form in HTML:
<form enctype="multipart/form-data" action="http://DOMAIN.COM/savedata.php" method="POST">
name: <input type="text" name="name" /><br />
email: <input type="text" name="email" /><br />
gender: <select name="gender"><option value="0">Female</option><option value="1">Male</option></select><br />
photo: <input name="photo" type="file" /><br />
<input type="submit" value="Upload File" /></form>
This question already has answers here:
PHP POST not working
(10 answers)
Closed 8 years ago.
I have the simplest form possible and all I want to do is echo whatever is written in text box.
HTML:
<form action="" method="post">
<input type="text" name="firstname">
<input type="submit" name="submit" value="Submit">
</form>
PHP:
if(isset($_POST['submit'])){
$test = $_POST['firstname'];
echo $test;
}
The problem is it's not working on my server (it works on another server). Does anyone has an idea what could be wrong? There are other forms on the server and are working fine.
I had something similar this evening which was driving me batty. Submitting a form was giving me the values in $_REQUEST but not in $_POST.
Eventually I noticed that there were actually two requests going through on the network tab in firebug; first a POST with a 301 response, then a GET with a 200 response.
Hunting about the interwebs it sounded like most people thought this was to do with mod_rewrite causing the POST request to redirect and thus change to a GET.
In my case it wasn't mod_rewrite to blame, it was something far simpler... my URL for the POST also contained a GET query string which was starting without a trailing slash on the URL. It was this causing apache to redirect.
Spot the difference...
Bad: http://blah.de.blah/my/path?key=value&otherkey=othervalue
Good: http://blah.de.blah/my/path/?key=value&otherkey=othervalue
The bottom one doesn't cause a redirect and gives me $_POST!
A few thing you could do:
Make sure that the "action" attribute on your form leads to the correct destination.
Try using $_REQUEST[] instead of $_POST, see if there is any change.
[Optional] Try including both a 'name' and an 'id' attribute e.g.
<input type="text" name="firstname" id="firstname">
If you are in a Linux environment, check that you have both Read/Write permissions to the file.
In addition, this link might also help.
EDIT:
You could also substitute
<code>if(isset($_POST['submit'])){</code>
with this:
<code>if($_SERVER['REQUEST_METHOD'] == "POST"){ </code>
This is always the best way of checking whether or not a form has been submitted
Dump the global variable to find out what you have in the page scope:
var_dump($GLOBALS);
This will tell you the "what" and "where" regarding the data on your page.
I also had this problem. The error was in the htaccess. If you have a rewrite rule that affects the action url, you will not able to read the POST variable.
To fix this adding, you have to add this rule to htaccess, at the beginning, to avoid to rewrite the url:
RewriteRule ^my_action.php - [PT]
Instead of using $_POST, use $_REQUEST:
HTML:
<form action="" method="post">
<input type="text" name="firstname">
<input type="submit" name="submit" value="Submit">
</form>
PHP:
if(isset($_REQUEST['submit'])){
$test = $_REQUEST['firstname'];
echo $test;
}
Have you check your php.ini ?
I broken my post method once that I set post_max_size the same with upload_max_filesize.
I think that post_max_size must less than upload_max_filesize.
Tested with PHP 5.3.3 in RHEL 6.0
FYI:
$_POST in php 5.3.5 does not work
PHP POST not working
try doing var_dump($_GLOBALS).
A potential cause could be that there is a script running before yours which unsets the global variables. Such as:
unset($_REQUEST);
or even.
unset($GLOBALS);
This could be done via the auto_prepend_file option in the php.ini configuration.
try this
html code
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="firstname">
<input type="submit" name="submit" value="Submit">
</form>
php code:
if(isset($_POST['Submit'])){
$firstname=isset($_POST['firstname'])?$_post['firstname']:"";
echo $firstname;
}
There is nothing wrong with your code. The problem is not visible form here.
Check if after the submit, the script is called at all.
Have a look at what is submitted: var_dump($_REQUEST)
html file and php file both should reside in htdocs folder in c:\apache2 (if you use apache web server).
Open html file by typing http://"localhost/html_file_name.html"
Now enter Your data in fields.. Your code will run..
Try get instead for test reasons
<form action="#?name=test" method="GET">
<input type="text" name="firstname" />
<input type="submit" name="submit" value="Submit" />
</form>
and
if(isset($_GET)){
echo $_GET['name'] . '<br>';
echo $_GET['firstname'];
}
so I've been fighting with this for a few days, and I just can't seem to make it work. Whenever I press the submit button, the browser should send the post variables to write.php, but instead, it just redirects back to the website homepage, or the Document Root. This should be really, really simple, and I've done it before, but now it doesn't work for me. What I want to know is if this is a problem with my webserver setup, or PHP, or just a stupid mistake on my part. It's just a simple HTML form, not really special, so here's the form itself, in index.php:
<p style="font-size:13px">
<?php
$rp = fopen('mainlog.txt', 'r');
while(!feof($rp))
{
$read = fgets($rp);
echo($read).('<br/>');
}
fclose($rp);
?>
</p>
<form action="write.php" method="post">
Name: <input type="text" name="user" /><br/>
Changes:<br/>
<textarea cols="70" rows="8" name="change" style="background-color:#555;color:#ccc;font-family:verdana,arial,helvetica,sans-serif;font-size:13px"></textarea><br/>
<input type="submit" value="Add Entry"/>
</form>
And here's where it send to, in write.php:
<?php
$fp = fopen('mainlog.txt', 'a');
$wr1 = $_POST['change'];
#$my_t = getdate(date("g"));
date_default_timezone_set("America/New_York");
$date = date("n").('/').date("d").('/').date("Y").(', ').date("g").(':').date("i").(':').date("s");
$who = $_POST['user'];
$write = $date.(' by ').$who.('
').$wr1.('
');
fwrite($fp, $write);
fclose($fp);
header('Location: http://www.zennixstudios.com/first/chlog/');
?>
I have tried this both on my Apache 2.2 dedicated server with PHP 5 on FreeBSD8.2, and on XAMPP for Windows, with the same results. I have a suspicion that it may have something to do with PHP, specifically PHP include(), because I have several of those on this page, and when I put this on a friend's computer with XAMPP, but without the included files, the include()s just put errors on the screen, but the HTML form suddenly works fine. So, are there any known conflicts with HTML forms and certain PHP functions?
Other Notes:
The code shown above for index.php is within the main page div, but if you want the whole page source just ask.
I'm pretty sure the error isn't in write.php, because I KNOW that the browser never sends anything to it, because it would at least put the date in mainlog.txt.
If you want to see what this looks like in context, go to http://www.zennixstudios.com/first/chlog/
Thanks,
Chaos
Here is your problem:
<table align="right"><tr><td align="right"><form action="/" method="post">Username: <input action="login.php" type="text" name="uname"/><br/>Password: <input type="password" name="passwd"/><br/><input type="submit" value="Login" align="right"/></td></tr></table>
You never closed the form up in your header for the username and password, so your <form action="/" method="post"> is being used for pretty much the entire page and your write.php form action is being ignored because a form is already, technically, open. You'll need to close the form in your header for the rest of your page to work properly.
To reiterate: nothing is being redirected, you're actually posting all the data from both 'forms' to the location / as specified.