HTML PHP Form Processing; Variable - php

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

Related

Php form example is not working

I'm learning Php, reading it from a book following examples.
But something isnt working as described in the book, i dont understand why not
I have installed XAMP, and am running PHP version 5.5.9.
register_globals has not been set as was described in the book (as i understood this is default in v 5.5.9)
There are two .php pages
The first page is called form.php, the book didnt explain where to put.
It didnt work in the root, and so i made phpforms folder and placed it there
<html><head><title>First HTML Form</title></head>
<body>
<table cellpadding ="15"><td bgcolor="lightblue" >
<form action="/phpforms/form.php" method="POST" >
<p> Please enter your name: <br /> <input type="text" size=80 name="your_name">
<p> Please enter your phone: <br /> <input type="text" size=80 name="your_phone">
<p>
<input type=submit value="submit"><input type=reset value="clear">
</form>
</table></body></html>
The seccond page is made to read the entered vaues.
<HTML><Head><Tile>Show me the data</Title></Head><body>
<?php extract($_REQUEST, EXTR_SKIP); // Extracting the form input
print "Welcome to PHP $your_name<br />";
print "Can I call you at $your_phone<br />";
?>
</body></HTML>
When I enter data in the first page, it gives no error.
However when i then try to readout the entered data in the seccond page it doesnt work
I get two lines showing
Notice: Undefined variable: your_name in C:\xampp\htdocs\Peter.php on line 3 *
Notice: Undefined variable: your_name in C:\xampp\htdocs\Peter.php on line 4 *
Its an introduction to php forms, but well its pretty essential for going on with this book. I'm new to this could someone help me out here ?.
Simply add quotes around the type="" attribute and insert a > at the end of the form.
It is important to understand how the POST method works. The action attribute on your first form should contain the url of the destination page. Then use
$name = $_POST["your_name"];
echo $name;
to display the information in the second page. the $_POST form returns an associative array so all of the keys correspond to the name="" attribute in the form and all of the values equal the value of the form. Be sure to use: if (isset($_POST["your_name"]){} to handle any exceptions as well.
Enclose your <form> tag's type attribute with quotations.
<input type=submit value="submit"><input type=reset value="clear">
should be
<input type="submit" value="submit"><input type=reset value="clear">
Other than that, your code appears to be working fine.
Whatever book you are following, I would recommend you to first check some HTML basics.
In any case, I've re-written the whole code here, actually trying to be as close close as possible to your.
What I've done is:
Define a DOCTYPE foreach file (this is important, not that we
have HTML5.
Syntax errors have been corrected, almost everywhere (in both scripts).
Tried to put some effor to make this code a little bit more responsive. Most of the tags you have used are deprecated, such as cellpadding, bgcolor.
What the tutorial was about is using the extract php function to extract the values passed to the form into some temporary variables in order to don't have to store each variable using $_POST, which is clever but you don't see that often.
First of all, check the extract() function: http://php.net/manual/en/function.extract.php
What I've done, I've rewritten your main code:
It should have any name, in your case, it SHOULD BE peter.php, in the main directory:
<!doctype HTML>
<html>
<head>
<meta charset="utf-8">
<title>First HTML Form</title>
</head>
<body>
<table>
<tr>
<td style="background: lightblue; padding: 1%;" >
<form action="/phpforms/form.php" method="POST" >
<p> Please enter your name:</p> <br /> <input type="text" size=80 name="your_name">
<p> Please enter your phone:</p> <br /> <input type="text" size=80 name="your_phone">
<input type="submit" value="submit"><input type="reset" value="clear">
</form>
</td>
</tr>
</table>
</body>
</html>
Most important:
THE CHARSET: Nowaday, please define a charset.
Your table structure was completely wrong. It still doesn't have a sense either, but at least it has a correct syntax.
I've corrected all the syntax error you had: First, you didn't close your <form, second, you didn't close any <p> tag with its </p> and third, you didn't enclose properly your submit and your reset button, but some browers still would interprete it correctly.
Now, in order to make it work, you should have, in your C:\xampp\htdocs\ folder, a folder named phpforms CONTAINING A FILE CALLED form.php. If you don't have such, your form will never work, because it is aiming to the form.php script which is located in the /phpforms folder. Don't forget that you have defined that in your action parameter in the form ahead, by writing: action="/phpforms/form.php".
At this point, the form.php page should be structured like such:
<!doctype HTML>
<html>
<head>
<meta charset="utf-8">
<title>Show me the data</title>
</head>
<body>
<?php
extract($_REQUEST, EXTR_SKIP); // Extracting the form input
print "Welcome to PHP $your_name<br />";
print "Can I call you at $your_phone<br />";
?>
</body>
</html>
How does it work?
Using extract() on $_REQUEST will get the $_REQUEST array and explode it in a serie of variables according to the name of the compiled form.
Since you have named the two inputs in the previous form as name="your_name" and name="your_phone", the extract() function will parse the value of the input named "your_name" in the variable $your_name and the value of the input named "your_phone" into the variable $your_phone.
Once you extracted the values, so, you can easily print them using:
print "Welcome to PHP $your_name<br />";
print "Can I call you at $your_phone<br />";
The output, would be this:
main page (maybe peter.php?) :
Once submitted:

PHP is not passing my user entries

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."
}

PHP with HTML textarea

I am quite new to PHP/HTML all this kind of stuff so sorry if this is an easy question!
Is there a way to read the contents of an HTML text area and pass it into a .php file?
Or even just reading the contents of the text area into a variable would do.
Thanks
You can achieve that with a basic form:
index.php:
<?php
if ($_POST) // If form was submited...
{
$text = $_POST["mytextarea"]; // Get it into a variable
echo "<h1>$text</h1>"; // Print it!
}
?>
<form method="post">
<textarea name="mytextarea"></textarea>
<input type="submit" value="Go!" />
</form>
And this can be done whatever input element you want e.g: inputs type text, password, checkbox, radio or a select or even and fresh HTML5 input type.
Give your textarea a name and post it to a PHP script with a form. The data from the textarea will be available in the $_POST['textareaName'] variable.
HTML
<form action="page.php" method="post">
<textarea name="myTextarea"></textarea>
<input type="submit" value="go" />
</form>
PHP
<?php
echo $_POST['myTextarea'];
?>
You can Post the form and can access the variable of the form with $_POST['myObj'];
Looking at the $_POST and fetching the value from the post through object name.
Here, "myObj" is an example object name.
you need to submit the form to get the textarea value. Or you can use Jquery to get the textarea content

Get variables from one PHP form to another

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>

Send values from HTML Form to PHP

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.

Categories