How get data from form submit in php? Chi tiết

How get data from form submit in php? Chi tiết

Kinh Nghiệm về How get data from form submit in php? Chi Tiết


Quý khách đang tìm kiếm từ khóa How get data from form submit in php? được Update vào lúc : 2022-10-01 19:20:27 . Với phương châm chia sẻ Mẹo Hướng dẫn trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi tìm hiểu thêm Post vẫn ko hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Ad lý giải và hướng dẫn lại nha.


The PHP superglobals $_GET and $_POST are used to collect form-data.


Nội dung chính


  • PHP – A Simple HTML Form

  • GET vs. POST

  • When to use GET?

  • When to use POST?

  • PHP Exercises

  • Test Yourself With Exercises

  • PHP Form Handling

  • Creating a Simple Contact Form

  • Explanation of code

  • Capturing Form Data with PHP

  • How can I get submitted form value in PHP?

  • How will you retrieve values submitted by this form?

  • Can PHP collect form data?

  • How get data from form and save it to database in PHP?

PHP – A Simple HTML Form


The example below displays a simple HTML form with two input fields and a submit button:



Example



<html>
<body toàn thân>


<form action=”welcome.php” method=”post”>
Name: <input type=”text” name=”name”><br>
E-mail: <input type=”text” name=”email”><br>
<input type=”submit”>

</form>


</body toàn thân>
</html>


Run Example »


When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named “welcome.php”. The form data is sent with the HTTP POST method.


To display the submitted data you could simply echo all the variables. The “welcome.php” looks like this:



<html>

<body toàn thân>


Welcome <?php echo $_POST[“name”]; ?><br>
Your email address is: <?php echo $_POST[“email”]; ?>


</body toàn thân>
</html>


The output could be something like this:



Welcome John
Your email address is


The same result could also be achieved using the HTTP GET method:



Example



<html>
<body toàn thân>


<form action=”welcome_get.php”

method=”get”>

Name: <input type=”text” name=”name”><br>
E-mail: <input type=”text” name=”email”><br>
<input type=”submit”>
</form>


</body toàn thân>
</html>


Run Example »


and “welcome_get.php” looks like this:



<html>
<body toàn thân>


Welcome <?php echo $_GET[“name”]; ?><br>
Your email address

is: <?php echo $_GET[“email”]; ?>


</body toàn thân>
</html>


The code above is quite simple. However, the most important thing is missing. You need to validate form data to protect your script from malicious code.



Think SECURITY when processing PHP forms!


This page does not contain any form validation, it just shows how you can send and retrieve form data.


However, the next pages will show how to process PHP forms with

security in mind! Proper validation of form data is important to protect your form from hackers and spammers!


GET vs. POST


