如何通过按钮提交数据

时间:2018-09-10 21:16:36

标签: php html html5

<section>
    <h3>Actions</h3>
    <ul class="actions">
        <li><a href="#" class="button special">Special</a></li>
        <li><a href="#" class="button">Default</a></li>
    </ul>
    <ul class="actions">
        <li><a href="#" class="button big">Big</a></li>
        <li><a href="#" class="button">Default</a></li>
        <li><a href="#" class="button small">Small</a></li>
    </ul>
    <ul class="actions fit">
        <li><a href="#" class="button special fit">Fit</a></li>
        <li><a href="#" class="button fit">Fit</a></li>
    </ul>
    <ul class="actions fit small">
        <li><a href="#" class="button special fit small">Fit + Small</a></li>
        <li><a href="#" class="button fit small">Fit + Small</a></li>
    </ul>

大家好,基本上,我正在使用带有上述代码的模板来显示按钮。我想知道是否有人可以在有人按下按钮时向我发送按钮的值。

假设用户X按下表示123的Button,我只想知道按钮123被按下了。到目前为止,我只能想到制作表单并使用php邮件发送此表单。但是,如果可以选择发送我想知道的按钮值。

预先感谢

2 个答案:

答案 0 :(得分:-1)

您应该创建一个数据库来收集使用电子邮件发送的方法不是一个好选择,因为如果您使用该数据库将每次点击存储在一行中,那么根据网站的访问量,您还会收到大量电子邮件如此快地填满服务器,解决方案是像这样创建一个MySql表

CREATE TABLE `clicks_counter` (`button_id` BIGINT NOT NULL, `clicks_count` BIGINT NOT NULL) ENGINE = InnoDB;

然后使用您的脚本通过PHP和javascript与ajax一起存储点击次数来计算点击次数

您必须发送button_id并保持跟踪button_id,每次使用按钮单击一次,PHP都要使用clicks_count来更新clicks_count

答案 1 :(得分:-1)

基本的PHP表单处理。

body {
  background-color: #000;
  color: #FFF;
  overflow: hidden;
  text-align: center;
}
/* ignore the above (prettify output) */
<form action="file/to/post/to.php" method="POST">
  <label for="name">Name:</label>
  <input type"text" name='name' autocomplete="off" placeholder="Enter your Name" id="name" />
  <br /><br />
  <label for="Email">Email:</label>
  <input type"email" name='email' autocomplete="off" placeholder="Enter your@email.here" id="Email" />
  <br /><br />
  <button type="submit" id="submit-btn">Submit</button>
</form>

现在,在提交表单后处理表单时, 您可以执行以下操作:

<?php
    //data for the file you are posting to
    if (isset($_REQUEST['submit-btn'])) {
       $name = $_REQUEST['name'];
       $email = $_REQUEST['email'];  
    }

  // to print the data after it has been submitted you can do this
   if(isset($name) && isset($email)) {
      print "Your name is <b>". $name . "</b> and your email is <b>" . 
      $email . "</b>";
   }
?>

请注意:上面的示例数据只是一种后处理方法,您仍然需要清理数据(错误处理程序)

这应该使您对发布方式有所了解: https://www.w3schools.com/php/php_forms.asp