mcfmullen Posted September 20, 2011 Share Posted September 20, 2011 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.