duanchai0028 2015-05-12 06:57
浏览 18
已采纳

表单值未提交

I have the following PHP Code

<form action="admin.php" method="post">

    <?php
    $conn=mysqli_connect("localhost","root","vravi","mrconnect");
    if(!$conn)
    {
        echo "error establishing the connection";
    }
    $sql="select UserID,UserEmail from Users order by UserID";
    $result=mysqli_query($conn,$sql);


    if(mysqli_num_rows($result)>0)
    {
        while($row=mysqli_fetch_assoc($result))
       {

          echo $row["UserID"];
          echo  $row["UserEmail"]." ".'<input type=\"text\" name=\"name[]\"/>'."</br>";     
        }
        echo '<button type=\"submit\" name=\"submit\"   value=\"submit\">'.Submit.'</button></br>';
    }
    ?>
</form>

which gives the following output in the browser.enter image description here

Now I have two questions.

1)When I try insert some values in one of the textboxes and click submit button in the bottom, The values are not getting submitted.

below is my admin.php php file.

<?php
if(isset($_POST['submit']))
{
    echo "submitted";
}
else
{
    echo "not submitted";
}


?>

2)Now the second question is that, I also want my email id to get submitted through the form, but it is just a normal text, can anyone tell me how do we do that?

  • 写回答

2条回答 默认 最新

  • douzhanglun4482 2015-05-12 07:12
    关注

    There are a few things that could change:

    1. You don't need to escape double quotes when they are between single quotes
    2. Use name="name['.$row["UserID"].']"
    3. Change the <button... to <input type="submit"...

    Your page will then look like this:

    <form action="admin.php" method="post">
        <?php
        $conn=mysqli_connect("localhost","root","vravi","mrconnect");
        if(!$conn) { echo "error establishing the connection"; }
    
        $sql="select UserID,UserEmail from Users order by UserID";
        $result=mysqli_query($conn,$sql);
    
        if(mysqli_num_rows($result)>0) {
            while($row=mysqli_fetch_assoc($result)) {
                echo $row["UserID"] . ' ' . $row["UserEmail"] ." ";
                echo '<input type="text" name="name['.$row["UserID"].']" />'."<br />";
            }
            echo '<button type="submit" name="submit" value="submit">Submit</button></br>';
        }
        ?>
    </form>
    

    Then, when the form is submitted to admin.php, you can loop the name.

    <?php
    if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
        foreach( $_POST['name'] as $data => $value ) {
            echo $data . ': ' . $value . '<br />';
            // will output    2:  12
        }
    }
    ?>
    

    In my example it will echo the user id and the submitted value. You could ofcourse also save it to a database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度