[英]How to pass values from one function to the other using an array variable
Hello I have a function that passes values to other functions using javascript without reloading page.您好,我有一个 function 使用 javascript 将值传递给其他函数,而无需重新加载页面。
The first action of the javascript below gets values from a form and sends to php via a call back function formbuilder_ajax_call.下面 javascript 的第一个操作从表单中获取值并通过回调 function formbuilder_ajax_call 发送到 php。 In my php script i return these form values as an array so i can call it in other functions.在我的 php 脚本中,我将这些表单值作为数组返回,以便我可以在其他函数中调用它。 Below is my code:下面是我的代码:
Javascript Javascript
jQuery("#button_to_load_data").click(function() {
var email = $("#email").val();
var other_email = $("#other_email").val();
var subject = $("#subject").val();
var sender = $("#sender").val();
var message = $("#message").val();
var form_name = $("#form_name").val();
var content = getPlainHtml();
// alert(content);
var data = {
'action' : 'formbuilder_ajax_call', // the name of your PHP function!
'form_content' : content, // another random value we'd like to pass
'email' : email,
'other_email' : other_email,
'subject' : subject,
'sender' : sender,
'message' : message,
'form_name' : form_name,
};
var data1 = {
'action' : 'make_short_code'
};
jQuery.post(ajaxurl, data, function(response) {
jQuery("#receiving_div_id").html(response);
});
jQuery.post(ajaxurl, data1, function(response) {
jQuery("#receiving_div_id").html(response);
});
});
PHP PHP
function formbuilder_ajax_call(){
$form_content = trim($_POST['form_content']); // these values are from a form
$email = trim($_POST['email']);
$other_email = trim($_POST['other_email']);
$subject = trim($_POST['subject']);
$sender = trim($_POST['sender']);
$message = trim($_POST['message']);
$form_name = trim($_POST['form_name']);
$form_array = array();
$form_array = array(
"form_content" => $form_content,
"email" => $email,
"other_email" => $other_email,
"subject" => $subject,
"sender" => $sender,
"message" => $message,
"form_name" => $form_name,
);
return $form_array;
}
function make_short_code() {
$get_mailinfo = formbuilder_ajax_call();
var_dump($get_mailinfo);
}
I am using var_dump to see the content being passed in make_short_code but its only the array key values from formbuilder_ajax_call() that are passed and not the content values that was stored in the array.我正在使用 var_dump 查看在 make_short_code 中传递的内容,但它只是传递的来自 formbuilder_ajax_call() 的数组键值,而不是存储在数组中的内容值。 However the values are passed in the但是这些值是在
try json_encode($form_array)
in php script and add "JSON" as 4th param
in jquery ajax.在 php 脚本中尝试json_encode($form_array)
并在 jquery ajax 中添加"JSON" as 4th param
。 That way you can access data sent from php script in callback eg response.email
这样您就可以在回调中访问从 php 脚本发送的数据,例如response.email
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.