New to php, don't have code for this, just a question.
I have a form that accepts user input, then validates the input.
input.php
if invalid, it repeats the input page.
if valid, it proceeds to another
process.php
What i don't understand is how to get the user data from input.php to process.php
I prefer to do this without storing the data in a database or file.
Any help would be appreciated.
You can use SESSION for storing the values on server or pass the values through URL using GET.
Please refer the php manual to have an idea on using $_SESSION and $_GET
On process.php just print the data using $_POST ,$_GET or $_REQUEST you will get all data in array which you processed through the form.
Eg. index.php
<html>
<body>
<form action="process.php" method="post">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
process.php
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
Related
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've been working for several hours trying to get this to work properly. The page I have a form on is /index.php?action=pagename. I have a form that needs to get a variable from the following /index.php?action=pagename&thing=something. My HTML form going like this:
<form role="form" action="pagename" method="get">
<input type="text" name="thing">
</form>
This form is located on /index.php?action=pagename and I want to get &thing from that URL.
Any ideas?
The problem I'm having is that when the form is submitted, the URL redirects to index.php?thing instead of staying on index.php?action=pagename.
It looks like you might be having some trouble with <forms> in general and not just PHP so here is an overview:
<!-- the action is where you want to send the form data -->
<!-- assuming THIS code is the index.php file then we want to send the data to ourselves -->
<!-- the method is GET so it will be directly accessible from the URL later -->
<form action="index.php" method="GET">
<!-- add a hidden value for pagename -->
<input type="hidden" name="action" value="pagename">
<!-- the name called "thing" will be appended to the URL and it's value as well -->
<input type="text" name="thing" value="<?php echo (isset($_GET['thing']) ? $_GET['thing'] : ''); ?>">
<br>
<!-- click this button to submit the form to itself -->
<!-- once this has been submitted then you can retrieve the URL value with $_GET as you can see above -->
<input type="submit" value="submit" />
</form>
You can simply use the PHP-variable $_GET['thing'] to get the value.
The action attribute of the form element is the target script which will be called on submit. If blank it will be the current script which is showing the form. You also can only give url parameters beginning with ?.
<form role="form" action="?action=pagename" method="get">
<input type="text" name="thing">
</form>
Exerpt from php.net: http://www.php.net/manual/de/tutorial.forms.php
One of the most powerful features of PHP is the way it handles HTML
forms. The basic concept that is important to understand is that any
form element will automatically be available to your PHP scripts.
Please read the manual section on Variables from external sources for
more information and examples on using forms with PHP. Here is an
example HTML form:
Example #1 A simple HTML form
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
There is nothing special about this form. It is a straight HTML form
with no special tags of any kind. When the user fills in this form and
hits the submit button, the action.php page is called. In this file
you would write something like this:
Example #2 Printing data from our form
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
A sample output of this script may be:
Hi Joe. You are 22 years old.
Basic script I made to get the variables from the URL:
<?php
if (isset($_GET["action"]) && isset($_GET["thing"])) {
$action = $_GET["action"];
$thing = $_GET["thing"];
echo $action .PHP_EOL . $thing;
}
?>
This will output the following for the URL /index.php?action=pagename&thing=something
pagename
something
Though you should probably learn how to use forms properly first.
How do I link the PHP to the HTML form? I understand how to do the PHP and how to do the HTML, but how do I link the php to the html or is that automatic,
how does the HTML form know about the PHP?
In your form set the action attribute to the path of your php script eg:
<form action="/path/to/php/script.php" method="post">
...
</form>
You set your action="" in your form to point to your PHP script. When the user clicks the submit button in your form, the PHP script will be called and the formdata will be handed over to the PHP script.
The method you choose when making your form is how PHP will gather the values passed in.
As such:
<form action="handler.php" method="post">
<!-- OR -->
<form action="handler.php" method="get">
The action tells where the form values will be sent to and the method tells how the values of the items in the form will be passed back to the server. The post method will send the values back so they may be retrieved by the $_POST array (both post and get can be retrieved by the $_REQUEST array). For example:
<input type="text" name="myInput">
Will post back to the server and can be retrieved by
$var = $_POST['myInput'];
It's always best to test if there is actually an input, and the following can be used
if(isset($_POST['myInput'])) { /*do something if set*/}
else{ /*do something if not set*/}
If the form was submitted by the get method, the values of the form is passed back in the URL, like such:
http://www.domain.tld/handler.php?myInput=someValue
The value is then retrieved by using the $_GET array:
$var = $_GET['myInput'];
Once again, you should test that it exists.
For good examples and explanations, please read a PHP book or search for PHP and HTML forms. This is the very basics of PHP.
Try this in a php file
<form action="" method="post">
<input type="text" value="html form data" name="name" />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST['submit']))
echo 'I am php. I know this value is from html - '. $_POST['name'];
?>
If you are talking about refilling your form with the php values: inside your input fields, just add the request variable.
<input type="text" name="input1" value="<?=$_REQUEST['var_name']?>" />
If you are talking about sending date to php, just point to a file using the form action.
<form action="file.php" method="post">
</form>
Then you process all the data in that php file.
I am just starting out in PHP/Html web development.
I have a basic page with a username and password field and a submit button.
I want to get the data from the username and password boxes into the php.
Here is my code...
<html>
<head>
<title>PAGE TITLE</title>
</head>
<body>
<h1 align = "center">Harro</h2>
<form align = "center">
Username: <input type="text" name="firstname"> <br>
Password: <input type="password" name="pwd"> <br>
<input type="submit" value="Enter">
<?php
???
?>
</form>
</body>
Specify the URI of the PHP program the form should be submitted to using the action attribute.
<form action="foo.php">
Then, when the form is submitted, PHP will populate the $_GET or $_POST superglobal with the data.
What you want is learning to code PHP web-apps from ground-up.
Currently what you will get from answers is just the code to get the Username and Password from the user. But the main thing comes after that. Like validation, and checking the login information from the database and then retrieving it.
Personally, I would recommend you to read O'Reilly Media's Learning PHP, MySQL, JavaScript, and CSS.
add name attribute in submit button
eg: name="submit"
And
add attribute in form tag method="post"
and write php code at top
<?php
if(isset($_POST['submit'])){
echo "USERNAME ".$_POST['firstname'];
echo "PASSWORD ". $_POST['pwd'];
}
?>
I'm a newbie learning and trying to understand how html forms and php processing works.
Came across this pair of examples:
HTML FORM:
<html>
<body>
<form action="hello-web.php" method="GET">
<label>Name:</label>
<input type="text" name="yourName" size="24">
<input type="submit">
</form>
</body>
</html>
PHP PROCESSOR:
<?php
$fname = $_GET["yourName"];
echo "Hello $fname!";
?>
OUTPUT SHOULD BE:
Hello Entered/Example Name!
QUESTION:
When I try to change the variable "yourName" (on BOTH HTML and PHP files) to, for example "typeName" , the entered name on the form does not show up.
In other words, the output becomes just: Hello !
Is "yourName" a standard php or html variable? Can it not be changed to what ever you want it to be?
Better yet, how exactly does the form process data?
Here is my altered code that won't output the entered name (I posted here as an answer because all the codes shows up as a continuous line, like a paragraph, when I paste as a comment to your answer:
HTML FORM(altered--typeName):
<html>
<body>
<form action="hello-web.php" method="GET">
<label>Name:</label>
<input type="text" name="typeName" size="24">
<input type="submit">
</form>
</body>
</html>
PHP PRCESSOR (altered--typeName):
<html>
<body>
<?php
$fname = $_GET["typeName"];
echo "Hello $fname!";
?>
</body>
</html>
You can see what data is available to you from the submitted form by outputting the entire array. Since your form method is GET, the following will show you all that was submitted:
var_dump( $_GET );
From this, you can see what the variable names should be in your PHP script.
Array
(
[YourName] => Jonathan
)
Anytime you come across a disconnect between what is being submitted, and what you're expecting, check $_GET (or if your method is POST, you would check $_POST).
For instance, if I were trying the following:
echo $_GET["yourName"]; // nothing output to the screen
I could refer to the array contents printed above and see that the correct key is "YourName":
echo $_GET["YourName"]; // Jonathan
$_GET["yourName"]; Contains a value based off of the input of the form field. It's a php superglobal http://us3.php.net/manual/en/reserved.variables.get.php
It sounds like you're changing the html form, but your not entering a value through the form. Therefore, $_GET["yourName"]; is empty