Commits

Alexander Dahl committed 8452e56 Draft

complete chapter 9

Comments (0)

Files changed (4)

         </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="page365.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">
     <p>Last Updated: <em id="updatedTime"></em></p>
 
     <script src="scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
-    <script src="scripts/page334.js" type="text/javascript"></script>
+    <script src="scripts/page361.js" type="text/javascript"></script>
     <script src="scripts/jquery.idTabs.min.js" type="text/javascript"></script>
 </body>
 </html>
                 'time' => $row['finish_time'] ) );
     }
 
-    echo json_encode($runners);
+    echo json_encode(array("runners" => $runners));
 
     function db_connection( $query ) {
         mysql_connect('127.0.0.1', 'runner_db_user', 'runner_db_password')
+<?php
+    header( "Content-type: text/javascript" );
+
+    if ( $_POST["action"] == "addRunner" ) {
+        $fname = htmlspecialchars($_POST['txtFirstName']);
+        $lname = htmlspecialchars($_POST["txtLastName"]);
+        $gender = htmlspecialchars($_POST["ddlGender"]);
+        $minutes = htmlspecialchars($_POST["txtMinutes"]);
+        $seconds = htmlspecialchars($_POST["txtSeconds"]);
+
+        if ( preg_match( '/[^\w\s]/i', $fname )
+                || preg_match( '/[^\w\s]/i', $lname ) )
+        {
+            fail( "Invalid name provided." );
+        }
+
+        if ( empty( $gender ) ) {
+            fail( "Please select a gender." );
+        }
+
+        $time = $minutes . ":" . $seconds;
+
+        $query = "INSERT INTO runners SET first_name='$fname', last_name='$lname', gender='$gender', finish_time='$time'";
+        $result = db_connection( $query );
+
+        if ( $result ) {
+            $msg = "Runner: " . $fname . " " . $lname . " added successfully";
+            success( $msg );
+        } else {
+            fail( "Insert failed." );
+            exit;
+        }
+    } else if ( $_GET["action"] == "getRunners" ) {
+        $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(array("runners" => $runners));
+        exit;
+    }
+
+    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/page361.js

+var FREQ = 10000;
+var repeat = true;
+
+function clearInputs() {
+    $('#addRunner :input:not([name=action])').val('');
+}
+
+function getTime() {
+    var a_p = "";
+    var d = new Date();
+    var curr_hour = d.getHours();
+
+    (curr_hour < 12) ? a_p = "AM" : a_p = "PM";
+    (curr_hour == 0) ? curr_hour = 12 : curr_hour = curr_hour;
+    (curr_hour > 12) ? curr_hour = curr_hour - 12 : curr_hour = curr_hour;
+
+    var curr_min = d.getMinutes().toString();
+    var curr_sec = d.getSeconds().toString();
+
+    if (curr_min.length == 1) { curr_min = "0" + curr_min; }
+    if (curr_sec.length == 1) { curr_sec = "0" + curr_sec; }
+
+    $('#updatedTime').html(curr_hour + ":" + curr_min + ":" + curr_sec + " " + a_p );
+}
+
+function getTimeAjax() {
+    $('#updatedTime').load( "time.php" );
+}
+
+function getDBRacers() {
+    $.getJSON( "page365.php?action=getRunners", function(json) {
+        if ( json.runners.length > 0 ) {
+            $("#finishers_m").empty();
+            $("#finishers_f").empty();
+            $("#finishers_all").empty();
+
+            $.each( json.runners, function() {
+                var info = "<li>Name: " + this["fname"] + " "
+                        + this["lname"] + ". Time: " + this["time"]
+                        + "</li>";
+                if ( this["gender"] == "m" ) {
+                    $("#finishers_m").append( info );
+                } else if ( this["gender"] == "f" ) {
+                    $("#finishers_f").append( info );
+                } else {}
+                $("#finishers_all").append( info );
+            });
+        }
+    });
+    getTimeAjax();
+}
+
+function showFrequency() {
+    $("#freq").html( "Page refreshes every " + FREQ/1000 + " second(s)." );
+}
+
+function startAJAXcalls() {
+    if ( repeat ) {
+        setTimeout( function() {
+                getDBRacers();
+                startAJAXcalls();
+            },
+            FREQ
+        );
+    }
+}
+
+jQuery(document).ready(function(){
+
+    $('#addRunner').submit(function(){
+        return false;
+    });
+
+    $('#btnSave').click(function(){
+        var data = $("#addRunner :input").serializeArray();
+        $.post($("#addRunner").attr('action'), data, function(json){
+            if ( json.status == "fail" ) {
+                alert( json.message );
+            }
+            if ( json.status == "success" ) {
+                alert( json.message );
+                clearInputs();
+            }
+        }, "json" );
+    });
+
+    $('#btnStart').click(function(){
+        repeat = true;
+        startAJAXcalls();
+        showFrequency();
+    });
+
+    $('#btnStop').click(function(){
+        repeat = false;
+        $('#freq').html( "Updates paused." );
+    });
+
+    showFrequency();
+    getDBRacers();
+    startAJAXcalls();
+});