What is the difference between POST and GET in HTML/PHP [duplicate] - php

This question already has answers here:
When should I use GET or POST method? What's the difference between them?
(15 answers)
What is the difference between POST and GET? [duplicate]
(7 answers)
Closed 9 years ago.
I am coding a PHP script, but I can't really seem to get it to work. I am testing out the basics, but I don't really understand what GET and POST means, what's the difference? All the definitions I've seen on the web make not much sense to me, what I've coded so far (but since I don't understand POST and GET, I don't know how to get it to work:
<form name="mail_sub" method="get">
Name: <input type="text" name="theirname"> <br />
Email: <input type="text" name="theirpass"> <br />
<input type="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_POST['mail_sub']))
{
echo $_POST['theirname'];
}
?>

$_POST isn't working for you because you have the form method set to get.
<form name="mail_sub" method="post">
There's plenty of better info online as to the difference between post and get so I won't go into that but this will fix the issue for you.
Change your PHP as well.
if ( isset( $_POST['theirname'] ) ) {
echo $_POST['theirname'];
}

To answer the question:
GET and POST are one of the many request types in the standards of internet.
The difference is that GET can't post data, parameters will be appended to the url (url-parameters) which has it's limitations. POST does post parameters/data.
The standard is:
GET for getting data.
POST for creating data.
PUT for updating data.
DELETE for removing data.

replace
isset($_POST['mail_sub'])
by
isset($_POST['theirname'])
The main difference between GET and POST requests is that in GET requests all parameter are part of the url and the user sees the parameters. In POST requests the url is not modified and all form parameter are hidden from the user. If you have no file uploads or very long field parameter use GET. Use POST when going in production.

Post is like "sending" the data to the page without giving the user having any interaction with it, when Get shows the parameters in the URL and making it public for the user. Get is more often used on "unsecure" type of datatransactions like for example a searchform and POST is used when you want to make more secure things like a login form.
Using a Get will give you like
index.php?parameter=hello&parameter2=world
In your example you should use either POST in the method attribute in the formtag or $_GET in the php section
So either
<form name="mail_sub" method="post">
Name: <input type="text" name="theirname"> <br />
Email: <input type="text" name="theirpass"> <br />
<input type="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_POST['theirname']))
{
echo $_POST['theirname'];
}
?>
or
<form name="mail_sub" method="get">
Name: <input type="text" name="theirname"> <br />
Email: <input type="text" name="theirpass"> <br />
<input type="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_GET['theirname']))
{
echo $_GET['theirname'];
}
?>

GET - If form method is GET then on submitting the form, Form will be submitted to
xyz.com/submit.php?theirname=john&theirpass=password // CHECK HERE - form elements are in URL so not so secure
and can be checked by PHP
as
if(isset($_GET['their']))
{
$name=$_GET['their']; //name will return John
}
POST - If form method is POST then on submitting the form, Form will be submitted to
xyz.com/submit.php // CHECK HERE form elements are not in URL
and can be checked by PHP
as
if(isset($_POST['their']))
{
$name=$_POST['their']; //name will return John
}

you used method as GET in form,so your code should be like this
<?php echo $_SERVER['PHP_SELF'];?>//the form and action will be same page
<form name="mail_sub" method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="theirname"> <br />
Email: <input type="text" name="theirpass"> <br />
<input type="submit" value="Join!" style="width:200px">
</form>
<?php
echo $_GET['theirname'];
?>

Replace Your Code:
<form name="mail_sub" method="post">
Name: <input type="text" name="theirname"> <br />
Email: <input type="text" name="theirpass"> <br />
<input type="submit" name="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_POST['submit']))
{
echo $_POST['theirname'];
}
?>

Related

Need clarity on basics of PHP [duplicate]

