/    Sign up×
Community /Pin to ProfileBookmark

AJAX radio buttons

I have a [B]contact[/B] table generated from a database. Included in the table from that query is a form that has the value of the [b]contact ID[/b] as it is recorded in the database (all radio buttons).

At the bottom of this table I have a button that calls a new JS function that should replace the current table with a table specifically about the selected [b]contact[/b].

However when the “onclick” event happens, noting happens, at all. No javascript errors, no PHP errors, no return, nada. Could it be due to the fact that these radio buttons aren’t included in the source code, so the external js function can’t access them? I have no idea, because the buttons included on the page work as much as you wan’t (changing between personal or group contacts).

Heres the script:

[code=php]
////////////
//My Table
$display = “<div class=’tbl_bg’>”;
$display .= “<table><thead><tr>”;
$display .= “<th class=’main’ colspan=’4′>$title</th></tr><tr>”;
$display .= “<th>Name</th>”;
$display .= “<th>Email</th>”;
$display .= “<th>Work Phone</th>”;
$display .= “<th>View Details</th></tr></thead>”;

$i = 0;

while(list($id, $name,$email,$w_phone) = mysql_fetch_array($result))
{
$tr = ($i%2 == 0) ? “<tr style=’background-color:#E0EEEE’>” : “<tr style=’background-color:#FFE4C4′>”;

if (($email == “”) || (!$email)) {
$email = “&nbsp”;
}
if (($w_phone == “”) || (!$w_phone)) {
$w_phone = “&nbsp”;
}
if ((!$name) || ($name == “”)) {
continue;
}
$display .= “$tr<td>$name</a></td>”;
$display .= “<td>$email</td>”;
$display .= “<td>$w_phone</td>”;
$display .= “<td align=’center’><input type=’radio’ name=’selectedContact’ value=’$id’></td></tr>”;
$i++;
}
$display .= “<tr><td colspan=’3′>&nbsp;</td><td align=’center’>”;
$display .= “<input type=’button’ value=’Show Details’ name=’showMe’ onclick=”showDetails()”></td></tr>”;
//^^Button
$display .= “</table></form></div>”;
[/code]

[code=php]
//My Function
///////////////
function showDetails(button)
{
var ajaxRequest;

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (e) {
try{
ajaxRequest = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (e){
// Not supported
alert(“AJAX supported browser required for this page.”);
return false;
}
}
}
ajaxRequest.onreadystatechange = function()
{
if (ajaxRequest.readyState == 4)
{
var ajaxDisplay1 = document.getElementById(‘dispContacts1’);
var ajaxDisplay2 = document.getElementById(‘dispContacts2’);
var ajaxDisplay3 = document.getElementById(‘dispDetails’);

ajaxDisplay1.innerHTML = “”;
ajaxDisplay2.innerHTML = “”;
if ((ajaxRequest.responseText == “”) || (ajaxRequest.responseText == null))
{
ajaxDisplay3.innerHTML = “NO RESPONSE TEXT”;
}
else
{
ajaxDisplay3.innerHTML = ajaxRequest.responseText;
}

}
var radField = document.detailform.selectedContact;
var radLength = document.detailForm.selectedContact.length;

for (var i=0; i<radLength; i++) {
if (radField[i].checked) {
var chkContact = radField[i].value;
continue;
}
}

var detailQuery = “?id=” +chkContact;

ajaxRequest.open(“GET”, “contactDetails.php” +detailQuery, true);
ajaxRequest.send(null);
}
}
[/code]

[code=php]
//The script to get details
////////////////////////////
if (isset($_GET[‘id’])) {
$id = $_GET[‘id’];
}
else {
echo “No Get Qeury!”;
}

$query = “SELECT * FROM contacts WHERE id=’$id'”;

$result = mysql_query($query,$db) or die (mysql_error());

$row=mysql_fetch_Array($result);

