user5166162
user5166162

Reputation: 156

Using AJAX POST Value in PHP

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; 

}

Upvotes: 1

Views: 75

Answers (2)

ThinkTank
ThinkTank

Reputation: 1191

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

Upvotes: 2

Nathan Manley
Nathan Manley

Reputation: 26

Try using fiddler to see what data is actually getting sent via AJAX. Telerik Fiddler

If it is returning empty brackets, it means no case was triggered in the switch, thus assigning to data to the $tabInfo array. Below I added a default case, with might help identify the issue.

switch($item) {
    case "item1":
         $tabInfo['itemName'] = "item1";
         $tabInfo['location'] = "location";
         $tabInfo['HWType']   = "HW Type";
         break;
    case "item2":
         $tabInfo['itemName'] = "item2";
         $tabInfo['location'] = "location2";
         $tabInfo['HWType']   = "HW Type 2";
         break;
    default:
         $tabInfo['itemName'] = "Undefined - " . $item;
         $tabInfo['location'] = "Undefined";
         $tabInfo['HWType']   = "Undefined";
         break;
}

Upvotes: 1

Related Questions