Jump to content

AJAX Post instead of GET


guestabc

Recommended Posts

Hi, I am trying to create a form that creates a radio button for all the teams in 3 different leagues, allowing the user to select one team from each league, which I have done. When the user selects a team I want there to be a total points output which gets updated upon each selection of a radio button using AJAX. Each team has points held against them in the database. I have created the following three scripts using the AJAX GET method but I think I may need to use POST instead but am unsure how to do this because of the dynamic radio button names?

 

Hopefully you understand what I am trying to do here!

 

Javascript

<script type="text/javascript">
function showLeagueTeamDetails(str)
{
if (str=="")
  {
	document.getElementById("LeagueTeamDetails").innerHTML="";
	return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  	xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  	if (xmlhttp.readyState==4 && xmlhttp.status==200)
  	{
  		document.getElementById("LeagueTeamDetails").innerHTML=xmlhttp.responseText;
  	}
  }
xmlhttp.open("GET","zz_get_test_points.php?q="+str,true);
xmlhttp.send();
}
</script>

 

Outputs The radio buttons

<?PHP
$sql  = 'SELECT team_id ';
$sql .= 'FROM league ';
$sql .= 'WHERE league_id = '.$league_row_array['league_id'];

$db_query = mysql_query($sql,$db_con);

if(!$db_query)
{
mysql_close($db_con);
exit();
}

$teams_count = mysql_num_rows($db_query_result);

for($team = 0; $team < $teams_count; $team++)
{			
$league_team_row = mysql_fetch_array($db_query_result, MYSQL_ASSOC);
print
print('<input type="radio" name="league_team_'.$league_row['league_id'].'" value="'.$league_team_row['team_id'].'" onclick="showLeagueTeamDetails(this.value)" />');

}
print('<div id="LeagueTeamDetails"><b>Total Points of Selected Teams To Show Here</b></div>');
?>

 

The script that gets a total points score for the three teams

$q=$_GET["q"];

// Select query on table variant_groups
$sql  = 'SELECT points ';
$sql .= 'FROM league ';
$sql .= 'WHERE team_id = '.$q.' ';
$sql .= 'ORDER BY points ASC';

if(!$db_query_result)
{
mysql_close($db_con);
exit();
}

// Number of variant groups
$teams_count = mysql_num_rows($db_query_result);

// Display variant groups
for($team = 0; team < $teams_count; $team++)
{
$team_points_row = mysql_fetch_array($db_query_result, MYSQL_ASSOC);
$_SESSION['total_points'] = $_SESSION['total_points'] + $team_points_row['points'];
print($_SESSION['total_points']);
}	

Link to comment
Share on other sites

While we could discuss the topic - POST via AJAX - I don't think that is the problem.

 

First, I assume the "league array" is set prior to the code you've shared... otherwise... this would really be bombing. You're dynamic radios should be fine with the onclick event as is. That said, what is actually happening with this code that makes you think there is a problem?

 

Is the AJAX always giving you the last selected team's point values only??

 

The reason that would be is that the SESSION is not persistent over an AJAX call. So if your AJAX handling script - "zz_get_test_points.php" - doesn't start out with a session_start() call or some inclusion of a session ini script from a framework you may have in place, then the session is not usable in the sense you would want it to be. In other words, $_SESSION['total_points'] would always be uninitialized when you were using it and would be initialized by the current selected team's points ONLY. The tally process wouldn't work.

 

If that is the issue... there is another. You can have n teams and n leagues... but you can only pick 1 team per league... but you only have 1 session var to hold the total. So... if you have...

 

L1 - T1: 10pts, T2: 15pts, T3: 5pts

L2 - T1: 0pts, T2: 10pts, T3: 15pts

L3 - T1: 20pts, T2: 10pts, T3: 5pts

 

we may pick: L1T2, L2T3, L3T2 which would give us 15+15+10 = 40pts total... now what happens if I change my L1 selection to say T1 instead of T2? Total points SHOULD say 35pts... but with the logic you are trying to implement, at best, this would no tell you the total is 50pts. There is no "substraction" of points... or handle on each teams points.

 

Is it possible to eliminate AJAX here though? Just store the points as the teams values on the radio buttons. Or do you need those radios to have the id as the value for form submission beyond the process you are sharing here?

 

In either case, you may want to just do the tally on return or in a session based array (again considering that you would have to start your session on the AJAX handling php script). Here's a description on those to resolutions... take your pick.

 

1 - on return of values...

Just return the point value from the ajax and insert it into a hidden form element (you would create 1 hidden container per league, something like <input type="hidden" id="league<?php echo $leagueid; ?>" value="" />). Then on return from the AJAX, takes its returned content and just say document.getElementById('league' + needAnotherParamForLeagueId).value = returnedValue; then automatically trigger a function that would tally all the values of all the hidden league fields.

 

Now... for the easier method...

 

2 - session based array

In your main page where the form is printed out, create this...

 

$_SESSION['leaguepoints'] = array();

 

Then as you loop through the leagues and print the radios, take the league id and make a new element in this array with it as the key...

 

$_SESSION['leaguepoints'][$league_row['ID']] = 0;

 

Then in your AJAX handling script - zz_get_test_points.php - you would simply update the appropriate array element AND loop through the array tallying the values....

 

...

$_SESSION['leaguepoints'][$teamsLeague] = $points;

...

$totalpoints = 0;

foreach ($_SESSION['leaguepoints'] as $league=>$points)

{

$totalpoints += $points;

}

 

echo $points; // or print or whatever you want to do to return the value.

 

 

Hope that helps. If you ever need help and want me to take a look, I have a direct help forum that I just open on my site for people to get programming help directly from me. My credentials are posted there to. http://www.evileyegames.com

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.