Jump to content

Send radio button value to MySQL


mcfmullen

Recommended Posts

For the life of me, I can't get this code to work. I have a pair of radio buttons attached to each post. The user selects one of the two radio buttons to choose whether he likes or dislikes it. I am not using checkboxes because I have switches that hide all liked or hide all disliked posts. Anyway, I have the following code which DOES work but only displays the value of the radio button in a hidden div below the each post. Each post is pulled from my database..

 

<?php
$data = mysql_query("SELECT * FROM Contests");

while($row = mysql_fetch_array( $data )){
?>
<script type="text/javascript">
    $(document).ready(function() {
        $("input[name*='pref_<?php echo $row['contestID']; ?>']").click(function() {
            var defaultValue = $("label[for*='" + this.id + "']").html();
            var defaultm = "You have chosen : ";
            $('#Message_<?php echo $row['contestID']; ?>').html('').html(defaultm + defaultValue + ' | Value is : ' + $(this).val());

        });

    });
</script>

<div id="contest_<?php echo $row['contestID']; ?>" class="post">
<div id="contest_<?php echo $row['contestID']; ?>_inside">
    <b><?php echo $row['Title']; ?></b><br>
    Expires: <?php echo $row['Exp']; ?><br>
    <ul id="listM"></ul>
    <form id="form_<?php echo $row['contestID']; ?>" method="post">  
        <fieldset> 
            <div class="left"><p><input id="like_<?php echo $row['contestID']; ?>" type="radio" name="pref_<?php echo $row['contestID']; ?>" value="1" />
            <label for="like_<?php echo $row['contestID']; ?>">Like</label></p></div>
            <div class="right"><p><input id="dislike_<?php echo $row['contestID']; ?>" type="radio" name="pref_<?php echo $row['contestID']; ?>" value="0" />
            <label for="dislike_<?php echo $row['contestID']; ?>">Dislike</label></p></div>
                <hr />
        </fieldset>  
    </form>  
</div>
</div>
<div id="Message_<?php echo $row['contestID']; ?>"></div>

<?php 
} 
?>

 

 

The following code DOES NOT work. It is my failed attempt to use ajax to send the radio button value over to check.php for insertion in my database. I'd also like a message to display inside my #Message div to confirm success of database insertion. Unfortunately, nothing happens with m code:

 

<?php
$data = mysql_query("SELECT * FROM Contests");

while($row = mysql_fetch_array( $data )){
?>
<script type="text/javascript">
    $(document).ready(function() {
        $("input[name*='pref_<?php echo $row['contestID']; ?>']").click(function() {
            var defaultValue = $("label[for*='" + this.id + "']").html();
            var defaultm = "You have chosen : ";

var contestID     = <?php echo $row['contestID']; ?>;  
var value     = $('input[name=pref_<?php echo $row['contestID']; ?>]:checked').val()   
    $.ajax({  
        type: "POST",  
        url: "check.php",  
        data: "userID="+ userID +"& contestID="+ contestID +"& value="+ value,  
        success: function(){  
            $('#Message_<?php echo $row['contestID']; ?>').html('').html(defaultm + defaultValue + ' | Value is : ' + $(this).val()); }

        });

    });
</script>

<div id="contest_<?php echo $row['contestID']; ?>" class="post">
<div id="contest_<?php echo $row['contestID']; ?>_inside">
    <b><?php echo $row['Title']; ?></b><br>
    Expires: <?php echo $row['Exp']; ?><br>
    <ul id="listM"></ul>
    <form id="form_<?php echo $row['contestID']; ?>" method="post">  
        <fieldset> 
            <div class="left"><p><input id="like_<?php echo $row['contestID']; ?>" type="radio" name="pref_<?php echo $row['contestID']; ?>" value="1" />
            <label for="like_<?php echo $row['contestID']; ?>">Like</label></p></div>
            <div class="right"><p><input id="dislike_<?php echo $row['contestID']; ?>" type="radio" name="pref_<?php echo $row['contestID']; ?>" value="0" />
            <label for="dislike_<?php echo $row['contestID']; ?>">Dislike</label></p></div>
                <hr />
        </fieldset>  
    </form>  
</div>
</div>
<div id="Message_<?php echo $row['contestID']; ?>"></div>

<?php 
} 
?> 

 

So, does anyone have any idea what's wrong with my code? I know my code is wrong for the success message as I want the message to display the text echoed in check.php My check.php file simply echos the $_POST variables to confirm that they have been passed. Your help is greatly appreciated!

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.