Jump to content

Ajax Radio Buttons not storing values


mcfmullen

Recommended Posts

Alright so I have a pair of radio buttons that are to store their values into a database in order to retain their state when the user returns to the site in the future.

 

For now, my code is just testing to make sure the values are being stored in the first place, into a variable. The thing is, whether the user chooses button 1 (like) or button 2 (dislike) the value is always returned as like. Can anyone help me figure out why dislike isn't being returned?

 

Here is my form.php:

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!

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){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
        var ajaxDisplay = document.getElementById('ajaxDiv');
        ajaxDisplay.innerHTML = ajaxRequest.responseText;
    }
}
var entered = document.getElementById('entered').value;
var queryString = "?entered=" + entered;
ajaxRequest.open("GET", "check.php" + queryString, true);
ajaxRequest.send(null); 
}

//-->
</script>

<form name="myform" action="check.php" method="post">
        <fieldset>
        <legend>Posts</legend>

<div id="post_1" class="post">
    <b>Post #1</b><br>
    Content of post #1<br>
    <p><input type="radio" id="entered" name="like_1" value="like" onclick="ajaxFunction();" onchange="ajaxFunction();" /><label for="like1a">Like</label></p> <p><input type="radio" id="entered" name="like_1" value="dislike" onclick="ajaxFunction();" onchange="ajaxFunction();" /><label for="like1b"> Dislike</label></p>
</div>
</fieldset>
</form>
<div id='ajaxDiv'>Your result will display here</div>

 

and this is check.php:

<?php
// Retrieve data from Query String
$entered = $_GET['entered'];
// Escape User Input to help prevent SQL Injection
$entered = mysql_real_escape_string($entered);
echo $entered;
?>

 

So basically $entered is only storing "like" no matter which radio button is selected and changing the selection should change the value stored, but that doesn't happen either. Am I missing something?

Link to comment
Share on other sites

FYI, this is my first attempt at ajax so be kind....

 

For more info:

 

The goal is to have two choices: Like and Dislike for each post in a  blog. The value of this choice is to be store in a database so that when the user returns, his choices are recalled into those same radio buttons. The reason? The user can then choose to hide all liked and/or hide all disliked posts.

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.