Submission of php form form

php forms inserting forms into Web pages

Insert the form in the normal WEB page as follows: a relatively complete form will be created here to show all the elements and attributes in < form >.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
    <form action="test.php" method="post" name="form1" enctype="multipart/form-data">
        <table width="400" border="1" cellpadding="1" bgcolor="#999">
            <tr bgcolor="#ffcc33">
                <td width="103" height="25" align="right">username: </td>
                <td height="25"><input type="text" name="user" size="20" maxlength="100"></td>
            </tr>
            <tr bgcolor="#ffcc33">
                <td height="25" align="right">gender: </td>
                <td height="25" colspan="2">
                    <input type="radio" name="gender" value="0" checked>male
                    <input type="radio" name="gender" value="1">female    
                </td>
            </tr>
            <tr bgcolor="#ffcc33">
                <td width="103" height="25" align="right">password: </td>
                <td width="289" height="25" colspan="2"><input type="password" name="pwd" size="20" maxlength="100"></td>
            </tr>
            <tr bgcolor="#ffcc33">
                <td height="25" align="right">schooling: </td>
                <td height="25" colspan="2">
                    <select name="select" >
                        <option value="0">Specialized subject</option>
                        <option value="1">Undergraduate course</option>
                        <option value="2" selected>A graduate student</option>
                    </select>
                </td>
            </tr>
            <tr bgcolor="#ffcc33">
                <td height="25" align="right">hobby: </td>
                <td height="25" colspan="2">
                    <input type="checkbox" name="hobby[]" value="0" checked>php
                    <input type="checkbox" name="hobby[]" value="1">thinkphp    
                    <input type="checkbox" name="hobby[]" value="2" checked>laravel
                </td>
            </tr>
            <tr bgcolor="#ffcc33">
                <td height="25" align="right">pic: </td>
                <td height="25" colspan="2">
                    <input type="file" name="image" size="20" maxlength="100">
                </td>
            </tr>
            <tr bgcolor="#ffcc33">
                <td height="25" align="right">info: </td>
                <td height="25" colspan="2">
                    <textarea name="info" cols="30" rows="10"></textarea>
                </td>
            </tr>
            <tr bgcolor="#ffcc33">
                <td height="25" align="right">info: </td>
                <td height="25" colspan="2">
                    <input type="submit" value="Submit">
                    <input type="reset" value="Reset">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

Enter the address in the browser and press enter. The operation result is as follows:

 

 

 

POST() method and GET() method of submitting form data in php

Submit the form using the POST() method

When using the POST() method, you only need to set the attribute method in the < form > form to POST. The POST() method is not URL dependent and does not appear in the address bar. The POST() method can deliver data to the server without restriction. All submitted information is transmitted in the background. Users cannot see this process in the browser side, security Sex will be higher. So the POST() method is more suitable for sending a confidential (such as bank account number) or large capacity data to the server.

The following example uses the POST() method to send text box information to the server. The example code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
    <form action="test.php" method="post" name="form1">
        <table width="300" border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td height="30">
                    order number:
                    <input type="text" name="order" size="20">
                    <input type="submit" value="Submit">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

Submit the form using the GET() method

Note: to submit a form using the GET() method, the URL should be limited to 1MB characters in length. If the amount of data sent is too large, the data will be truncated, resulting in unexpected or failed processing results.

Next, create a form to apply the get () method to submit the user name and password and display them in the URL address bar. Add a text box named user; add a password field named pwd; set the method property of the form to GET() method. The example code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
    <form action="test.php" method="get" name="form1">
        <table width="300" border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td height="30">
                    user name:<input type="text" name="user" size="20"><br>
                    password:<input type="password" name="pwd" size="20"><br>
                    <input type="submit" value="Submit">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

Run this instance, enter the user name and password in the text box, and click Submit. The information in the text box will be displayed in the URL address bar, as shown in the following figure:

 

 

 

The get () method exposes the parameters to the address bar. If the parameters passed by the user are non confidential (such as id=8), it is feasible to use GET() method to pass the data; if the parameters passed by the user are confidential (such as password), it is not safe to use this method to pass the data. The solution to this problem is to replace the default get () method of the method attribute in the form with the POST() method.

 

php determines whether the form form is submitted for details

When we submit a form through submit, we usually care whether a large amount of data filled in the form is submitted to the background. Here we need to make a judgment. php code is used to determine whether the form data is submitted in the following form:

<html>
<body>
    <form action="" method="post" name="form1">
        <table width="300" border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td height="30">
                    user name:<input type="text" name="user" size="20"><br>
                    password:<input type="password" name="pwd" size="20"><br>
                    <input type="submit" name="submit" value="Submit">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