This question already has answers here:
Can someone tell me the difference between $_REQUEST and the other 2 method($_GET,$_POST) for php [duplicate]
(1 answer)
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I have gone through some tutorials in W3Schools and others on HTML and PHP but I need a little clarity on how stuff works. I am trying to transport a value via URL using PHP from page1 to page2. Although I have a fair bit of idea on how to do it using HTML
Page1:
<form method="post" action="page2.php">
FIRST NAME <input type="text" name="fname"><br>
LAST NAME <input type="text" name="lname"><br>
<input type="submit" name="sub" value="sub">
</form>
Page2:
<?php
$fname=$_POST['fname'];
$lname=$_POST['lname'];
echo $fname."-".$lname;
?>
I want to use only PHP to transport values, I was planning to use a tag without using a form but I am sure it is not the right way because I won't be able to fetch values on page 2. Plz someone help me out clear these basics.
<?php
$name="hello world";
echo '<input type="button name="sub" value="submit">';
?>
You're going about it backwards. POST data goes in the body of the request, not in the URL. GET data goes in the URL.
If you want to have the data in the URL, you would change your form tag to:
<form method="get" action="page2.php">
and the page2.php code to:
<?php
$fname = $_GET['fname'];
$lname = $_GET['lname'];
echo $fname . "-" . $lname;
?>
Please note that this has a number of vulnerabilities (such as XSS) and is not how you should do these things.
You have to use 'GET'.so your data pass With Append into URL.
Page 1
<form method="GET" action="page2.php">
FIRST NAME <input type="text" name="fname"><br>
LAST NAME <input type="text" name="lname"><br>
<input type="submit" name="sub" value="sub">
</form>
Page 2
<?php $fname=$_GET['fname']; $lname=$_GET['lname']; echo $fname."-".$lname; ?>

How to send a get form wthout losing get parameters

I have a form located at a url containing get parameters,my form is also using this method.When the form is submitted it rewrites the previos get parameters.
Is there a simple way to rewrite only my form parameters?
I have in mind a Javascript solution ,however I want to know if there is a simpler way?Using HTML/PHP perhaps?
As far as I know, u u are not interested in using JS, then using form's hidden element is only way u have like this-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<input type="hidden" name="country" value="Norway">
<input type="submit" value="Submit">
</form>
<p>Notice that the hidden field above is not shown to a user.</p>
The question is how u can use it with PHP, right?
The solution is here-
//In PHP
if( isset($_GET['fromPerson']) )
{
echo $fromPerson;
}
So combined HTML and PHP code will be like this (assuming a get element from prevous page is named fromPerson)-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<?php
if( isset($_GET['fromPerson']) )
{
echo '<input type="hidden" name="country" value=".$_POST['fromPerson'].">';
}
?>
<input type="submit" value="Submit">
</form>
Lets say you get a parameter p1 from a get request, it should look like this:
http://server.com/?p1=123
In your form, you can add hidden fields that would have the same effect when you submit, like this:
<form method="GET">
<input type="hidden" value="<?php echo $_GET["p1"]; ?>" name="p1">
</form>
That way you can resend the variables as many times as you need.
I'm not sure I understand your question... Can you post your code?
I assume you mean something like this?
in index.php
<input type="hidden" name="id" value="<?php echo $id; ?>" />
in return.php
Edit

How use $_GET variable in a Wordpress page

