Alexander Dahl avatar Alexander Dahl committed c89724b Draft

get json with ajax request (and tune php to output this)

Comments (0)

Files changed (3)

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" lang="de" xml:lang="de">
+<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
 <head>
     <title>2011 Race Finishers</title>
     <link href="styles/page295.css" rel="stylesheet" type="text/css" />
         </div>
         <div id="new">
             <h4>Add New Finisher</h4>
-            <form id="addRunner" name="addRunner" action="page343.php" method="POST">
+            <form id="addRunner" name="addRunner" action="page343.php" method="post">
                 First Name: <input type="text" name="txtFirstName" id="txtFirstName" /> <br />
                 Last Name: <input type="text" name="txtLastName" id="txtLastName" /> <br />
                 Gender: <select id="ddlGender" name="ddlGender">
                     <option value="f">Female</option>
                     <option value="m">Male</option>
                 </select><br/>
-                Finish Time: 
+                Finish Time:
                 <input type="text" name="txtMinutes" id="txtMinutes" size="10" maxlength="2" />(Minutes)
                 <input type="text" name="txtSeconds" id="txtSeconds" size="10" maxlength="2" />(Seconds)
                 <br/><br/>
+<?php
+    header( "Content-type: text/javascript" );
+
+    $query = "SELECT first_name, last_name, gender, finish_time FROM runners order by finish_time ASC ";
+    $result = db_connection($query);
+
+    $runners = array();
+
+    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
+        array_push( $runners, array( 'fname' => $row['first_name'],
+                'lname' => $row['last_name'], 'gender' => $row['gender'],
+                'time' => $row['finish_time'] ) );
+    }
+
+    echo json_encode($runners);
+
+    function db_connection( $query ) {
+        mysql_connect('127.0.0.1', 'runner_db_user', 'runner_db_password')
+            OR die( fail( 'Could not connect to database.' ) );
+        mysql_select_db('hfjq_race_info');
+
+        return mysql_query($query);
+    }
+
+    function fail($message) {
+        die(json_encode(array('status' => 'fail', 'message' => $message)));
+    }
+
+    function success($message) {
+        die(json_encode(array('status' => 'success', 'message' => $message)));
+    }
+?>

scripts/page334.js

 var repeat = true;
 
 function clearInputs() {
-    §('#addRunner:input').val('');
+    $('#addRunner:input').val('');
 }
 
 function getTime() {
     $('#updatedTime').load( "time.php" );
 }
 
+function getDBRacers() {
+    $.getJSON( "page357.php", function(json) {
+        alert( json.length );
+    });
+    getTimeAjax();
+}
+
 function getXMLRacers() {
     $.ajax({
         url:        "page295/finishers.xml",
 function startAJAXcalls() {
     if ( repeat ) {
         setTimeout( function() {
-                getXMLRacers();
+                getDBRacers();
                 startAJAXcalls();
             },
             FREQ
     });
 
     showFrequency();
-    getXMLRacers();
+    getDBRacers();
     startAJAXcalls();
 });
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.