我如何将PHP变量发送到PHP并返回结果

时间:2016-12-23 10:16:58

标签: javascript php jquery

我有一个带输入的表单(分别带有ID paramparam1param3)和带有名为getform()的函数的外部php文件,它带有三个参数来自表单(paramparam1param3

标识为results的文本字段,用于显示php文件的响应 我已将onclick功能放在param3上。

我需要用户在param3中输入内容时将其发送到php文件,结果显示在id results的文本字段中。

这是我的代码

    
<script>
  function post(){
    var param = $('#param').val();
    var param2 = $('#param2').val();
    var param3 = $('#param3').val();
    $.post('curencyconvert.php', {
      postparam: param,
      postparam2: param2,
      postparam3:param3
    }, function(data){
      $('#results').html(data);
    });
  }
</script>

我在php文件中的php函数

function Conv($param,$param2,$param3){
    //my code here
    return $output;
}

4 个答案:

答案 0 :(得分:1)

if(isset($_POST)){
//Calling the defined function here
Conv($_POST['postparam'], $_POST['postparam2'], $_POST['postparam3']);
}

在功能代码下添加这些行..并且更好地回显函数中的输出而不是返回它。

答案 1 :(得分:0)

jQuery(document).ready(function($) {
$('#param3').change(function() {
// put here the code //
} );
})

答案 2 :(得分:0)

你在Console(firbug)中遇到了什么错误?

如其他答案所述,应使用$ _POST。

如果php代码返回一个数组或返回一个对象,它会返回什么内容它无法放入Input中。返回值必须是a     串

PHP代码必须是:

      function Conv($param,$param2,$param3){
        //my code here
          // take particular field of the DB results or resultset that you want to show in the input.
            return $output['name'];

        }

我知道答案是不完整的,因为你的问题不完整。 请告诉我控制台的错误,以便我可以为您提供进一步的帮助

答案 3 :(得分:0)

回声而不是回归。

if(isset($_POST)){
  echo Conv($_POST['postparam'], $_POST['postparam2'], $_POST['postparam3']);
}

做这样的事情,它更干净:

Conv($param, $param2, $param3){
   // your code here
   return $output;
}

至于javascript部分,jquery ajax是你的朋友

 function post(){

    var param = $('#param').val();
    var param2 = $('#param2').val();
    var param3 = $('#param3').val();

    $.ajax({
        url: '/path/to/file',
        type: 'POST',
        data : { postparam: param, postparam2: param2, postparam3: param3 },     
    }).done(function(data) {
          $('#results').html(data);
    });

}