浏览代码

Added primitive ajax.php for testing

Foppe Hemminga 6 年之前
父节点
当前提交
b5d7aae8c4
共有 2 个文件被更改,包括 51 次插入16 次删除
  1. 49 16
      ajax.php
  2. 2 0
      index.php

+ 49 - 16
ajax.php

@@ -1,21 +1,7 @@
 <?php
 
-if (is_ajax()) {
-	if (isset($_GET["venue"]) && !empty($_GET["venue"])) { //Checks if action value exists
-		// $action = $_POST["action"];
-		// switch ($action) { //Switch case for value of action
-		//	case "test":
-				test_function();
-		//		break;
-		//}
-	}
-}
-
-//Function to check if the request is an AJAX request
-function is_ajax()
-{
-	return True;
-	// return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
+if ( isset( $_GET["venue"] ) && !empty( $_GET["venue"] ) ) {
+	parse_data();
 }
 
 function test_function()
@@ -31,3 +17,50 @@ function test_function()
 	$return["json"] = json_encode($return);
 	echo $_GET['callback'] . '(' . json_encode($return ). ')';
 }
+
+function parse_data()
+{
+	$return = [];
+	$data = $_GET;
+	if ( empty( $data ) ) {
+		test_function();
+	}
+	$venue = $data['venue'];
+	$races = $data['races'];
+	if ( empty( $venue ) || empty( $races ) ) {
+		test_function();
+	}
+
+	require_once 'dbconfig.php';
+	$dsn = "pgsql:host=$host;port=5432;dbname=$db;user=$username;password=$password";
+	try{
+		// create a PostgreSQL database connection
+		$conn = new PDO( $dsn );
+
+		// display a message if connected to the PostgreSQL successfully
+		if( !$conn ){
+			test_function();
+		}
+		$query_string = "UPDATE race_program
+			SET torn = TRUE
+			WHERE race_date = %s
+				AND venue = %s 
+				AND start_time = %s;";
+		for ($i = 0; $i < count( $races ); $i++ ) {
+			$datetime = strtotime( $races['localtime'] );
+			$date = date( 'Y-m-d', $datetime );
+			$time = date( 'H:i', $datetime );
+			$return[] = [$date, $time, $venue['name']];
+			$query = sprintf( $query_string,
+				$conn->quote( $date ),
+				$conn->quote( $venue['name'] ),
+				$conn->quote( $time ) );
+			$conn->query($query);
+		}
+		$conn->commit();
+		echo $_GET['callback'] . '(' . json_encode($return ). ')';
+	}catch (PDOException $e){
+		// report error message
+		echo $e->getMessage();
+	}
+}

+ 2 - 0
index.php

@@ -52,6 +52,7 @@
             <th>start</th>
             <th>utc</th>
             <th>horse</th>
+            <th>torn</th>
         </tr>
         </thead>
         <tbody>
@@ -95,6 +96,7 @@ TABLEFOOT;
         <td><?php echo htmlspecialchars(substr($row['start_time'], 0, -3)); ?></td>
         <td><?php $t = strtotime($row['utctime']); echo gmdate('H:i', $t); ?></td>
         <td><?php echo htmlspecialchars($row['horse']); ?></td>
+        <td><?php echo htmlspecialchars($row['torn']); ?></td>
     </tr>
 	<?php endwhile; ?>
 </table>