【问题标题】:How to send data from Html button to php command如何将数据从 Html 按钮发送到 php 命令
【发布时间】:2019-11-06 23:40:50
【问题描述】:

I hope this is helpful now 当你问我数据库结构时,我创建了一个

感谢您的帮助!提前致谢!

我目前有以下html代码:

<html>
    <head>
      <title>Test Software</title>
    </head>
    <body>
      <label>
         Datum:<br/>
        <input name="date" type="date" placeholder="Datum" style="height: 50px; width: 100px;"/>
      </label>
      <label>
        <p> <form action="#"> </p>
		    <label>Station:<br>
			        <select name="top5" style="height: 50px; width: 100px;"/> 
				    <option>Choice 1 </option> 
			    	<option>Choice 2 </option> 
				    <option>Choice 3 </option>
				    <option>Choice 4 </option>
				    <option>Choice 5 </option>
			    </select>
			
		    </label>
	    </form>
   </main>
      <br/><br/><br/>
      <input type="submit" name="button[Button1]" value="Button 1" style="height: 50px; width: 100px;"/>
      <input type="submit" name="button[Button2]" value="Button 2" style="height: 50px; width: 100px;"/>
      <input type="submit" name="button[Button3]" value="Button 3" style="height: 50px; width: 100px;"/>
      <input type="submit" name="button[Button4]" value="Button 4" style="height: 50px; width: 100px;"/>
      <input type="submit" name="button[Button5]" value="Button 5" style="height: 50px; width: 100px;"/>
 
     </form>
  </body>
</html>

如何将数据发送给 Tom 一个 php 文件,然后将其存储在数据库中?

DB
CREATE TABLE button_counter (
id INTEGER PRIMARY KEY,
name VARCHAR(64) NOT NULL,
datum DATE NOT NULL,
savedate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
Button BIT NOT NULL DEFAULT 0,
Button2 BIT NOT NULL DEFAULT 0,
Button3 BIT NOT NULL DEFAULT 0,
Button4 BIT NOT NULL DEFAULT 0
Button5 BIT NOT NULL DEFAULT 0
);

【问题讨论】:

  • 您可以使用ajax将数据发送到php脚本。
  • 请用按钮显示完整代码。然后我会用相关名称给你答案。
  • @NipunTharuksha 我编辑了帖子并添加了完整代码
  • @Astra-Joe 让我有一个清晰的想法。用户是否可以多次单击相同的按钮,并且您需要将其计数存储在数据库中。我说的对吗?
  • @NipunTharuksha,是的,用户从选择标签中选择日期和选择,然后单击不同的按钮,这必须存储在数据库中。例如,如果用户在 button1 上单击 5 次,在 button2 上单击 3 次,则必须将其存储为当前日期和选择标签,直到选择新的选择标签或日期

标签: php html mysql sql web-applications


【解决方案1】:

请正确格式化您的 html 代码。 格式正确的代码在这里: 在每个按钮单击时添加 js 函数,并在这些函数中增加相关计数器并在另一个输入字段中设置。

	<html>
	   <head>
		  <title>Test Software</title>
	   </head>
	   <body>
		  <form action="#">
			 <label>
			 Datum:<br/>
			 </label>
			 <input name="date" type="date" placeholder="Datum" style="height: 50px; width: 100px;"/>
			 <label>Station:<br>
			 </label>
			 <select name="top5" style="height: 50px; width: 100px;"/>
				<option>Choice 1 </option>
				<option>Choice 2 </option>
				<option>Choice 3 </option>
				<option>Choice 4 </option>
				<option>Choice 5 </option>
			 </select>
			 <input type="submit" name="button[Button1]" value="Button 1" style="height: 50px; width: 100px;"/>
			 <input type="submit" name="button[Button2]" value="Button 2" style="height: 50px; width: 100px;"/>
			 <input type="submit" name="button[Button3]" value="Button 3" style="height: 50px; width: 100px;"/>
			 <input type="submit" name="button[Button4]" value="Button 4" style="height: 50px; width: 100px;"/>
			 <input type="submit" name="button[Button5]" value="Button 5" style="height: 50px; width: 100px;"/>
		  </form>
	   </body>
	</html>

