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

时间:2020-12-05 作者:Landry

您好,我有一个函数,它使用javascript将值传递给其他函数,而无需重新加载页面。

下面javascript的第一个操作从表单获取值,并通过回调函数formbuilder\\u ajax\\u call发送到php。在php脚本中,我将这些表单值作为数组返回,以便在其他函数中调用它。下面是我的代码:

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

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);

  }
我使用var\\u dump查看make\\u short\\u代码中传递的内容,但传递的只是formbuilder\\u ajax\\u call()中的数组键值,而不是存储在数组中的内容值。但是,这些值是在

1 个回复
SO网友:Raashid Din

这个问题离题了。如果您使用的是WordPress,那么它可以这样使用。

您需要使用WordPress的Ajax功能。为了能够在后端接收数据,您必须向WordPress注册操作。

以下是您必须与WordPress一起使用的AJAX代码

add_action( "wp_ajax_formbuilder_ajax_call", \'formbuilder_ajax_call\' );
add_action( "wp_ajax_nopriv_formbuilder_ajax_call", \'formbuilder_ajax_call\' );

function formbuilder_ajax_call(){

    global $form_array;
    // Sanitize data, after all we all are not white hat hackers
    $form_content = trim(sanitize_text_field($_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() { 
    global $form_array;

    $get_mailinfo = $form_array;

    var_dump($get_mailinfo);
}

我希望这有帮助。此外,您应该像这样使用jquery,以使代码更易于阅读和维护。

这是Jquery的修改版本,

jQuery("#button_to_load_data").click(function() {
    var email = function() {
        return $("#email").val();
    }

    var other_email = function() {
        return $("#other_email").val();
    }

    var subject = function() {
        return $("#subject").val();
    }   

    var sender = function() {
        return $("#sender").val();
    }   

    var message = function() {
        return $("#message").val();
    }   

    var form_name = function() {
        return $("#form_name").val();
    }   

    var content = getPlainHtml();

 //    alert(content);     
       var data1 = {
        \'action\'   : \'make_short_code\'
        };

       $.ajax({
        url: ajaxurl,
        type: \'POST\',
        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()
        },
       })
       .done(function(data) {
            console.log(data);
       })
       .fail(function(data) {
        console.log(data);
       });
  
  });
我希望这有帮助。

更新:我假设您已经本地化了必要的变量。如果没有,请使用阅读此文档Ajax in WordPress