ITM381B Linking DB items between Client-Server

https://secrdir.com/campus/ionic/ionic_2_free.php

Looking at the https://secrdir.com/campus/ionic/files.php?show=add_agent.php php code and https://secrdir.com/campus/ionic/ionic_2_pro.php JavaScript code side-by-side.

In add_agent.php has

$_REQUEST is where the parameter was passed from JavaScript/AngularJS? add_agent code at the bottom. 
$_REQUEST in PHP is HTTP Request Variables. An associative array that by default contains the contents of $_GET, $_POST, and $_COOKIE.
$_REQUEST is one of the 'superglobal' variables of PHP.
<?php /* This is add_agent.php */
	header('Access-Control-Allow-Origin: *');

	include('_global.php');

	$datetime = date('Y-m-d H:i:s');

	$AgentName=$_REQUEST['AgentName'];

	$OfficeKey=$_REQUEST['OfficeKey'];

	$AgentName_as=addslashes($AgentName);

	$AgentID = 0;

	$OfficeLocation = '';

	$inserted = 0;

	$message='';

	if( strlen($AgentName) > 5 && $OfficeKey > 0 )
	{
		$sql = "SELECT AgentName, OfficeKey FROM AGENT WHERE AgentName='$AgentName_as' AND OfficeKey='$OfficeKey'";

		$sqlTable = mysql_query($sql, $conn) or die("Couldn't perform query $sql (".__LINE__."): " . mysql_error() . '.');

		if($sqlRecord = mysql_fetch_assoc($sqlTable))
		{
			$message = "already added";
		}
		else
		{
			$sql="INSERT INTO AGENT (AgentName,OfficeKey) VALUES ('$AgentName_as',$OfficeKey)";
			$sqlExecute = mysql_query($sql, $conn) or die("Couldn't perform query $sql (".__LINE__."): " . mysql_error() . '.');
			$message = "added";
			$inserted = 1;
		}

		$sql = "SELECT * FROM AGENT INNER JOIN OFFICE ON OfficeKey=OfficeID WHERE AgentName='$AgentName_as' AND OfficeKey='$OfficeKey'";

		$sqlTable = mysql_query($sql, $conn) or die("Couldn't perform query $sql (".__LINE__."): " . mysql_error() . '.');

		if($sqlRecord = mysql_fetch_assoc($sqlTable))
		{
			$message = $sqlRecord['AgentName'] . " $message at " . $sqlRecord['OfficeLocation'] . " location.";
			$AgentID = $sqlRecord['AgentID'];
			$OfficeLocation = $sqlRecord['OfficeLocation'];
		}
	}
	else
	{
		$message = "AgentName or OfficeKey wasn't specified correctly.";
	}

	$json = json_encode([		"datetime" => $datetime,
								"inserted" => $inserted,
								"message" => $message,
								"AgentID" => $AgentID,
								"AgentName" => $AgentName,
								"OfficeKey" => $OfficeKey,
								"OfficeLocation" => $OfficeLocation
						], JSON_PRETTY_PRINT);

	echo $json;

?>
  • Add Agent
    Replace line: function ($scope, $stateParams) {
    with 25 lines of code:

    function ($scope, $stateParams, GetOffices, AddAgent) {
    
      $scope.offices = [];    
    
      $scope.data = {};
    
      $scope.addAgent = function(){
        var agent = $scope.data.agent;
        var office = $scope.data.office;
        $scope.hideButton=true;
    
        AddAgent.getPost(agent, office) 
        .then(function(response) {
          $scope.post = response;
         });
      };
    
      GetOffices.getPost()
      .then(function(response) {
        $scope.post = response;
        $scope.offices =  $scope.post.records;
    	$scope.offices.unshift({ "OfficeID": 0, "OfficeLocation": "--- Select Office Location ---" });
    	$scope.data.office=0;
      });
    

Adding API Services JavaScript

services.js the first line should be:
angular.module('app.services', []) then the rest follows. 

.service('AddAgent', function($http) {
  return {
    getPost: function(agent,office) {
      var query = '?AgentName='+agent;
      query += '&OfficeKey='+office;
      return $http.get('https://secrdir.com/campus/ionic/add_agent.php'+query)
      .then(function (response) {
        return response.data;
      });
    }
  };
})

Advertisements