【讨论】:

    【解决方案2】:

    对于简单的表单提交,您可以使用表单标签的属性。

    例如:&lt;form action="&lt;?=$_SERVER['PHP_SELF']; ?&gt;" method="POST" enctype= "multipart/form-data"&gt;

    $_SERVER['PHP_SELF'] 用于同页重定向,使用它的方法(POST & GET)。

    为了检查页面上可用的表单数据和在数据库中插入数据,这可能会对您有所帮助:

    if(!empty($_POST)){
    $data=$_POST['name']; 
    $insert="INSERT INTO table(name) VALUES('".$data."')";
    if ($conn->query($insert) === TRUE) {  echo "Successfully Inserted";
    }else{  echo "error insert";  }
    }
    

    【讨论】:

    • 是的,我知道@Dharman,但在这里他不是在谈论或询问任何安全问题。至少阅读问题并尝试帮助他人。
    【解决方案3】:

    我已将您的代码更改为如下所示并检查。它的工作。在您的问题中,我可以看到要填充当前时间戳的日期字段。但是有一个输入字段可以为用户选择日期。所以我修改了你的代码,假设用户会拍下日期。请参阅下面的代码,如果您得到所需的内容,请将其标记为答案。谢谢

    index.php

    <html>
    
    <head>
        <title>Test Software</title>
    
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script>
            var counter1 = 0;
            $(document).on('click', '#button01', function(e) {
                e.preventDefault();
                counter1++;
                $("#button01").text('You have clicked' + counter1);
                $("#hidde_val_of_button01").val(counter1);
            });
            var counter2 = 0;
            $(document).on('click', '#button02', function(e) {
                e.preventDefault();
                counter2++;
                $("#button02").text('You have clicked' + counter2);
                $("#hidde_val_of_button02").val(counter2);
            });
            var counter3 = 0;
            $(document).on('click', '#button03', function(e) {
                e.preventDefault();
                counter3++;
                $("#button03").text('You have clicked' + counter3);
                $("#hidde_val_of_button03").val(counter3);
            });
            var counter4 = 0;
            $(document).on('click', '#button04', function(e) {
                e.preventDefault();
                counter4++;
                $("#button04").text('You have clicked' + counter4);
                $("#hidde_val_of_button04").val(counter4);
            });
            var counter5 = 0;
            $(document).on('click', '#button05', function(e) {
                e.preventDefault();
                counter5++;
                $("#button05").text('You have clicked' + counter5);
                $("#hidde_val_of_button05").val(counter5);
            });
        </script>
    </head>
    
    <body>
        <form action="action.php" method="post">
            <label>
                Datum:
                <br/>
                <input name="date" type="date" placeholder="Datum" style="height: 50px; width: 100px;" />
            </label>
            <label>
                <p> </p>
                <label>Station:
                    <br>
                    <select name="top5" style="height: 50px; width: 100px;" />
                    <option>Choice 1 </option>
                    <option>Choice 2 </option>
                    <option>Choice 3 </option>
                    <option>Choice 4 </option>
                    <option>Choice 5 </option>
                    </select>
    
                </label>
    
                </main>
                <br/>
                <br/>
                <br/>
                <button id="button01" style="height: 50px; width: 100px;">Button 1</button>
                <!--Using hiddne text input typeto pass the data!-->
                <input type="text" hidden value="0" id="hidde_val_of_button01" name="button01" />
    
                <button id="button02" style="height: 50px; width: 100px;">Button 2</button>
                <!--Using hiddne text input typeto pass the data!-->
                <input type="text" hidden value="0" id="hidde_val_of_button02" name="button02" />
    
                <button id="button03" style="height: 50px; width: 100px;">Button 3</button>
                <!--Using hiddne text input typeto pass the data!-->
                <input type="text" hidden value="" id="hidde_val_of_button03" name="button03" />
    
                <button id="button04" style="height: 50px; width: 100px;">Button 4</button>
                <!--Using hiddne text input typeto pass the data!-->
                <input type="text" hidden value="" id="hidde_val_of_button04" name="button04" />
    
                <button id="button05" style="height: 50px; width: 100px;">Button 5</button>
                <!--Using hiddne text input typeto pass the data!-->
                <input type="text" hidden value="" id="hidde_val_of_button05" name="button05" />
                <br/>
                <br/>
                <button type="submit" style="height: 50px; width: 100px;background-color:green;">Submit</button>
                  <br/>
                     <textarea name="comment" >Enter your comment here...</textarea>
        </form>
    </body>
    
    </html>
    

    action.php

    <?php
    
    //FETCH DATA
    $date = $_POST['date'];
    $option = $_POST['top5'];
    $btn01 = $_POST['button01'];
    $btn02 = $_POST['button02'];
    $btn03 = $_POST['button03'];
    $btn04 = $_POST['button04'];
    $btn05 = $_POST['button05'];
    $comment = $_POST['comment'];
    
    // echo $date."<br/>";
    // echo $option."<br/>" ;
    // echo $btn01."<br/>" ;
    // echo $btn02."<br/>" ;
    // echo $btn03."<br/>" ;
    // echo $btn04."<br/>" ;
    // echo  $btn05."<br/>";
    
    $sql = "INSERT INTO your_table_name(Date,Choice,Button_01,Button_02,Button_03,Button_04,Button_05,comment) VALUES ($date,$option,$btn01,$btn02,$btn03,$btn04,$btn05,$comment) ";
    
    ?>
    

    【讨论】:

    • 不要按原样使用此代码。如果这个 SQL 曾经被执行过,它很容易受到 SQL 注入的攻击,并且可能会破坏您的应用程序。如果您要访问数据库,请使用准备好的语句。
    • @Dharman 我刚刚给了他一个想法。他应该知道如何使用SQL
    • @NipunTharuksha 是否可以在页面上打印出每个选项单击了多少个按钮?例如对于Choice 1,Date xx/xx/xxxx,button1 (6x),button2 (9x) ... 我想显示每个选择和日期每个按钮被点击的次数,用于统计
    • @Astra-Joe 我不清楚你在问什么。你的意思是在提交之前你想显示该按钮被点击的次数
    • @NipunTharuksha 我提到我想在我的页面上显示存储在我的数据库中的数据。现在我正在尝试创建一个数据库,然后我想在页面上显示存储的数据。也许我应该为此开始一个新问题? echo " 日期 选择"; echo "Button1 Button2"; echo "Button3 Button4"; echo "Button5 ";
    【解决方案4】:

    @astra-joe 这篇文章是为了展示如何从 DB 表中获取数据以及如何显示它们。

    这是我为测试创建的数据库表

    在这里,我要做的是第一次创建数据库凭据,然后通过这些凭据创建连接。

    然后检查连接,如果连接失败显示错误($conn-&gt;connect_error)

    然后我创建引导表并首先定义表头。

    现在创建一个sql 语句来从数据库表中获取数据

    然后查询带有连接$result的sql语句

    现在检查输出结果if ($result-&gt;num_rows &gt; 0)的行数

    如果$result&gt;0表示从db中获取数据成功。这样每个数据都会在带有引导表的while循环中循环

    如果$result&lt;0 这意味着没有数据被提取,所以输出将是

    没有要获取的数据。

    检查此代码

    <!DOCTYPE html>
    <html>
       <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Page Title</title>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
             integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
             crossorigin="anonymous">
       </head>
       <body>
          <?php
             $servername ="localhost";//these credititlas to be updated according to your DB
             $username ="root";//these credititlas to be updated according to your DB
             $password ="";//these credititlas to be updated according to your DB
             $dbname ="astrajoy";//these credititlas to be updated according to your DB
             // Create connection
             $conn =new mysqli($servername, $username, $password, $dbname);
    
             // Check connection
             if ($conn->connect_error) {
                     die("Connection failed: ". $conn->connect_error);
             }
             ?>
          <!-- Bootstrap table header section. This section shouldnt be repeated. So that I have put it before while loop. -->
          <table
             class="table table-bordered table-dark">
             <thead>
                <tr>
                   <th scope="col">Index</th>
                   <th scope="col">Date</th>
                   <th scope="col">Choice</th>
                   <th scope="col">Button_01</th>
                   <th scope="col">Button_02</th>
                   <th scope="col">Button_03</th>
                   <th scope="col">Button_04</th>
                   <th scope="col">Button_05</th>
                   <th scope="col">Comment</th>
                </tr>
             </thead>
             <tbody>
                <?php
                   $sql ="SELECT * FROM user";// * mean all in SQL
    
                   //In here query will be excuted with db connetion
                   $result = $conn->query($sql);
                   // to have a unique ID for a row
                   $index =0; 
                   //check the result
                   if ($result->num_rows>0) {
                   // output data of each row
                   while($row = $result->fetch_assoc()) {
                   $index++;
                   ?>
                <!-- Data display will be started from here -->
                <tr>
                   <th scope="row"><?=$index;?></th>
                   <td><?=$row['Date'];?></td>
                   <td><?=$row['Choice'];?></td>
                   <td><?=$row['Button_01'];?></td>
                   <td><?=$row['Button_02'];?></td>
                   <td><?=$row['Button_03'];?></td>
                   <td><?=$row['Button_04'];?></td>
                   <td><?=$row['Button_05'];?></td>
                   <td><?=$row['comment'];?></td>
                </tr>
                <?php
                   }
                   } else {
                   $conn->close();
                   ?>
                <tr>
                   <th
                      scope="row"></th>
                   <td
                      colspan="7">No data available</td>
                </tr>
                <?php
                   }
                   ?>
             </tbody>
          </table>
       </body>
    </html>
    

    请务必使用名为 comment

    的字段名称更新 db 表

    这是最终结果

    【讨论】:

    • 能否分享一下数据库的SQL文件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2019-03-11
    • 2014-07-02
    • 1970-01-01
    相关资源
    最近更新 更多