<?php
if(isset($_POST['submit'])){
    echo 'Data submitted';
}

Note: isset -- check whether the variable is set. The return value has the following forms.

FALSE if variable does not exist

FALSE if variable exists and its value is NULL

If the variable exists and the value is not NULL, return true

When multiple variables are checked at the same time, only when each single item meets the previous requirement can TRUE be returned, otherwise the result is FALSE.

 

In data query, GET mode is recommended, while in data addition, modification or deletion, POST mode is recommended.
request is to read get first and then post, and exists at the same time, that is, to overwrite the previous variables.

 

php prevent form from submitting repeatedly

When a user submits a form, the same record may be repeatedly inserted into the database because of the speed of the network or the malicious refresh of the web page. Therefore, a limitation that cannot be ignored is to prevent the user from submitting the form repeatedly, because it is possible that the user continuously clicks the submit button or the attacker submits the data maliciously. Then we can handle such as the number of modifications or additions after submitting the data You get into trouble when you get to the database.

 

First, limit from the front.

The front-end JavaScript is disabled after the button is clicked once, that is, disabled. This method simply prevents multiple clicks of the submit button, but the disadvantage is that if the user disables the JavaScript script, it will fail.

As mentioned in the following example, we use it to deal with the repeated submission of forms. Please see the following code:

<html>
<body>
    <form action="" method="post" name="register" enctype="multipart/form-data">
        <input type="text" name="text" id="text"><br>
        <input name="cont" value="Submit" type="button" onclick="document.register.cont.value='Submitting, please wait...';document.register.cont.disabled=true;document.register.submit();">
    </form>
</body>
</html>

When the user clicks the submit button, the button changes to gray unavailable

There is another method, which also uses the function of JavaScript, but the OnSubmit() method is used. If the form has been submitted once, a dialog box will pop up immediately. The code is as follows:

<html>
<body>
    <form action="" method="post" name="register" onSubmit="return submitOnce(this)">
        <input type="text" name="text" id="text"><br>
        <input name="cont" value="Submit" type="submit">
    </form>
</body>
</html>

<script>
    var count = 0;
    function submitOnce(form){
        if(count == 0){
            count++;
            return true;
        }else{
            alert('Do not submit the form again!');
            return false;
        }
    }
</script>

 

Second, Cookie processing

Use cookies to record the status of the form submission, and check whether the form has been submitted according to its status. Please refer to the following code:

<html>
<body>
    <form action="" method="post" name="register" enctype="multipart/form-data">
        username: <input type="text" name="text" id="text"><br>
        <input name="submit" value="Submit" type="submit">
    </form>
</body>
</html>

<?php
if(isset($_post['submit'])){
    setcookie('tmpcookie','',time()+30);
    header('Location:'.$_SERVER[PHP_SELF]);
    exit;
}
if(isset($_COOKIE['tmpcookie'])){
    setcookie('tmpcookie','',0);
    echo 'Do not submit the form again';
}

Note that this method will not work if cookies are prohibited by the client.

Third, use Session processing

By using the session function of PHP, you can also avoid repeatedly submitting forms. Session is saved on the server side. The session variable can be changed during PHP operation. The next time you access this variable, you get the newly assigned value. Therefore, you can use a session variable to record the submitted value of the form. If it does not match, it is considered that the user is repeatedly submitting. Please refer to the following code:

<?php
session_start();
$code = mt_rand(0,1000000);
$_SESSION['code'] = $code;
?>
<html>
<body>
    <form action="deal.php" method="post" name="register" enctype="multipart/form-data">
        username: <input type="text" name="text" id="text"><br>
        <input type="hidden" name="code" value="<?=$code?>">
        <input name="submit" value="Submit" type="submit">
    </form>
</body>
</html>

The PHP code on the receiving page is as follows:

<?php
session_start();
if(isset($_POST['code'])){
    if($_POST['code'] == $_SESSION['code']){

    }else{
        echo 'Please do not submit the form again';
    }
}
?>

Fourth, use the header function to turn

In addition to the above method, there is a simpler method, that is, when the user submits the form, the server will immediately turn to other pages after processing. The code is as follows.

<html>
<body>
    <form action="deal.php" method="post" name="register" enctype="multipart/form-data">
        username: <input type="text" name="text" id="text"><br>
        <input name="submit" value="Submit" type="submit">
    </form>
</body>
</html>

deal.php

<?php
if(isset($_POST['submit'])){
    //Business processing...
    
    header('location:success.php');
}
?>

success.php

<?php
echo 'Submitted successfully';

Tags: PHP Session Javascript Attribute

Posted on Fri, 22 May 2020 01:05:11 -0700 by roba59