简体   繁体   English

获取JavaScript值以将它们插入mysql数据库

[英]Get javascript values to insert them into mysql database

The following code is used for getting two values from two textboxes , calculate the sum using javascript and display the result in third textbox. 以下代码用于从两个文本框中获取两个值,使用javascript计算总和并在第三个文本框中显示结果。 When I click the button, I just want these textbox values (both input and result) to be inserted into mysql database. 当我单击按钮时,我只希望将这些文本框值(输入和结果)插入到mysql数据库中。 I want to do this in the same page. 我想在同一页面中执行此操作。 How can I get the javascript values to php for inserting them into database? 我如何才能将javascript值插入php以将其插入数据库? It would be great if you could help. 如果您能提供帮助,那就太好了。

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>

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'];
<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>

You can Pass the JavaScript Values to a HIDDEN Value in FORM and then POST the form to the PHP Page. 您可以在FORM中将JavaScript值传递给HIDDEN值,然后将表单发布到PHP页面。 Below is an example of how to set hidden value. 以下是如何设置隐藏值的示例。

<!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! 希望这个能对您有所帮助!

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

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