簡體   English   中英

表單值未提交

[英]Form Values not getting submitted

我有以下PHP代碼

<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>

在瀏覽器中提供以下輸出。 在此處輸入圖片說明

現在我有兩個問題。

1)當我嘗試在其中一個文本框中插入一些值並單擊底部的“提交”按鈕時,這些值未得到提交。

下面是我的admin.php php文件。

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


?>

2)現在第二個問題是,我也希望通過表單提交我的電子郵件ID,但這只是普通文本,誰能告訴我我們該怎么做?

有些事情可能會改變:

  1. 當雙引號位於單引號之間時,無需轉義
  2. 使用name="name['.$row["UserID"].']"
  3. <button...更改為 <input type="submit"...

您的頁面將如下所示:

<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>

然后,將表單提交到admin.php時,可以循環name

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

在我的示例中,它將回顯用戶ID和提交的值。 您當然也可以將其保存到數據庫中。

添加print_r(); 到腳本頂部,以查看要發布到頁面的內容。

更正您的input語法-它不完整:

echo  $row["UserEmail"]." ".'<input type=\"text\"

應該是這樣的:

echo  $row["UserEmail"]." ".'<input type=\"text\" name=\"userEmail".$row["UserID"]."\" />

甚至更好:

echo  '<label>'.$row["UserEmail"].'</label><input type="text" name="userEmail['.$row["UserID"].']" />';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM