P粉8632950572023-08-23 20:54:49
PHP cannot access your DOM directly. PHP only runs on the server and simply receives requests and gives responses.
After submitting this form to its action page ./form.php
, the values entered into the form will be stored in $_POST
with the key name of its name
Attributes. In your HTML code, add the name
attribute to the input tag like this:
<form action="./form.php" method="post"> <div name="name"><input type="text" name="name"></div> <div name="surname"><input type="text" name="surname"></div> </form>
Now, if I submit this form and enter Zachary in the name
input tag and Taylor in the surname
input tag, I can get those values like this:
In the ./form.php
file:
$name = $_POST['name'];
// "Zachary"
$surname = $_POST['surname'];
// "Taylor"
To verify that there was any input first, use: isset($_POST['key'])
, because sometimes the input value is not even sent to the action page when it is null. This prevents PHP from throwing an error when referencing a non-existent $_POST key.
P粉1639513362023-08-23 11:56:52
To get the posted data from the submitted form, you can use $_POST['fieldname']
to achieve this.
Just try the following and see what you get after the form is submitted.
//echo "<pre>"; //print_r($_POST);
Uncomment the two lines above, see what you get, and then comment them out again.
if( isset($_POST['name']) ) { $name = $_POST['name']; } if( isset($_POST['surname']) ) { $surname = $_POST['surname']; } if( isset($_POST['subject']) ) { $subject = $_POST['subject']; }