提交表单,但无法获取输入值

时间:2015-04-27 18:24:49

标签: php html forms materialize

我正面临代码问题

  1. 我使用materialize来创建这个网页,但是当输入类型提交运行PHP函数时,按钮类型提交没有做任何事情

  2. 填写输入并按下输入类型后,运行PHP代码并打印" PHP函数执行"这意味着我的变量设置为""而不是他们的输入值

  3. 请有人帮助我吗

       <!DOCTYPE HTML> 
       <html>
        <head>
            <title>Mobile Phone</title>
            <meta name="viewport" content="width=device-width, initil-scale=1.0">
            <link href="css/materialize.min.css" rel="stylesheet">
        </head>
        <body class="green lighten-5"> 
    
            <?php
                // define variables and set to empty values
                $MID = $MModel = $Color = $Price ="";
    
                if ($_SERVER["REQUEST_METHOD"] == "POST")
                {
                   $MID = test_input($_POST["MID"]);
                   $MModel = test_input($_POST["MModel"]);
                   $Color = test_input($_POST["Color"]);
                    $Price = test_input($_POST["Price"]);
                }
    
                function test_input($data)
                {
                   $data = trim($data);
                   $data = stripslashes($data);
                   $data = htmlspecialchars($data);
                   return $data;
                }
            ?>
    
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
            <script src="js/materialize.min.js"></script>
    
            <div class="container">   
              <div class="row">
                <form class="col s12" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                  <div class="row">
                    <div class="input-field col s2">
                      <input placeholder="A11" id="MID" type="text" pattern="[A-Za-z]{1}[0-9]{2}" class="validate">
                      <label for="MID">Mobile ID</label>
                    </div>
                    <div class="input-field col s2">
                      <input id="MModel" type="text" class="validate">
                      <label for="MModel">Mobile Model</label>
                    </div>
                    <div class="input-field col s2">
                      <input id="Color" type="text" class="validate">
                      <label for="Color">Color</label>
                    </div>
                    <div class="input-field col s2">
                      <input id="Price" type="number" class="validate" min="0" max="5000">
                      <label for="Price">Price</label>
                    </div>
                    </div>
    
                     <!-- This do not works -->
                    <button class="waves-effect waves-light btn" type="submit" name="action">Add
                        <i class="mdi-content-send right"></i>
                    </button>
    
                    <!-- This works -->
                    <input type="submit" name="submit" value="Submit"> 
                </form>
                          </div>
                        </div>
            <?php
           if (isset($_POST['submit']))
        {
               echo "PHP function is executed<br>";
               echo $MID;
               echo $MModel;
               echo $Color;
               echo $Price;
        }
            ?>        
    
        </body>
        </html>
    

1 个答案:

答案 0 :(得分:0)

变化

<button class="waves-effect waves-light btn" type="submit" name="action">Add
   <i class="mdi-content-send right"></i>
</button>

<button class="waves-effect waves-light btn" type="submit" name="submit">Add       
   <i class="mdi-content-send right"></i>
</button>