dql123000 2016-08-02 05:13
浏览 71

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

I am new to php and ajax things. i want to know that is it possible to pass a variable from one ajax success function to another ajax data field. following are two ajax calls.

$("#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. Now i want to send this clientName in insert.php through second ajax call. The second ajax call is as follows:

$("#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. 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";}
?>
  • 写回答

3条回答 默认 最新

  • dqpdb82600 2016-08-02 05:21
    关注

    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

    2. Store the value in a global javascript variable that you set within the first ajax call then catch in the second call

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算