Both GET and POST create an array (e.g. array( key1 => value1, key2 => value2, key3 => value3, …)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.


Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always

accessible, regardless of scope – and you can access them from any function, class or file without having to do anything special.


$_GET is an array of variables passed to the current script via the URL parameters.


$_POST is an array of variables passed to the current script via the HTTP POST method.


When to use GET?


Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET

also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.


GET may be used for sending non-sensitive data.


Note: GET should NEVER be used for sending passwords or other sensitive information!


When to use POST?


Information sent from a form with the POST method is invisible to others

(all names/values are embedded within the body toàn thân of the HTTP request) and has no limits on the amount of information to send.


Moreover POST supports advanced functionality such as tư vấn for multi-part binary input while uploading files to server.


However, because the variables are not displayed in the URL, it is not possible to bookmark the page.


Developers prefer POST for sending form data.


Next, lets see how we can process PHP forms the

secure way!


PHP Exercises


Test Yourself With Exercises



Exercise:


If the form in the white section below gets submitted, how can you, in welcome.php, output the value from the “first name” field?



<form action=”welcome.php” method=”get”>

First name: <input type=”text” name=”fname”>

</form>


<html>

<body toàn thân>

Welcome <?php echo ; ?>

</body toàn thân>

</html>



PHP Form Handling


In this tutorial you’ll learn how to collect user inputs submitted through a form using the PHP superglobal variables $_GET, $_POST and $_REQUEST.


Creating a Simple Contact Form


In this tutorial we are going to create a simple HMTL contact form that allows users to enter their comment and feedback then displays it to the browser using PHP.


Open up your favorite code editor and create a new PHP file. Now type the following

code and save this file as “contact-form.php” in the root directory of your project.



<!DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8″>

<title>Contact Form</title>

</head>

<body toàn thân>

<h2>Contact Us</h2>

<p.>Please fill in this form and send us.</p.>

<form action=”process-form.php” method=”post”>

<p.>

<label for=”inputName”>Name:<sup>*</sup></label>

<input type=”text” name=”name” id=”inputName”>

</p.>

<p.>

<label for=”inputEmail”>E-Mail:<sup>*</sup></label>

<input type=”text” name=”email” id=”inputEmail”>

</p.>

<p.>

<label for=”inputSubject”>Subject:</label>

<input type=”text” name=”subject” id=”inputSubject”>

</p.>

<p.>

<label for=”inputComment”>Message:<sup>*</sup></label>

<textarea name=”message” id=”inputComment” rows=”5″ cols=”30″></textarea>

</p.>

<input type=”submit” value=”Submit”>

<input type=”reset” value=”Reset”>

</form>

</body toàn thân>

</html>


Explanation of code


Notice that there are two attributes within the opening <form> tag:


  • The action attribute references a PHP file “process-form.php” that receives the data entered into the form when user submit it by pressing the submit button.

  • The method attribute tells the browser to send the form data through

    POST method.

Rest of the elements inside the form are basic form controls to receive user inputs. To learn more about HTML form elements please check out the HTML Forms tutorial.


Capturing Form Data with PHP


To access the value of a particular form field, you can use

the following superglobal variables. These variables are available in all scopes throughout a script.



SuperglobalDescription$_GET

Contains a list of all the field names and values sent by a form using the get method (i.e. via the URL parameters).

$_POST

Contains a list of all the field names and values sent by a form using the post method (data will not visible in the URL).

$_REQUEST

Contains the values of both the $_GET and $_POST variables as well as the values of the $_COOKIE superglobal variable.


When a user submit the above contact form through clicking the submit button, the form data is sent to the “process-form.php” file on the server for processing. It simply captures the information submitted by the user and displays it to browser.


The PHP code of “process-form.php” file will look something like this:



<!DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8″>

<title>Contact Form</title>

</head>

<body toàn thân>

<h2>Thank You</h2>

<p.>Here is the information you have submitted:</p.>

<ol>

<li><em>Name:</em> <?php echo $_POST[“name”]?></li>

<li><em>E-Mail:</em> <?php echo $_POST[“email”]?></li>

<li><em>Subject:</em> <?php echo $_POST[“subject”]?></li>

<li><em>Message:</em> <?php echo $_POST[“message”]?></li>

</ol>

</body toàn thân>

</html>


The PHP code above is quite simple. Since the form data is sent through the post method, you can retrieve the value of

a particular form field by passing its name to the $_POST superglobal array, and displays each field value using echo() statement.


In real world you cannot trust the user inputs; you must implement some sort of validation to filter the user inputs before using them. In the next chapter you will learn how sanitize and validate this contact form data and send it through the email using PHP.


How can I get submitted form value in PHP?


Use isset() method in PHP to test the form is submitted successfully or not. In the code, use isset() function to check $_POST[‘submit’] method.


How will you retrieve values submitted by this form?


Since the form data is sent through the post method, you can retrieve the value of a particular form field by passing its name to the $_POST superglobal array, and displays each field value using echo() statement.


Can PHP collect form data?


The PHP superglobals $_GET and $_POST are used to collect form-data.


How get data from form and save it to database in PHP?


php Save both files in one thư mục under htdocs.. Start XAMPP Server.. Open localhost/phpmyadmin in your web browser.. Create database of name staff and table of name college.. Write HTML and PHP code in your Notepad in a particular thư mục.. Submit data through HTML Form.. Verify the results..

Tải thêm tài liệu liên quan đến nội dung bài viết How get data from form submit in php?


programming

php

PHP Form submit

PHP form

PHP input form


How get data from form submit in php?Reply
How get data from form submit in php?5
How get data from form submit in php?0
How get data from form submit in php? Chia sẻ


Share Link Cập nhật How get data from form submit in php? miễn phí


Bạn vừa Read nội dung bài viết Với Một số hướng dẫn một cách rõ ràng hơn về Video How get data from form submit in php? tiên tiến và phát triển nhất Share Link Cập nhật How get data from form submit in php? Free.



Giải đáp vướng mắc về How get data from form submit in php?


Nếu sau khi đọc nội dung bài viết How get data from form submit in php? vẫn chưa hiểu thì hoàn toàn có thể lại phản hồi ở cuối bài để Admin lý giải và hướng dẫn lại nha

#data #form #submit #php

Related posts:

Post a Comment

Previous Post Next Post

Discuss

×Close