简体   繁体   中英

I'm trying to link my form to my PHP page, and I want to print out the user's input onto that page when he clicks the submit button

I've tried using the method GET in my form, and $_GET in my PHP page, but it doesn't link both of them together. I want to print out the elements in the form inputted by the user when the user clicks the submit button. Here is the code I used for my form:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MY Form</title>
</head>
<body>
    <form action="q4.php" method="get">

      <label><b>First Name:</b></label><br />
      <input type="text" name="first_name"><br />
      <label><b>Last Name:</b></label><br />
      <input type="text" name="second_name">

        <br>

        <label for="status">Status</label><br>
        <select name="status" id="status">
        <option value="undergraduate">UnderGraduate</option>
        <option value="postgraduate">Postgraduate</option>
        <option value="alumni">Alumni</option>
        </select>
        <br><br><br>

        <input type="radio" name="continuing">Continuing<br>
        <input type="radio" name="continuing">Not Continuing<br>

        <br><br>
        <label>Satisfaction Level <br></label>
        <label>0</label><input type="range" name="satisfaction" min="0" max="100"/><label>100</label>
        <br><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

Here's the code I used for my PHP:

<?php
$names = array(first_name => '', second_name => '');

reset($names);
while (list($key, $val) = each($names)) {
    echo "$key => $_GET\n";
}
?>

If you want to print out all results you could just do something like

<?php
foreach($_GET as $key => $value){
echo $key.": ".$value;
}
?>

But if you would rather only show specific data then perhaps something like this

<?php
$names = array(first_name => '$_GET['first_name']', second_name => 'second_name');

foreach($names as $name => $value){
echo $name.": ".$value;
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM