[英]how do i send javascript variables to PHP and return the results
I have a form with inputs (each with ID param
, param1
, param3
respectively) and an external php file with a function called getform()
that takes three parameters from the form ( param
, param1
, param3
respectively). 我有一个带有输入的窗体(每个窗体分别具有ID
param
, param1
, param3
)和一个外部php文件,该文件带有一个名为getform()
的函数,该函数从窗体中分别获取三个参数(分别为param
, param1
, param3
)。
A text feild with id results
to display the response from php file. results
为id的文本字段显示php文件的响应。
I have placed onclick
function on param3
. 我已经将
onclick
函数放在param3
。
I need whenever a user types something in param3
it should be sent to php file and the results displayed in the text filed of id results
. 每当用户在
param3
键入内容时,我都需要将其发送到php文件,并将结果显示在id results
的文本文件中。
here is my code 这是我的代码
<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>
my php function in the php file 我的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']);
}
Add these line below your function code.. and better echo the output in your function instead of returning it. 将这些行添加到函数代码下面..并更好地在函数中回显输出,而不是返回输出。
jQuery(document).ready(function($) { $('#param3').change(function() { // put here the code // } ); })
What errors you get in Console(firbug)? 您在Console(firbug)中遇到什么错误?
As stated by other answers $_POST should be used. 如其他答案所述,应使用$ _POST。
What your php code returns if it returns an array or returns an object It can not be put into the Input. 如果您的php代码返回数组或返回对象,则返回的内容不能放入Input中。 Return value must be a string
返回值必须是字符串
PHP code must be : 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'];
}
I know answer is incomplete because your question is incomplete. 我知道答案不完整,因为您的问题不完整。 Please let me know errors from the console so that i can help you further
请让我知道控制台中的错误,以便我进一步帮助您
echo instead of return. 回声而不是返回。
if(isset($_POST)){
echo Conv($_POST['postparam'], $_POST['postparam2'], $_POST['postparam3']);
}
Do something like this, it is more clean: 做这样的事情,它更干净:
Conv($param, $param2, $param3){
// your code here
return $output;
}
As for the javascript part, jquery ajax is your friend 至于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.