$display = “<div class=’tbl_bg’><table class=’form’><tr><th colspan=’2′>Contact Details</th></tr>”;
$display .=”<tr><td align=’right>Name: </td><td>”.$row[‘name’].”</td></tr>”;
$display .=”<tr><td align=’right>Primary Email: </td><td>”.$row[’email’].”</td></tr>”;
$display .=”<tr><td align=’right>Other Email: </td><td>”.$row[‘alt_email’].”</td></tr>”;
$display .=”<tr><td align=’right>Work Phone: </td><td>”.$row[‘w_phone’].”</td></tr>”;
$display .=”<tr><td align=’right>Cell Phone: </td><td>”.$row[‘c_phone’].”</td></tr>”;
$display .=”<tr><td align=’right>Other Phone: </td><td>”.$row[‘o_phone’].”</td></tr>”;
$display .=”<tr><td align=’right>Address: </td><td><textarea readonly=’readonly’>”.$row[‘address’].”</textarea></td></tr>”;
$display .=”<tr><td align=’right>Comments: </td><td><textaera readonly=’readonly’>”.$row[‘comments’].”</textarea></td></tr>”;
$display .=”</table></div>”;

echo mysql_error();
echo $id.”<br>”;
echo $query.”<br>”;
echo $display;
[/code]

Pretty hard to tell whats wrong when nothing produces an error and nothing happens.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@cluettrJul 14.2007 — Have you added the MySQL connection function within the script that is called via ajax? Without it it will not connect to the database. An Ajax pulled script will not inherently contain the values, parameters and variables of the container script. You need to reinstantiate those unless you are using javascript or some other client side language.
Copy linkTweet thisAlerts:
@TJ111authorJul 14.2007 — I include a database connection file for all my PHP/Database scripts. Also I always put a mysql_close at the end of each of those scripts.

If you look at the top of the 3rd script i put a response if there is no $_GET query, so after thinking about it I think my problem is in the middle script. I think I am never reaching readyState 4.
Copy linkTweet thisAlerts:
@TJ111authorJul 16.2007 — I added the following to my script. Still nothing happens. Still no errors in either PHP, MySQL, or javascript.

[code=php]
if (ajaxRequest.readyState == 4)
{
var ajaxDisplay1 = document.getElementById('dispContacts1');
var ajaxDisplay2 = document.getElementById('dispContacts2');
var ajaxDisplay3 = document.getElementById('dispDetails');

ajaxDisplay1.innerHTML = "";
ajaxDisplay2.innerHTML = "";
if (ajaxRequest.status == 200)
{
ajaxDisplay3.innerHTML = ajaxRequest.responseText;
}
else is (ajaxRequest.status != 200)
{
ajaxDisplay3.innerHTML = ajaxRequest.status;
}
}

else if (ajaxRequest.readyState < 4)
{
var ajaxDisplay1 = document.getElementById('dispContacts1');
var ajaxDisplay2 = document.getElementById('dispContacts2');
var ajaxDisplay3 = document.getElementById('dispDetails');

ajaxDisplay1.innerHTML = "";
ajaxDisplay2.innerHTML = "";
ajaxDisplay3.innerHTML = ajaxRequest.readyState;
}
[/code]


I'm at a total loss.
Copy linkTweet thisAlerts:
@TJ111authorJul 16.2007 — WOW I'm an idiot. The whole time my problem was that my query was accidentally included in my "onreadystatechange" function. So this whole time it was just because my closing bracket was in the wrong spot :mad: . Haha alls well that ends well I guess.

[code=php]
ajaxRequest.onreadystatechange = function()
{
if (ajaxRequest.readyState == 4)
{
var ajaxDisplay1 = document.getElementById('dispContacts1');
var ajaxDisplay2 = document.getElementById('dispContacts2');


ajaxDisplay1.innerHTML = "";
if (ajaxRequest.status == 200)
{
ajaxDisplay2.innerHTML = ajaxRequest.responseText;
}
}

} //This bracket was underneath this query before, not above it
var hiddenInput = document.detailForm.hiddenInput.value;
var detailQuery = "?id=" +hiddenInput;

ajaxRequest.open("GET", "contactDetails.php" +detailQuery, true);
ajaxRequest.send(null);

}
[/code]
×

Success!

Help @TJ111 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...