简体   繁体   English

如何使用数组变量将值从一个 function 传递到另一个

[英]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.

相关问题 如何将变量从一个ajax函数传递给另一个 - How to pass variable from one ajax function to other Protractor 如何从其他 function 获取/使用一个变量的值 - Protractor how to get/use the values for one variable from other function 如何在延迟函数中将变量从一个JS文件传递到另一个 - How to pass variable from one JS file to the other from within a deferred function 如何通过javascript将值从一页传递到另一页,并使用php获取另一页中传递的值? - How to pass values from one page to other page by javascript and get the passed value in the other page using php? 如何将合金中的一个变量从一个控制器传递到另一控制器 - how to pass one variable from one controller to other controller in alloy 如何将值从函数传递到数组 - How to pass values from function to an array 如何将表单值从一个 function 传递到另一个 function 并显示 - How to pass Form value from one function to other function and display 如何在React中将值从一个组件传递到另一个组件? - How to pass values from one component to other in React? 将变量从数组传递到函数 - Pass variable from array into a function 如何在javascript中将变量从一个函数传递给另一个函数 - how to pass variable from one function to another function in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM