weixin_33699914 2015-11-13 15:49 采纳率: 0%
浏览 13

在PHP中使用AJAX POST值

I'm trying to evaluate what PHP information to display depending on when a user selects that item from a list. After help and some research, I've heard a lot about AJAX and have looked into it, however, I'm a bit confused on the use of it.

I have an AJAX function to return to me a variable value from an external PHP file and it's working as anticipated. However, I would really like to capture that and set it to a PHP variable to continue my script execution. How does one accomplish this? I want to stay within the same file as the one that called the AJAX function. I don't think I fully comprehend how it all works and I'm still unsure of the exact method to get that value as a variable in my current PHP file.

My AJAX:

function captureName(item){
        $.ajax({
            type: 'POST',
            url: 'externalFile.php',
            data: { item: item },
            success: function(data) {
            alert(data);
            }
        });
    }                           

My PHP External File:

  $item = $_POST['item'];
   echo $item;

Stuff I want to set on my current PHP File determined by what is returned by AJAX:

    switch ($item){
                 case "item1":
                     $itemName = "item1";
                     $location = "location";
                     $HWType = "HW Type";
                 break;
                 case "item2":
                     $vmName = "item2";
                     $location = "location2";
                     $HWType = "HW Type2";                       
                     break; 

}
  • 写回答

2条回答 默认 最新

  • MAO-EYE 2015-11-13 16:02
    关注

    You can do it in 1 shot.

    your javascript :

    function captureName(item){
        console.log(item);
        $.post('externalFile.php', {item:item}, function(jsonTabInfo){
            console.log(jsonTabInfo);
        }, 'json');        
    }
    

    your PHP :

    $item = $_POST['item'];
    
    $tabInfo = array();
    switch ($item){
         case "item1":
             $tabInfo['itemName'] = "item1";
             $tabInfo['location'] = "location";
             $tabInfo['HWType'] = "HW Type";
         break;
         case "item2":
             $tabInfo['vmName'] = "item2";
             $tabInfo['location'] = "location2";
             $tabInfo['HWType'] = "HW Type2";
             break; 
         default:
             $tabInfo['vmName'] = "Unknown";
             $tabInfo['location'] = "Unknown";
             $tabInfo['HWType'] = "Unknown";
    }
    
    echo json_encode($tabInfo);
    

    In PHP you use JSON to send all your info to your client (javascript). Those informations are new stored in jsonTabInfo

    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程