简体   繁体   English

将变量从一个ajax函数传递到另一个ajax函数

[英]passing variable from one ajax function to another ajax function

I am new to php and ajax things. 我是php和ajax的新手。 i want to know that is it possible to pass a variable from one ajax success function to another ajax data field. 我想知道是否可以将变量从一个ajax成功函数传递到另一个ajax数据字段。 following are two ajax calls. 以下是两个ajax调用。

$("#submit_btn").on("click",function(e) {
    $.ajax({
    url: "./script.php",
    type: 'POST',
    data: {
        name: $('#clientName').val(),
        email: $('#clientEmail').val(),
        question: $("#quiz").val()
    }
}).done(function(data){
        //alert(data);
        //console.log(data);

});

});

On first success function return clientName. 在第一个成功函数上,返回clientName。 Now i want to send this clientName in insert.php through second ajax call. 现在,我想通过第二个ajax调用在insert.php中发送此clientName。 The second ajax call is as follows: 第二个ajax调用如下:

$("#chat_send_btn").on("click",function(e) {
    $.ajax({
    url: "./insert.php",
    type: 'POST',
    data: {
            textarea:$('#hidden_textarea').val(document.getElementById('text_area').innerHTML),
            customerName: data
    }

}).done(function(data){
    $('html, div').animate({scrollTop: $('#chat_messages_area').height()+5000});
});

}); });

this is insert.php. 这是insert.php。 is it ok or am i doing something wrong? 可以吗,还是我做错了什么?

<?php
session_start();    
require 'connect.php';
$uname ='';
$msg = '';
$tableName= "logs";
if(isset($_POST['textarea']) && isset($_POST['data']))
{
   $msg = $_POST['textarea'];
   $uname = $_POST['data'];
   $sql_insertquery = "INSERT INTO `logs` (`msgID`, `username`, `msg`) VALUES (NULL, '$uname', '$msg');";
   $sth = $conn->query($sql_insertquery);
   $sql_selectquery = "SELECT * FROM logs";

}
else{echo "error";}
?>

创建一个隐藏的输入/自定义属性,并在第一个ajax完成时将名称保存在那里,然后显示聊天,在第二个ajax调用中,您只需引用该隐藏的输入/自定义属性值

Here are 2 approaches: 这是两种方法:

  1. Set the value needed in a hidden html field on first call success and then obtain it in the next Ajax call 在首次调用成功时,在隐藏的html字段中设置所需的值,然后在下一个Ajax调用中获取它

  2. Store the value in a global javascript variable that you set within the first ajax call then catch in the second call 将值存储在您在第一个ajax调用中设置的全局javascript变量中,然后在第二个调用中捕获

Easiest way is to use a global variable and set its value to the success value and then use it in the second ajax call like below : 最简单的方法是使用全局变量并将其值设置为成功值,然后在第二个ajax调用中使用它,如下所示:

var clientName = '';
$("#submit_btn").on("click",function(e) {
    $.ajax({
        url: "./script.php",
        type: 'POST',
        data: {
            name: $('#clientName').val(),
            email: $('#clientEmail').val(),
            question: $("#quiz").val()
        }
    }).done(function(data){

        //alert(data);
        //console.log(data);

        //Set the clientName data here 
        clientName = data.clientName;

    });

});

Now use this variable value in second ajax call : 现在在第二个ajax调用中使用此变量值:

//append the clientName data to be sent to second ajax in your data

$("#chat_send_btn").on("click",function(e) {
    $.ajax({
        url: "./insert.php",
        type: 'POST',
        data: {clientName : clientName}, //passed clientName data from first ajax call success   
    }).done(function(data){

        $('html, div').animate({scrollTop:$('#chat_messages_area').height()+5000});
    });
});

Another solution would be to set a data attribute for the "#chat_send_btn" button with the required data value from first ajax call and then use it in second ajax call : 另一个解决方案是为“ #chat_send_btn”按钮设置一个数据属性,该属性具有来自第一个ajax调用的所需数据值,然后在第二个ajax调用中使用它:

//first ajax call success code 
.done(function(data){

        //alert(data);
        //console.log(data);

        //Set the clientName data here 
        $('#chat_send_btn').data('clientName', data.clientName);

});

And then use it in second ajax using $('#chat_send_btn').data('clientName') ; 然后使用$('#chat_send_btn').data('clientName')在第二个ajax中使用它;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM