Page 1 of 1

PHP / SQL form w/ Ajax - Please help me...

Posted: Mon Aug 31, 2009 4:14 pm
by k3ndr4
Hello there. I'm trying to develop an survivor fantasy football site. Basically, I have a form with two dropdown boxes. The first drop down box works just fine - it queries SQL and posts the NFL schedule in a table based on the week # the user selects from the dropdown list. I would like to take the awayTM (away team) and homeTM (home team) results and also put them in a 2nd dropdown list for users to select their weekly football picks from. How do I populate the 2nd dropdown list? I realize that the data is already out there given that is displayed in the table properly, but don't know how to send it to the 2nd dropdown list. Sorry for such an elementary question, but this is my first attempt with PHP/SQL/Ajax.

Here is my code:

selectweek.js:

Code: Select all

 
// JavaScript Document
var xmlhttp;
 
function showWeek(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support HTTP Requests");
  return;
  }
var url="getweek.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
 
function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
 
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
 
getweek.php:

Code: Select all

 
<?php
$q=$_GET["q"];
 
$conn = mysql_connect("localhost","omitted","omitted");
if (!$conn)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("omitted for security",$conn);
 
$sql="SELECT weekNum, awayTM, homeTM, date, time FROM schedule09 WHERE weekNUM = '".$q."'";
 
$result = mysql_query($sql);
 
echo "<table border='0'>
<tr>
<th><u>Away</u></th>
<th></th>
<th><u>Home</u></th>
<th></th>
<th><u>Date</u></th>
<th></th>
<th><u>Time</u></th>
</tr>";
 
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['awayTM'] . "</td>";
  echo "<td>" . " " . "</td>";
  echo "<td>" . $row['homeTM'] . "</td>";
  echo "<td>" . " " . "</td>";
  echo "<td>" . date('D m/d/y', strtotime($row['date'])) . "</td>";
  echo "<td>" . " " . "</td>";
  echo "<td>" . date('g:i A', strtotime($row['time'])) . "</td>";
  echo "</tr>";
  }
echo "</table>";
 
mysql_close($conn);
?>
 
My index.php page:
 
<div id="weekSelectContainer">
                        <form><b>Step 1: SELECT A WEEK</b><br><br>
                        <select name="weeks" id="firstselectbox" onchange="showWeek(this.value)">
                        <option value="0">Please select...</option>
                        <option value="1">Week 1</option>
                        <option value="2">Week 2</option>
                        <option value="3">Week 3</option>
                        <option value="4">Week 4</option>
                        <option value="5">Week 5</option>
                        <option value="6">Week 6</option>
                        <option value="7">Week 7</option>
                        <option value="8">Week 8</option>
                        <option value="9">Week 9</option>
                        <option value="10">Week 10</option>
                        <option value="11">Week 11</option>
                        <option value="12">Week 12</option>
                        <option value="13">Week 13</option>
                        <option value="14">Week 14</option>
                        <option value="15">Week 15</option>
                        <option value="16">Week 16</option>
                        <option value="17">Week 17</option>
                        </select>
                        </form>
                        <div id="txtHint"></div>
                        <br />
                        <br />
                        </div>
 
I want to add a Step 2 dropdown list that is populated from the schedule so it does not include teams with byes that given week.

Re: PHP / SQL form w/ Ajax - Please help me...

Posted: Mon Aug 31, 2009 5:06 pm
by Benjamin
:arrow: Moved to Javascript