获取 javascript 值以将它们插入到 mysql 数据库中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17873145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:55:53  来源:igfitidea点击:

Get javascript values to insert them into mysql database

phpjavascript

提问by JeNy

The following code is used for getting two values from two textboxes , calculate the sum using javascript and display the result in third textbox. When I click the button, I just want these textbox values (both input and result) to be inserted into mysql database. I want to do this in the same page. How can I get the javascript values to php for inserting them into database? It would be great if you could help.

以下代码用于从两个文本框中获取两个值,使用 javascript 计算总和并将结果显示在第三个文本框中。当我单击按钮时,我只想将这些文本框值(输入和结果)插入到 mysql 数据库中。我想在同一页面中执行此操作。如何将 javascript 值获取到 php 以将它们插入数据库?如果你能帮忙就太好了。

Thanks
Jeny

谢谢
珍妮

Code:

代码:



<html>
<head>
<script language="javascript">
   function getText3()
   {
      var in1=document.getElementById('in1').value;
      var in2=document.getElementById('in2').value;
      var in3 = parseInt(in1, 10) + parseInt(in2, 10);
      document.getElementById('in3').value=in3;
   }
</script>
</head>
<body>

<form>
   <table width="306" border="0">
  <tr>
    <td width="146">Enter A </td>
    <td width="144"><input name="text" type="text" id="in1"/></td>
  </tr>
  <tr>
    <td>Enter B </td>
    <td><input name="text2" type="text" id="in2"/></td>
  </tr>
  <tr>
    <td height="41" colspan="2">   <center>
      <button type="button" onclick="getText3()"> Get calculated result</button></center>                        </td>
    </tr>
  <tr>
    <td><strong>RESULT</strong></td>
    <td><input name="text3" type="text" id="in3"/></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
</body>
</html>

回答by Tim Baas

For this you should use jQuery (http://jquery.com/)

为此,您应该使用 jQuery ( http://jquery.com/)

Just send them to your php file like the following:

只需将它们发送到您的 php 文件,如下所示:

// JS:
var in1 = $('#in1').val();
var in2 = $('#in2').val();
var in3 = parseInt(in1, 10) + parseInt(in2, 10);

$('#in3').val( in3 );

$.post('file.php', { one: in1, two: in2, three: in3 }, function(data) {
    alert( data );
} )

PHP file get's them as normal POST params:

PHP 文件将它们作为普通 POST 参数获取:

// file.php
echo $_POST['one'] . " + " . $_POST['two'] . " = " . $_POST['three'];

回答by sudhakar

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script language="javascript">
   function getText3()
   {
      var in1=document.getElementById('in1').value;
      var in2=document.getElementById('in2').value;
      var in3 = parseInt(in1, 10) + parseInt(in2, 10);
      document.getElementById('in3').value=in3;
    $.ajax({
        type: "POST",
        url: "your_php_file_path.php",//you can get this values from php using $_POST['n1'], $_POST['n2'] and $_POST['add']
        data: { n1: in1, n2: in2, add: in3 }
    }).done(function( msg ) {
          alert( "Data Saved: " + msg );
        });
   }
</script>

回答by DonOfDen

You can Pass the JavaScript Values to a HIDDENValue in FORMand then POST the form to the PHP Page. Below is an example of how to set hidden value.

您可以将 JavaScript 值传递给一个HIDDENFORM,然后将表单发布到 PHP 页面。以下是如何设置隐藏值的示例。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>
      </title>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <script type="text/javascript">
        function setPrice(el){
          var prices=[30, 20, 10];
          var p=el.options.selectedIndex;
          el.form.elements['price'].value=prices[p];
        }
      </script>
      </head>
      <body>
        <form>
          <select name="category" onchange="setPrice(this);">
            <option value="men">
              Men
            </option>
            <option value="women">
              Women
            </option>
            <option value="under18">
              Under 18's
            </option>
          </select>
          <input name="price" type="hidden" value="30">
        </form>
      </body>
</html>

Hope this will help you!

希望能帮到你!