简体   繁体   English

如何从Java脚本回复按钮获取值到数据库?

[英]How to get value from java script reply button into database?

Below is the Java Script code to display a text box on clicking a Reply button. 下面是Java脚本代码,用于在单击“答复”按钮时显示一个文本框。

    while($row = mysqli_fetch_array( $results )) { 
    echo  "<div class='cmt'>
   <div class='cmttext'></div>
   <button class='replybtn'>Reply</button>
   <div class='replyform'></div>
   </div> ";        
   } 

and java script 和Java脚本

<script type="text/javascript">
var varHtml = "<form method='post' action=''><textarea name='reply'></textarea>  <input type='button' value='Cancel' onclick='history.go(0)' />  <input type='submit' /> </form>";
var allElements = document.body.getElementsByClassName("replybtn");
var addCommentField = function() {
  for (var i = 0; allElements.length > i; i++) {
    if (allElements[i] === this) {
      console.log("this " + i);
      if (document.getElementsByClassName("replyform")[i].innerHTML.length === 0) {
        document.getElementsByClassName("replyform")[i].innerHTML = varHtml;
      }
    }
  }
};

for (var i = 0; i < allElements.length; i++) {
  allElements[i].addEventListener('click', addCommentField, false);
}    
</script>

How can I get the values from text box and store it into database? 如何从文本框中获取值并将其存储到数据库中?

Assuming you want to use Ajax, you'll need to attach a click handler to the button, which will grab the relevant values and send them to a server-side script (eg PHP) which can then insert them into the db. 假设您要使用Ajax,则需要在按钮上附加一个单击处理程序,该处理程序将获取相关值并将其发送到服务器端脚本(例如PHP),然后可以将其插入数据库。

(If you don't want to use Ajax, just use a form and submit it like normal). (如果您不想使用Ajax,只需使用表单并像平常一样提交即可)。

Here's a generic example (using jQuery) which does that. 这是一个使用jQuery的通用示例。 You can adapt it to your needs: 您可以根据需要进行调整:

index.html 的index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Ajax example</title>
  </head>
  <body>
    <form action="">
      <input type="text" id="myTextField" />
      <button>Submit</button>
    </form>

    <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
    <script>
      function submitValuesToDatabase(values){
        $.ajax({
          url: '/path/to/submit.php',
          type: 'POST',
          data: values
        })
        .done(function(res) {
          console.log("success");
          console.log("The server replied: " + res);
        })
        .fail(function(err) {
          console.log("Something went wrong!");
          console.log(err);
        })
        .always(function() {
          console.log("complete");
        });
      }

      $("button").on("click", function(e){
        e.preventDefault();
        var values = {
          val1: $("#myTextField").val()
        }
        submitValuesToDatabase(values);
      });
    </script>
  </body>
</html>

submit.php submit.php

<?php
// Retrieve values from $_POST
// And insert them into database

print_r($_POST);

If you run this demo (on a server), you will see output resembling the following in the console: 如果在服务器上运行此演示,则将在控制台中看到类似于以下内容的输出:

success
The server replied: Array
(
    [val1] => rtzr
)

complete

So, in this case to get a reference to val1 in the PHP script you would do: 因此,在这种情况下,要在PHP脚本中获取对val1的引用,您可以执行以下操作:

$val1 = filter_var($_POST['val1'], FILTER_SANITIZE_STRING);

Let me know if that's unclear. 让我知道是否不清楚。

Edit: The .always() .fail() and .done() callbacks are not stricly necessary. 编辑: .always() .fail().done()回调不是绝对必要的。 I included them for demonstration purposes, but feel free to leave them out. 我出于示范目的将它们包括在内,但可以随时将它们省略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM