通过GET方法获取值后无法打印

时间:2016-03-02 11:15:07

标签: php

我试图通过GET方法获取两个文本框的值。但是当我尝试打印它们时。我无法做到。使用单个文本框时,代码可以正常工作。

<?php
if(isset($_GET['name']) && isset($_GET['id']))
{

$username = $_GET['name'];
$userid = $_GET['id'];
echo 'Hi '.$username.'<br>';
echo "Emp Id is ".$userid;
}
?>

<form action="contact-DB.php" method="GET">
Enter Employee Name : <input type = "text" name = "name"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Enter Employee ID : <input type = "text" name = "id">
<input type = "button" name="submit" value="Submit">

2 个答案:

答案 0 :(得分:1)

提交表单时,应该有一个提交按钮。在您的情况下,没有提交按钮。这就是表格从未提交的原因。

请尝试以下示例:

<!DOCTYPE html>
<html>
    <body>
        <?php
            if(isset($_GET['name']) && isset($_GET['id']))  {

                $username = $_GET['name'];
                $userid = $_GET['id'];
                echo 'Hi '.$username.'<br>';
                echo "Emp Id is ".$userid;
            }
        ?>
        <form action="contact-DB.php" method="GET">
            Enter Employee Name : 
            <input type = "text" name = "name">
            <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            Enter Employee ID : 
            <input type = "text" name = "id">
            <input type = "submit" name="submit" value="Submit">
        </form>
    </body>
</html>

在您的情况下,表单从未提交,因为

<input type = "button" name="submit" value="Submit">

答案 1 :(得分:0)

<input type="button" /> buttons will not submit a form - they don't do anything by default. They're generally used in conjunction with JavaScript as part of an AJAX application.

<input type="submit"> buttons will submit the form they are in when the user clicks on them, unless you specify otherwise with JavaScript.

所以使用这个

 <input type="submit"  value="Submit" name="submit">