php - 获取 javascript 值并将其插入 mysql 数据库

标签 php javascript

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

谢谢
珍妮

代码:


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

最佳答案

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

只需将它们发送到您的 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 文件将它们作为正常的 POST 参数获取:

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

关于php - 获取 javascript 值并将其插入 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17873145/

相关文章:

php - SimpleXmlElement->addAttribute() 不允许空字符串

php - Opencart 1.4 https 中的图像 URL 不正确

javascript - 在 ReactJS 中的 Spotify API 上为 PKCE 身份验证创建代码验证器和质询

javascript - Owl Carousel 不适用于 Rails

javascript - Bootstrap 模式不会从包含 AJAX 调用创建的内容的模式中显示

Javascript:没有多次调用的水平链接 promise

php - TinyMCE 画得不错,但不让我输入。在 FF 中,按钮有错误 : Component returned failure code: 0x80004005

php - MySQL加入3个级联表

php - 尝试将数字和字母插入表中时出现数据库错误,但只有数字有效

javascript - 网站上的移动菜单问题