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:
Related
Hi I had URLs like this...
www.example.com/list.php?type=vehicles&stype=Cars
www.example.com/list.php?type=vehicles&stype=Cars&loc=Ludhiana
And to sort results, sort variable was appending at last of URL and URL after adding sort variable was
www.example.com/list.php?type=vehicles&stype=Cars&sort='**ANYVALUE**'
www.example.com/list.php?type=vehicles&stype=Cars&loc=Ludhiana&sort='**ANYVALUE**'
And on href i was using
Above code was working perfectly for me. It was adding sort variable at end of URL as i want. Also it was modifying the value of sort variable if variable was existing already in URL.
But then i made my URLs SEO friendly and URLs now are
www.example.com/vehicles/Cars
www.example.com/vehicles/Cars/Ludhiana
After adding sort variable it becomes
www.example.com/vehicles/Cars&sort='**ANYVALUE**'
www.example.com/vehicles/Cars/Ludhiana&sort='**ANYVALUE**'
But now as URLs are SEO friendly, So when i use same href, it doesn't work and it becomes
http://www.example.com/type=vehicle&stype=Car&sort=R-ASC
which results in a 404 error.
What will be the right href code so that it could do the following
Append sort variable at the end of URL, whether the URL has 2 varibles or 3 variables.
If sort variable already exists in URL, it will manipulate its value with current href.
Thanks in advance....
The first parameters char should be an ?.
http://www.example.com/?type=vehicle&stype=Car&sort=R-ASC
I am not sure what do you need, but try something like that:
Random HTML form that gets the attributes and their values (like ?type=vehicle&stype=blabla) and redirects you to the page you want (pure HTML used, without any PHP):
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="http://example.com" method="get">
<input type="text" placeholder="Type" name="type">
<input type="text" placeholder="Stype" name="stype">
<input type="submit" value="Search">
</form>
</body>
</html>
Also if you want a link to the page instead of getting redirect
<head>
<title>Test</title>
</head>
<body>
<form action="" method="get">
<input type="text" placeholder="Type" name="type">
<input type="text" placeholder="Stype" name="stype">
<input type="submit">
</form>
<?php
echo "<a href='http://example.com/listItems.php?type=". $_GET["type"] ."&stype=". $_GET["stype"] ."'>Link</a>";
?>
</body>
</html>
I hope i helped you less or more.
I have just started to learn HTML and PHP, but have run into a road block while following beginner tutorials. I am attempting to have the user input numbers into a form on the HTML page, then press submit to redirect to a PHP page that displays the values. The PHP page shows up and successfully displays prepared text but displays nothing for the values.
HTML code:
<html>
<body>
<head>
<title>Practice Page</title>
</head>
<h1>Numbers</h1>
<p>Put numbers in the boxes</p>
<form action="welcome.php" method="post">
NumOne: <input type="text" name="oynumone"><br>
NumTwo: <input type="text" name="oynumtwo"><br>
<input type="submit" value="Submit" id="SubmitRegister" name="submit" />
</form>
</body>
<html>
PHP code:
<html>
<body>
Number one is <?php echo $_POST["oynumone"]; ?><br>
Number two is <?php echo $_POST["oynumtwo"]; ?>
</body>
</html>
Both of the files are simply in the same folder in my documents. I understand that I need a server to host PHP content; I have downloaded MAMP for this, but I don't yet understand how to use it.
Any help would be most appreciated.
Store both file name with .php extension AND/OR update Welcome.php like below -
Welcome.php
<?php
if isset($_POST['submit'])
{
$oynumone = $_POST['oynumone'];
$oynumtwo = $_POST['oynumtwo'];
echo "Number one is ".$oynumone;
echo "Number two is ".$oynumtwo;
}
?>
Also check this
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'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
I was wondering if my code below is even correct, I've been having numerous errors with this, but am not sure if the problem really exists here. The code is below:
The user will click 'Exit Group'.
<p class="logout"><a id="exit" name="logout" href="#">Exit Group</a></p>
The code that should be execute when 'Exit Group' is clicked is below:
if(isset($_GET['logout'])){
//CODE TO BE EXECUTED
}
However, the code I am trying to execute when the user clicks 'Exit Group' is not even being executed. There is nothing wrong with the code within the braces, as numerous people have checked it. But I was wondering if my problem may lie in the code above? Thank you.
If you click the link, nothing happens because the URL only contains the fragment identifier #. Not even a GET request will be issued.
You use this kind of link normally to jump to an element inside the page (e.g. Top to jump to an element with ID top). This is completely handled in the browser.
And if you only put the fragment identifier there, just nothing will happen. This is very often used if the link should execute some JavaScript and should actually not link to something else.
You are testing the $_POST array at the server side. But this array only contains elements, if you initiate a POST request by a form. That means you need to create a form with a submit button, e.g.:
<form action="" method="POST">
<input type="submit" name="logout" value="Exit Group" />
</form>
Here comes the name attribute into play, which will be the key in the $_POST array. But assigning this on a normal link will have no effect.
You could do it also with the link, but with a GET request this way:
<a id="exit" href="?logout=1">Exit Group</a>
<!-- ^-- parameter must be part of the URL, name has no effect -->
and
if(isset($_GET['logout'])){
//CODE TO BE EXECUTED
}
Note that you have to pass a parameter logout it here.
It seems you have mixed up GET and POST requests. If you have a form, the name s of the form elements will be transmitted as parameters to the server. That means given this form:
<form method="POST">
<input type="text" name="foo" value="" />
<input type="text" name="bar" value="" />
<input type="submit" name="send" value="Send" />
</form>
if the user clicks on the submit button, the $_POST array at the server side will have the keys:
$_POST['foo']
$_POST['bar']
$_POST['send']
This does not work with links though. A click on a link will create a normal GET request, and here, the parameters must be part of the URL, appended after a question mark ? and separated by an ampersand &:
Link
will result in
$_GET['foo']
$_GET['bar']
$_GET['andMore']
You probably should read about the HTTP protocol.
a isnt a form control. it needs to be an input or select if it's within a form.
For manual linking, do href="/page?logout"
You're using a regular hyperlink, no form will get posted. you need a submit button of some kind in a form with method="post" to do that. regular links just result in GET requests and nothing will ever be posted that way.
edit: added simple example:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Form test</title>
</head>
<body>
<?if ($_SERVER['REQUEST_METHOD'] == 'POST'):?>
<pre><? print_r($_POST)?></pre>
<?endif;?>
<? // $_SERVER['REQUEST_URI'] holds the current URL, so we know that ?>
<? // we'll end up back in this file when the form is submitted. ?>
<form method="post" action="<?= $_SERVER['REQUEST_URI']; ?>">
<input type="text" name="textbox"
value="<?= isset($_POST['textbox'])?$_POST['textbox']:'Type something' ?>" />
<input type="submit" name="submitbutton" value="Submit" />
</form>
</body>
</html>
$_POST will only be filled if you use a form with method=post.
Yes. A POST and a GET are two different things ;)
if(isset($_GET['logout']))
This <a id="exit" name="logout" href="#"> should be <a id="exit" href="?logoff=true#">.
Then logoff will be in the $_GET array.