[英]how do i send javascript variables to PHP and return the results
我有一个带有输入的窗体(每个窗体分别具有ID param
, param1
, param3
)和一个外部php文件,该文件带有一个名为getform()
的函数,该函数从窗体中分别获取三个参数(分别为param
, param1
, param3
)。
results
为id的文本字段显示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;
}
if(isset($_POST)){
//Calling the defined function here
Conv($_POST['postparam'], $_POST['postparam2'], $_POST['postparam3']);
}
将这些行添加到函数代码下面..并更好地在函数中回显输出,而不是返回输出。
jQuery(document).ready(function($) { $('#param3').change(function() { // put here the code // } ); })
您在Console(firbug)中遇到什么错误?
如其他答案所述,应使用$ _POST。
如果您的php代码返回数组或返回对象,则返回的内容不能放入Input中。 返回值必须是字符串
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'];
}
我知道答案不完整,因为您的问题不完整。 请让我知道控制台中的错误,以便我进一步帮助您
回声而不是返回。
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);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.