Get input

Sep 3, 2020 PHP

Output side

Set the destination php file in action of form attribute of html on the output side.

<form action = "submit.php" method = "get">
        <label for = "my_name"> Name: </ label>
        <input type = "text" id = "my_name"
               name = "my_name" maxlength = "255"
               value = "">
        <input type = "submit" value = "submit">
    </ form>

Recipient

The contents of $ _REQUEST can be obtained by entering the value given in the name attribute.

By using htmlspecialchars, the character string recognized as an html tag can be escaped.

Since ENT_QUOTES is a numerical value, "” is not added.

<? php
        print (htmlspecialchars
        ($ _REQUEST ['my_name'], ENT_QUOTES));
        ?>

Other than get method

Specify method = post This way ** the URL doesn’t change **

<form action = "submit.php" method = "post">
        <label for = "my_name"> Name: </ label>
        <input type = "text" id = "my_name"
               name = "my_name" maxlength = "255"
               value = "">
        <input type = "submit" value = "submit">
    </ form>

submit.php uses $ _POST

If you use $ _REQUEST, you can receive both get and post for general purposes, but be aware that operations that you may not see may occur.

<? php
        print (htmlspecialchars
        ($ _POST ['my_name'], ENT_QUOTES));
        ?>

###Radio button What is sent to the php side is the value set in the value attribute, In the following cases, the value sent even if male is selected is male set in value

<form action = "submit.php" method = "post">
        <p> Gender:
            <input type = "radio" name = "gender" value = "male"> Male
            /
            <input type = "radio" name = "gender" value = "female"> Female
        </ p>
            <input type = "submit" value = "submit">
    </ form>

Get multiple values

Make it possible to receive multiple selections by adding [] to the name attribute

<form action = "submit.php" method = "post">
        <p>
            <input type = "checkbox" name = "reserve []" value = "1/1"> January 1st <br>
            <input type = "checkbox" name = "reserve []" value = "1/2"> January 2 <br>
            <input type = "checkbox" name = "reserve []" value = "1/3"> January 3 <br>

        </ p>
            <input type = "submit" value = "submit">
    </ form>

Use the foreach / for syntax because the value of checkbox is ** output in the form of an array **

<? php
        foreach ($ _POST ['reserve'] as $ reserve) {
            print (htmlspecialchars ($ reserve, ENT_QUOTES).',');
        }
        ?>