I've been following this tutorial to learn php so I have a html script:
<html>
<body>
<form name="form" method="post" action="registernext.php">
<p>Username: <input type="text" name="username" size="15" maxlength="20" value="megaman"></p>
<p><input type="submit" name="submit" value="register"></p>
</form>
</body>
</html>
and the php script:
<?php
echo file_get_contents('php://input');
$test = $_POST["username"];
echo " testname = (".$_POST["username"].")";
?>
But what I get when I run it is:
username=megaman testname = ()
My problem is that the name (megaman) get's sent to POST but doesn't show up in the php script.
Also, I haven't made any changes to the php.ini as oppose to some others that had problems when they did so.
I don't know if this helps but I'm running this via xampp (fresh download).
Help would be deeply appreciated, as searching for solutions haven't been successful.
try replacing all the PHP code with
<?php
var_dump($_POST);
?>
submit the form. if anything comes up then nothing is wrong with PHP.
Related
What I'm trying to do is get simple input from the user an print it out using echo within my php tags but not working for some reason. I'm still a beginner to php so if anyone could assist me, that be dope.
Here's my code :
<p>
<form method="GET">
<input type="text" name="firstname">
<input type="submit" value="sub">
</form>
</p>
<?php
echo $_GET['firstname'];
?>
The file is saved with a .php extension and at the moment, I'm running it locally on my computer using apache. The HTML for the form appears but when I click the submit button, it does not echo what was inputted. I know its not going to the code within the php tag but I'm unsure on how to make it go there.
To finish this you will need a action which means this:
<p>
<form method="GET" action="index.php">
<input type="text" name="firstname">
<input type="submit" name="submit" value="sub">
</form>
</p>
Also if you would like to get the result from the form whenever you hit that submit button, you will have to "tell" php that you would like whenever you hit that submit button to print the result. The simpliest way is this:
<?php
if(isset($_GET['submit']))
{
echo $_GET['firstname'];
}
?>
There are two things that you might wanna do:
In the form action write <?php echo $_SERVER['PHP_SELF']; ?>
The PHP_SELF redirect you to the same .php file with the form data
To avoid any exception, use isset function in the last part of your code, which would check if the POST variable exist or not.
<?php
if(isset($_GET['firstname'])){
echo $_GET['firstname'];
}
?>
I'm trying to run a very simple script to test the "action=echo $_SERVER['PHP_SELF'];" in a HTML form (with php start and end on it but this site doesn't allow me to introduce those chars). This is the script:
<?php if(isset($_POST['submit'])) { echo "it works"; } ?>
<form name="test" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="submit" value="Submit Form">
Problem is whenever I hit the submit button I get a "Safari can't find the file" error stating that "no file exists at the address" where I run the script.
What am I doing wrong here?
Many thanks in advance for your help!
Eduardo
Try using $_SERVER['REQUEST_URI'] instead of $_SERVER['PHP_SELF']. I think you are using mod_rewrite and that's why it cannot find the file.
You don't need to set the action attribute of the form, if you want to post data to the same page.
<form name="test" method="post">
...
<input type="submit" name="submit" value="Submit Form" />
</form>
should do it.
Cheers!
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.
I have a php file that does a whole sleuth of user account stuff. It's purely being used to test if the features are working. However the script stops before the first feature is output. I have determined that the code is failing somewhere in this code:
...more above...
Authentication Form<br>
<br>
<?php if($this->session->userdata('authenticated')):?>
Welcome back <?php echo $this->session->userdata('userid');?><br>
<form action="<?php echo base_url();?>/account/logout" method="post">
<input type="submit" value="Log Out"/>
</form>
<?php else:?>
<form action="<?php echo base_url();?>/account/login" method="post">
Username: <input type="text" name="username"/><br>
Password: <input type="text" name="password"/><br>
<input type="submit" value="Login"/>
</form>
<?php endif;?><br>
...more below...
What i can not figure out is why this code if not executing. No errors are being outputted and i have used code similar to this before with no problem. Any help would be greatly appreciated.
One last note:
I'm using xampp running on my laptop and using codeigniter. newest of both.
The really snazzy way is to use xdebug http://www.xdebug.org/
The brute force way might be to lace your file with
echo __FILE__, ":", __LINE__, "\n<br/>";
and
var_dump(get_defined_vars());
plus also read up on setting error_reporting to an appropriate level for debugging plus show_errors in the PHP manual # php.net
dear all
I am a beginer in PHP world (PHP 5.3.5) my web server is IIS fastCGI on win xp
I tried to pass values from the HTML form to the php but the data are not passed
this is my html file
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
and this is my php file welcome.php
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
After pressing submit the output was like this
Welcome!
You are years old.
but it should be like this
Welcome John!
You are 28 years old.
Can you please help me with this.
on IIS change in the form: method="post" to method="POST" <-- UPPERCASE should solve your issue.
Anyway what about the apache? and PHP version around just 5, flat five :)
5.3.5 on IIS for the beginner? sounds like to get the Mount Everest by night without gloves
That is pretty standard simple code, and looks correct. One thing you might look at is the way PHP is configured on your server. Also, try using $_REQUEST in place of $_POST on your welcome.php page and see if it still does the same thing.
Try naming your form as well
After looking at your code it seems that it must be a configuration issue.
Steps to resolve this:
Firstly is the code that spits out the page actually welcome.php
Secondly add the following to your php block
var_dump($_POST);
Click submit and if this still shows
array (
)
if so then do
var_dump($_REQUEST);
and post the contents to your post, and show us.