I have a form for asking information about courses , every course has it page, but the information page is one for all.
The form should be something like that:
<form action="#" method="POST">
<label for="name">Name</label>
<input name="name" type="text">
<label for="email">Email</label>
<input name="email" type="email">
<input type="hidden" id="code" value="<?php echo $course_code; ?>">
<input id="submit" type="submit" value="Invia" />
</form>
I wish to change the var $course code according to the referrer page. (With a $_GET var)
I tried "Shortcode Exec PHP" plugin to execute php in wp pages, but doesnt work.
When you POST the form, the variable won't be set in $_GET but in $_POST. It's either one or the other, so if you want to read the $_GET var, you must also use GET on the form, like this:
<form action="#" method="GET">
<label for="name">Name</label>
...
(this is what Fred commented on, but I couldn't expand upon that comment due to my low rep)
I was wrong to use "Shortcode Exec PHP" plugin.
I set a shortcode:
$course_name = $_GET['cn'];
$courses= array("courses1","courses2","couses3");
if (in_array($course_name, $courses)) {
echo $course_name:
}
and the in the wordpress page can be used the name of the shortcode
[couse_name]
Now its work!
You can just use $_REQUEST so it doesn't matter if its a POST or a GET from the form. But I wouldn't use GET from a form unless it was a search or something where the user could bookmark the url and see the result. Mostly use POST for all other instances.
HTML form...
<form method="post">
<label>Name<br>
<input type="text" name="name">
</label>
...
<input type="submit" value="Invia">
</form>
PHP page that handles the form...
<?php
// $_REQUEST will contain POST, GET & COOKIE
echo $_REQUEST['name'];
?>

How do I retrieve a PHP variable in my HTML code?

I call a PHP script from my HTML form submit.
Then I process the values retrieved from this HTML form in the PHP script and now I want to display these values in another HTML form that I am calling from this PHP script.
How do I retrieve this variable?
Note: I tried echo but I think I actually want to display this value in the form (HTML) and not break it again in a PHP tag.
I'm not sure what you mean by "not breaking it again with a PHP tag". HTML on its own cannot access PHP variables. This is because PHP is a server-side language and HTML is a client side language. But here is the simplest way to print php variables retrieved from a form.
<form>
<p>Your name: <?php echo $_POST['name'] ?></p>
</form>
This is assuming that there was a form that submitted a field called 'name' using POST.
You can process the name in a php script at the top of the file and then simply echo it when you're printing the html. This way, you won't have too much php code mixed in with the HTML (which makes it look cleaner).
Once you got the values in the PHP script, are you calling a new script? If so, you might wanna save the values in $_SESSION["varible_name"]. If not, you just have to echo it.
It depends on how you are accessing your form data, either through $_POST or through $_GET. For simplicity, I'll assume your using $_GET and modify this example for more clarity.
So lets say you have a form hosted on welcome.php:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
Now the results will be returned back to the same page, so you want to modify the page to say:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" value="<?php echo $_GET["fname"]; ?>"/>
Age: <input type="text" name="age" value="<?php echo $_GET["age"]; ?>" />
<input type="submit" />
</form>
Though you'll notice that we're using the same page, and we can only have one version of the page, so we want to render if upon the condition that our variable has been set.
if (isset($_GET["fname"])){
//code to print second form
}
else{
//code to print first form
}
Or, in another way (using the ternary operator):
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" value="<?php echo ((isset($_GET["fname"]))?$_GET["fname"]:""); ?>"/>
Age: <input type="text" name="age" value="<?php echo ((isset($_GET["age"]))?$_GET["age"]:""); ?>" />
<input type="submit" />
</form>

How can I execute a PHP function in a form action?

I am trying to run a function from a PHP script in the form action.
My code:
<?php
require_once ( 'username.php' );
echo '
<form name="form1" method="post" action="username()">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
I echo the form but I want the function "username" which is called from username.php to be executed. how can I do this in a simliar way to the above?
<?php
require_once ( 'username.php' );
if (isset($_POST['textfield'])) {
echo username();
return;
}
echo '
<form name="form1" method="post" action="">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
You need to run the function in the page the form is sent to.
In PHP functions will not be evaluated inside strings, because there are different rules for variables.
<?php
function name() {
return 'Mark';
}
echo 'My name is: name()'; // Output: My name is name()
echo 'My name is: '. name(); // Output: My name is Mark
The action parameter to the tag in HTML should not reference the PHP function you want to run. Action should refer to a page on the web server that will process the form input and return new HTML to the user. This can be the same location as the PHP script that outputs the form, or some people prefer to make a separate PHP file to handle actions.
The basic process is the same either way:
Generate HTML form to the user.
User fills in the form, clicks submit.
The form data is sent to the locations defined by action on the server.
The script validates the data and does something with it.
Usually a new HTML page is returned.
A simple example would be:
<?php
// $_POST is a magic PHP variable that will always contain
// any form data that was posted to this page.
// We check here to see if the textfield called 'name' had
// some data entered into it, if so we process it, if not we
// output the form.
if (isset($_POST['name'])) {
print_name($_POST['name']);
}
else {
print_form();
}
// In this function we print the name the user provided.
function print_name($name) {
// $name should be validated and checked here depending on use.
// In this case we just HTML escape it so nothing nasty should
// be able to get through:
echo 'Your name is: '. htmlentities($name);
}
// This function is called when no name was sent to us over HTTP.
function print_form() {
echo '
<form name="form1" method="post" action="">
<p><label><input type="text" name="name" id="textfield"></label></p>
<p><label><input type="submit" name="button" id="button" value="Submit"></label></p>
</form>
';
}
?>
For future information I recommend reading the PHP tutorials: http://php.net/tut.php
There is even a section about Dealing with forms.
I'm not sure I understand what you are trying to achieve as we don't have what username() is supposed to return but you might want to try something like that. I would also recommend you don't echo whole page and rather use something like that, it's much easier to read and maintain:
<?php
require_once ( 'username.php' );
if (isset($_POST)) {
$textfield = $_POST['textfield']; // this will get you what was in the
// textfield if the form was submitted
}
?>
<form name="form1" method="post" action="<?php echo($_SERVER['PHP_SELF']) ?">
<p>Your username is: <?php echo(username()) ?></p>
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>
This will post the results in the same page. So first time you display the page, only the empty form is shown, if you press on submit, the textfield field will be in the $textfield variable and you can display it again as you want.
I don't know if the username() function was supposed to return you the URL of where you should send the results but that's what you'd want in the action attribute of your form. I've put the result down in a sample paragraph so you see how you can display the result. See the "Your username is..." part.
// Edit:
If you want to send the value without leaving the page, you want to use AJAX. Do a search on jQuery on StackOverflow or on Google.
You would probably want to have your function return the username instead of echo it though. But if you absolutely want to echo it from the function, just call it like that <?php username() ?> in your HTML form.
I think you will need to understand the flow of the client-server process of your pages before going further. Let's say that the sample code above is called form.php.
Your form.php is called.
The form.php generates the HTML and is sent to the client's browser.
The client only sees the resulting HTML form.
When the user presses the button, you have two choices: a) let the browser do its usual thing so send the values in the form fields to the page specified in action (in this case, the URL is defined by PHP_SELF which is the current page.php). Or b) use javascript to push this data to a page without refreshing the page, this is commonly called AJAX.
A page, in your case, it could be changed to username.php, will then verify against the database if the username exists and then you want to regenerate HTML that contains the values you need, back to step 1.
I think it should be like this..
<?php
require_once ( 'username.php' );
echo '
<form name="form1" method="post" action="<?php username() ?>">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
It's better something like this...post the data to the self page and maybe do a check on user input.
<?php
require_once ( 'username.php' );
if(isset($_POST)) {
echo "form post"; // ex $_POST['textfield']
}
echo '
<form name="form1" method="post" action="' . $_SERVER['PHP_SELF'] . '">
<p>
<label>
<input type="text" name="textfield" id="textfield">
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit">
</label>
</p>
</form>';
?>
Is it really a function call on the action attribute? or it should be on the onSUbmit attribute, because by convention action attribute needs a page/URL.
I think you should use AJAX with that,
There are plenty PHP AJAX Frameworks to choose from
http://www.phplivex.com/example/submitting-html-forms-with-ajax
http://www.xajax-project.org/en/docs-tutorials/learn-xajax-in-10-minutes/
ETC.
Or the classic way
http://www.w3schools.com/php/php_ajax_php.asp
I hope these links help
You can put the username() function in another page, and send the form to that page...

Categories