将jquery生成的表单发送到php

时间:2017-04-08 00:39:29

标签: php jquery

我下面的脚本不起作用。我试图将使用jquery函数生成的值传递给php。当我运行此代码时。表单向右滑动,但没有编码值。我在想我做的事情很简单。



<!DOCTYPE html>
<html>
  <head>
    <title>Target Example</title>
    <?php
	  $x = $_POST['total'];
	  
	  echo $x;
	  
	  ?>
	  
	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.css" />
    <script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.js"></script>
	<script>
	  $(document).ready(function() {
	    function compute() {
          var a = $('#a').val();
          var b = $('#b').val();
          var total = a * b;
          $('#total').val(total);
        }

        $('#a, #b').change(compute);
		
	  });
	</script>
  </head>
  <body>
  <form action="<?php echo $PHP_SELF;?>" method="post">
  	<div data-role="page" id="irr">
      <div data-role="header">
        <h1>Calculation</h1>
      </div>
      <div data-role="content">
        <div data-role="fieldcontain">
          <label for="a">Diameter:</label>
          <input type="number" name="a" id="a" value=""  />
          <label for="b">Diver:</label>
          <input type="number" name="b" id="b" value=""  />
          <label for="total">Result:</label>
          <input type="text" name="total" id="total" value=""  />
          <input type="submit">
        </div>
        What did we do here?
      </div>
     </div>
	  </form>
  </body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您的表单操作设置不正确。请改用$ _SERVER [&#39; SCRIPT_NAME&#39;]。此外,我还使用了短手回声功能,以防您想知道。希望能为你修复它:)

<!DOCTYPE html>
    <html>
      <head>
        <title>Target Example</title>
        <?php
          $x = $_POST['total'];

          echo $x;

          ?>

        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.css" />
        <script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.js"></script>
        <script>
          $(document).ready(function() {
            function compute() {
              var a = $('#a').val();
              var b = $('#b').val();
              var total = a * b;
              $('#total').val(total);
            }

            $('#a, #b').change(compute);

          });
        </script>
      </head>
      <body>
      <form action="<?=($_SERVER['SCRIPT_NAME'])?>" method="post">
        <div data-role="page" id="irr">
          <div data-role="header">
            <h1>Calculation</h1>
          </div>
          <div data-role="content">
            <div data-role="fieldcontain">
              <label for="a">Diameter:</label>
              <input type="number" name="a" id="a" value=""  />
              <label for="b">Diver:</label>
              <input type="number" name="b" id="b" value=""  />
              <label for="total">Result:</label>
              <input type="text" name="total" id="total" value=""  />
              <input type="submit">
            </div>
            What did we do here?
          </div>
         </div>
          </form>
      </body>
    </html>