Search code examples
phphtmlforms

How do I import the data from a html 5 form into php, and in some way make it visible to ensure that I didn't make any mistakes?


How do I import the data from a html 5 form into php, and in some way make it visible to ensure that I didn't make any mistakes?

Here is what I currently have:

HTML

<form action="Input.php" method="post">
Zipcode: <input type="text" maxlength="5" name="Zipcode" onkeypress="return isNumberKey(event)" ><br>
<br>
<br>
Age: <input type="text" maxlength="3" name="Age" onkeypress="return isNumberKey(event)" ><br>
<br>
<br>
<input type="radio" name="sex" value="male" checked>Male
<br>
<input type="radio" name="sex" value="female">Female
<br>
<br>
<input type="radio" name="smoke" value="yes" checked>Yes, I use tobacco products.
<br>
<input type="radio" name="smoke" value="no">No, I do not use tobacco products.
<br>
<br>
<input type="submit" name="submit" value="Submit"> 

PHP

<?php
echo htmlspecialchars($_POST["Zipcode"])
echo htmlspecialchars($_POST["Current Age"])
echo htmlspecialchars($_POST["sex"])
echo htmlspecialchars($_POST["smoke"])

$zipcode = test_input($_POST["Zipcode"]);
$sex = test_input($_POST["sex"]);
$smoke = test_input($_POST["smoke"]);
$age = test_input($_POST["Current Age"]);


print <h2>Your Input:</h2>
print $Zipcode; <br> $age; <br> $smoke; <br> $sex;
?>

Solution

  • <form action="Input.php" method="post">
    Zipcode: <input type="text" maxlength="5" name="Zipcode" onkeypress="return isNumberKey(event)" ><br>
    <br>
    <br>
    Age: <input type="text" maxlength="3" name="Age" onkeypress="return isNumberKey(event)" ><br>
    <br>
    <br>
    <input type="radio" name="sex" value="male" checked>Male
    <br>
    <input type="radio" name="sex" value="female">Female
    <br>
    <br>
    <input type="radio" name="smoke" value="yes" checked>Yes, I use tobacco products.
    <br>
    <input type="radio" name="smoke" value="no">No, I do not use tobacco products.
    <br>
    <br>
    <input type="submit" name="submit" value="Submit">
    </form>
    
    <?PHP
    $zipcode = $_POST["Zipcode"];
    $sex = $_POST["sex"];
    $smoke = $_POST["smoke"];
    $age = $_POST["Age"];
    
    
    echo '<h2>Your Input:</h2>';
    echo $Zipcode . '<br>' . $age . '<br>' . $smoke . '<br>' $sex;
    ?>