XML : Clicking button does nothing

I'm trying to make a program that takes 5 words from a database randomly and inserts them into an array. The page initially loads as desired, but nothing happens after the button is clicked. None of the alerts are ever triggered, so the function must never be entered, but why is beyond me. Also, I get an error saying name isn't a legitimate index (referencing line 13) the first time I try running it on a browser, so advice about that would be great too.

lingo.php:

  <?php      session_start();      if (empty($_POST["name"])):         $_SESSION["error"] = "You did not enter a name.";         header("Location: entername.php");      else:         $name = $_POST["name"];         setcookie("name", "$name", time()+3600);      endif;  ?>  <html>    <head>      <b>Welcome to Lingo, <?php echo $_COOKIE["name"]; ?></b><br />      <script src = "http://code.jquery.com/jquery-latest.js"></script>      <script type = "text/javascript" language = "javascript">      var arr = [];      function collectWords() {          $.post("getWord.php",      function(data) {          arr[word1] = $(data).find("Word1").text();          alert("function reached");          alert(arr[word1]);          arr[word2] = $(data).find("Word2").text();          alert(arr[word2]);          arr[word3] = $(data).find("Word3").text();          alert(arr[word3]);          arr[word4] = $(data).find("Word4").text();          alert(arr[word4]);          arr[word5] = $(data).find("Word5").text();          alert(arr[word5]);      });      }      </script>    </head>    <body>      <table id = "theTable" border = "1" class = "thetable"> </table>      <input type = "submit" value = "Start" onclick = "collectWords()">    </body>  </html>    

getWord.php

  <?php     $db = new mysqli('localhost', 'spj916', "cs4501", 'spj916');     if ($db->connect_error):        die ("Could not connect to db " . $db->connect_error);     endif;       $query = "select word from Words order by rand() limit 1";     $result = $db->query($query);     $rows = $result->num_rows;     if ($rows >= 1):        header('Content-type: text/xml');        echo "<?xml version='1.0' encoding='utf-8'?>";        echo "<Word1>";        $row = $result->fetch_array();        $ans = $row["word"];        echo "<value>$ans</value>";        echo "</Word1>";     else:        die ("DB Error");     endif;       $query = "select word from Words order by rand() limit 1";     $result = $db->query($query);     $rows = $result->num_rows;     if ($rows >= 1):        header('Content-type: text/xml');        echo "<?xml version='1.0' encoding='utf-8'?>";        echo "<Word2>";        $row = $result->fetch_array();        $ans = $row["word"];        echo "<value>$ans</value>";        echo "</Word2>";     else:        die ("DB Error");     endif;       $query = "select word from Words order by rand() limit 1";     $result = $db->query($query);     $rows = $result->num_rows;     if ($rows >= 1):        header('Content-type: text/xml');        echo "<?xml version='1.0' encoding='utf-8'?>";        echo "<Word3>";        $row = $result->fetch_array();        $ans = $row["word"];        echo "<value>$ans</value>";        echo "</Word3>";     else:        die ("DB Error");     endif;       $query = "select word from Words order by rand() limit 1";     $result = $db->query($query);     $rows = $result->num_rows;     if ($rows >= 1):        header('Content-type: text/xml');        echo "<?xml version='1.0' encoding='utf-8'?>";        echo "<Word4>";        $row = $result->fetch_array();        $ans = $row["word"];        echo "<value>$ans</value>";        echo "</Word4>";     else:        die ("DB Error");     endif;       $query = "select word from Words order by rand() limit 1";     $result = $db->query($query);     $rows = $result->num_rows;     if ($rows >= 1):        header('Content-type: text/xml');        echo "<?xml version='1.0' encoding='utf-8'?>";        echo "<Word5>";        $row = $result->fetch_array();        $ans = $row["word"];        echo "<value>$ans</value>";        echo "</Word5>";     else:        die ("DB Error");     endif;  ?>    

No comments:

Post a Comment