I want to get radio button values and send them through AJAX to PHP.
My AJAX is running but is currently inserting a 0 in each row, so it's not picking up the value from the radio button. Any help would be appreciated.
$("#save_privacy").submit(function() {
var message_pri = $("#message_pri").val();
var follow_pri = $("#follow_pri").val();
var post_pri = $("#post_pri").val();
var check7 = $("#check7").val();
var s = {
"message_pri": message_pri,
"follow_pri": follow_pri,
"post_pri": post_pri,
"check": check7
}
$.ajax({
url: 'edit_check.php',
type: 'POST',
data: s,
beforeSend: function() {
$(".privacy_info").html("<img src=\"style/img/ajax/load2.gif\" alt=\"Loading ....\" />");
},
success: function(data) {
$(".privacy_info").html(data);
}
});
return false;
});
<form id="save_privacy">
<table align="center" width="70%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td style="padding: 5px;">
<b>Message Buttun: </b>
</td>
<td style="padding: 5px;">
<input type="radio" id="message_pri" name="message_pri" value="1" /> ON
<input type="radio" id="message_pri" name="message_pri" value="2" /> OFF
<input type="radio" id="message_pri" name="message_pri" value="3" /> FOLLOWERS
</td>
</tr>
<tr>
<td style="padding: 5px;">
<b>Follow Buttun: </b>
</td>
<td style="padding: 5px;">
<input type="radio" id="follow_pri" name="follow_pri" value="1" /> ON
<input type="radio" id="follow_pri" name="follow_pri" value="2" /> OFF
</td>
</tr>
<tr>
<td style="padding: 5px;">
<b>Who Can Post On Your Profile: </b>
</td>
<td style="padding: 5px;">
<input type="radio" id="post_pri" name="post_pri" value="1" /> Evry one
<input type="radio" id="post_pri" name="post_pri" value="2" /> Followers
</td>
</tr>
<tr>
<td colspan="2" style="padding: 5px;">
<input type="hidden" id="check7" value="save_privacy" name="check7" />
<input class="small color blue button" type="submit" value="Save" />
</td>
</tr>
</table>
<div class="privacy_info"></div>
</form>
Firstly you have a lot of duplicated id attributes, which is incorrect. Use classes instead, then use the :checked selector to get the specific instance of the radio which was selected.
Try this:
<input type="radio" class="message_pri" name="message_pri" value="1" /> ON
<input type="radio" class="message_pri" name="message_pri" value="2" /> OFF
<input type="radio" class="message_pri" name="message_pri" value="3" /> FOLLOWERS
var message_pri = $(".message_pri:checked").val();
And so on for your other radio inputs.
dont use id two time first thing
now for selected value of radio box use
$("input:radio[name=post_pri] :selected").val();
I would like to add that it is best to use the onChange event instead of the onClick event on the radio fieldset button call to the AJAX function. I am not sure why, but in this case it posts the correct value every time. When using the onClick event it sometimes posts a value different from the checked value. It is not much but it will definitely save somebody somewhere from a slight headache.
Example of the radion button group:
<fieldset **onChange="return rating_score()"** id="rating_selectors" data-
role="controlgroup" data-type="horizontal">
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"1"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_0"
value="1" />
<label title="1" for="ratings_0"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"2"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_1"
value="2" />
<label title="2" for="ratings_1"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"3"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_2"
value="3" />
<label title="3" for="ratings_2"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"4"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_3"
value="4" />
<label title="4" for="ratings_3"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"5"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_4"
value="5" />
<label title="5" for="ratings_4"></label>
</fieldset>
Example of the AJAX function:
<script type="text/javascript">
function rating_score ( txt_rating )
{ $.ajax( { type : "POST",
data: {"txt_captcha" : $("#txt_captcha").val(), "txt_rating" :
$("input[name=rating]:checked").val()},
url : "functions/reviewrater.php",
success : function (txt_rating)
{
$('#rating-container').load(document.URL + ' #rating-container');
$('span.stars').stars();
},
error : function ( xhr )
{ alert( "error" );
}
} );
return false;
}
</script>
Quick explanation:
The onChange event in the fieldset sends the value of the checked radio button to the AJAX function. I have added validation for the rest of the elements on the page, so the <?php if (!(strcmp($row_rs_new_rating['rating_value'],"3"))) {echo "checked=\"checked\"";} ?> compares the value stored in the rating session and retrieves the value again, so the user does not have to click on the rating again once they are warned that some of the other elements are empty. It looks much more complicated than it is, but all I actually wanted to say was to use the onChange instead of the onCLick event. :-)
You can visit this page to see the above code in action:
Rental Property Reviews Page
Try this use serialize function check serialize here
$("#save_privacy").submit(function(){
var serialise = $("#save_privacy").serialize();
$.ajax({
url:'edit_check.php',
type:'POST',
data:serialise,
beforeSend: function (){
$(".privacy_info") .html("<img src=\"style/img/ajax/load2.gif\" alt=\"Loading ....\" />");
},
success:function(data){
$(".privacy_info") .html(data);
}
});
return false;
});
Related
I have multiple radio buttons differentiated by their name, here is my script
<?php
if(mysqli_num_rows($result)>0){
while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['fullname'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['class'];?></td>
<td><input type="radio" value="present" name="<?php echo($row['id']); ?>" checked></td>
<td><input type="radio" value="absent" name="<?php echo($row['id']); ?>"></td>
</tr>
<?php }
}
?>
I want to pass their value to a php script, how I can pass each radio group value, Say if mysql query returned 10 rows then there are 10 radio button groups and I need to send all values of radio buttons.
And my ajax call is
$("#submitAttendance").click(function(){
$.ajax({
url: 'teacher.php',
method: 'post',
data:
});
});
You can use $(form_selector).serialize() that serializes all the values of the form and send to your server as an intended input.
$("#submitAttendance").click(function(){
$.ajax({
url: 'teacher.php',
method: 'post',
data: $(form_selector).serialize()
});
});
Also, make sure you write this code in:
$(form_selector).submit()
Rather than attaching an event handler to the submit button.
So, a typical output of your form, say like this:
<form action="">
<div>
<label><input type="radio" name="student-1" id="" value="present"> Present</label>
<label><input type="radio" name="student-1" id="" value="absent"> Absent</label>
</div>
<div>
<label><input type="radio" name="student-2" id="" value="present"> Present</label>
<label><input type="radio" name="student-2" id="" value="absent"> Absent</label>
</div>
<div>
<label><input type="radio" name="student-3" id="" value="present"> Present</label>
<label><input type="radio" name="student-3" id="" value="absent"> Absent</label>
</div>
<div>
<label><input type="radio" name="student-4" id="" value="present"> Present</label>
<label><input type="radio" name="student-4" id="" value="absent"> Absent</label>
</div>
<div>
<label><input type="radio" name="student-5" id="" value="present"> Present</label>
<label><input type="radio" name="student-5" id="" value="absent"> Absent</label>
</div>
</form>
Would be:
"student-1=absent&student-2=present&student-3=absent&student-4=present&student-5=absent"
Is this what you are looking for? And you can get this using PHP using:
$_POST["student-1"] // absent
$_POST["student-2"] // present
$_POST["student-3"] // absent
$_POST["student-4"] // present
$_POST["student-5"] // absent
I am trying to refresh a page via CodeIgniter, but I can't seem to get this to work.
Basically, we have a list of jobs on a page and a user has the option to filter the said jobs.
So, a user will select checkboxes and then click save to save their selections to a database based on their user id.
I will obviously need the data to refresh once they have saved their selection, but I have tried many things and this doesn't seem to work at all. I assume I've got the redirect in the wrong place, which is currently in the controller.
I've tried the following:
$this->load->helper('url');
redirect('dashboard', 'refresh');
But nothing happens.
I have the above redirect in the controller function:
public function save_filters() {
if (isset($_POST)){
$filters = $_POST['client_ids'];
}
$this->load->model('dashboard_model');
$this->dashboard_model->update_dashboard_filters($filters);
$this->load->helper('url');
redirect('dashboard', 'refresh');
}
Did I put it in the wrong place?
I get no errors or warnings from firebug?
The form:
<form id="filters_form">
<table>
<tr>
<td>
<label for="GAB Robins">GAB Robins </label><input type="checkbox" name="client_ids[]" value="4">
</td>
<td>
<label for="Cunningham Lindsay">Cunningham Lindsay </label><input type="checkbox" name="client_ids[]" value="5">
</td>
<td>
<label for="Lloyds">Lloyds </label><input type="checkbox" name="client_ids[]" value="7">
</td>
<td>
<label for="Sundry/Private">Sundry/Private </label><input type="checkbox" name="client_ids[]" value="9">
</td>
<td>
<label for="WNS Ltd">WNS Ltd </label><input type="checkbox" name="client_ids[]" value="4441">
</td>
<td>
<label for="Stream">Stream </label><input type="checkbox" name="client_ids[]" value="4493">
</td>
<td>
<label for="Redesign">Redesign </label><input type="checkbox" name="client_ids[]" value="5295">
</td>
</tr>
</table>
<input type="button" value="Save selections" name="save_filters" id="save_filters_button" />
</form>
The javascript that deals with the form
save_filter: function () {
$.post('/dashboard/save_filters', $('#filters_form').serialize(), function (response) {
if (response.status == 'ok') {
if ($('.ui-success').length == 0) {
$('#page_main').before('<div class="ui-success" style="margin-bottom: 1em; font-weight: bold; border-bottom: 1px solid #ccc; padding: 1em;"><img src="/common/images/icons/accept.png" style="margin-right: 10px;" align="absmiddle" />Filters saved!</div>').fadeIn(1000);
}
}
else {
$(response.errors).each(function (i, item) {
alert(item);
});
}
});
}
Put the action in form
action="<?php base_url(); ?>controller_name/function_name" and method="post"
Example:
<form id="filters_form" action="<?php base_url(); ?>controller_name/function_name" method="post">
UPDATE: Because you are using JS, you are not refreshing page. Don't use JS if you want to refresh the page.
Its not working because you have an Ajax request, and you cant make redirect on server before you return data to JavaScript that made that request.
you can make redirect based on your response
You're setting up some attribute values for an object, such as colour, size, weight etc in a form. If using buttons to specify these values before wishing to post all the information to a php page for further processing - how do you get the values passed to php, if the buttons are not in themselves submitting the form?
For example:
<form action="processgivenvalues.php" method="post">
choose colour: <button type="button" name="colour" value="green"></button>
<button type="button" name="colour" value="blue"></button>
choose size: <button type="button" name="size" value="big"></button>
<button type="button" name="size" value="small"></button>
<input type="submit">
and on php page:
<?php
echo You specified:
echo $size;
echo $frame
?>
Many thanks.
The point of a type="button" is to be a thing you can hook JavaScript up to. It isn't designed to send values to the server.
Submit buttons are, but you want to let the user pick from multiple sets rather then just one, so you can't use those either.
While you use buttons with JavaScript that generates/updates hidden inputs with the values you want, you really should just use radio buttons instead. They are designed to let users pick one item from a group.
<form action="processgivenvalues.php" method="post">
<fieldset>
<legend>Colour</legend>
<label>
<input type="radio" name="colour" value="green"> Green
</label>
<label>
<input type="radio" name="colour" value="blue"> Blue
</label>
</fieldset>
<fieldset>
<legend>Size</legend>
<label>
<input type="radio" name="size" value="big"> Big
</label>
<label>
<input type="radio" name="size" value="small"> Small
</label>
</fieldset>
<input type="submit">
</form>
You could use jQuery for this.
html:
choose colour: <button type="button" class="colour" value="green">Green</button>
<button type="button" class="colour" value="blue">Blue</button>
choose size: <button type="button" class="size" value="big">Big</button>
<button type="button" class="size" value="small">Small</button>
<button id='submit' type="button">Submit</button>
jQuery:
You make the form on the run
$(document).ready(function(){
window.size = '';
window.colour = '';
$(".colour").click(function() {
window.colour = $(this).val();
});
$(".size").click(function() {
window.size = $(this).val();
});
$("#submit").click(function() {
if(window.size == '' || window.colour == ''){
return alert('Choose colour and Size');
}
var form = $('<form></form>');
$(form).hide().attr('method','post').attr('action',"processgivenvalues.php");
var input1 = $('<input type="hidden" />').attr('name',"size").val(window.size);
var input2 = $('<input type="hidden" />').attr('name',"colour").val(window.color);
$(form).append(input1);
$(form).append(input2);
$(form).appendTo('body').submit();
});
});
jsFiddle
This can also be done with only javaScript.
As Quentin stated, buttons are not useful for selecting between options, but you comment that you want something customizable with an image...
Option 1: Use <label for="id"></label> with a radio button in a table or list.
<table>
<tbody>
<tr>
<td style="background-color: green;">
<label for="green">
<input name="colour" value="green" id="green" type="radio">
</label>
</td>
</tr>
<tr>
<td style="background-color: blue;">
<label for="blue">
<input name="colour" value="blue" id="blue" type="radio">
</label>
</td>
</tr>
<tr>
<td>
<label for="big">
<input name="size" value="big" id="big" type="radio">
<big>Big</big>
</label>
</td>
</tr>
<tr>
<td>
<label for="small">
<input name="size" value="small" id="small" type="radio">
<small>Small</small>
</label>
</td>
</tr>
</tbody>
</table>
Option2: Use droplists with styling:
<select name="colour2">
<option value="0">Select a color</option>
<option value="green" style="background: green;"> </option>
<option value="blue" style="background: blue;"> </option>
</select>
<br>
<select name="size2">
<option value="0">Select a size</option>
<option value="big" style="font-size: 16px;">Big</option>
<option value="small" style="font-size: 12px;">Small</option>
</select>
Here's a demo fiddle I created (just signed up)
I have form that has 2 radio buttons(Yes and No) and a text box. If the user clicks Yes it enables the text box you can input information and it is uploaded to the database including the value from the radio buttons. If you click no it disables the text box and suppose to upload the value of the radio box only to the database. But I am not getting that.
<input type="radio" name="TermLease" value="No" onclick="TermLeaseMonths.disabled=true">No
<input type="radio" name="TermLease" value="Yes" onclick="TermLeaseMonths.disabled=false">Yes |
How many months:<input type="hidden" name="TermLeaseMonths" value="0" />
<input type="text" name="TermLeaseMonths" id="TermLeaseMonths" size="1" disabled="true">
I have a hidden input type that uploads the value. But when I click yes it does not disable the text box. Not sure where I am going wrong.
You forgot give id for the text field. Try this
<input type="radio" name="TermLease" value="No" onclick="TermLeaseMonths.disabled=true">No
<input type="radio" name="TermLease" value="Yes" onclick="TermLeaseMonths.disabled=false">Yes |
How many months:<input type="hidden" name="TermLeaseMonths" value="0" />
<input type="text" name="TermLeaseMonths" id="TermLeaseMonths" size="1" disabled="true">
Try this on click of radio button we can do by this way
$('#radio').click(function () {
if (("#radio").val() == "") {
$('class_name_oftextBox').attr("disabled", true);
} else {
$('class_name_oftextBox').removeAttr("disabled");
}
});
Or you can do this as
$('#enable').click(function () {
$('#textBox').removeAttr("disabled")
});
$('#disable').click(function () {
$('#textBox').attr("disabled", "disabled");
});
Demo jsFiddle
HTML
<form>
<span style="float: left;">
<label><input type="radio" name="TermLease" value="No" onclick="ShowHideTextbox('leaseMonths', false)" />No</label>
<label><input type="radio" name="TermLease" value="Yes" onclick="ShowHideTextbox('leaseMonths', true)"/>Yes</label>
</span>
<span id="leaseMonths" style="display: none; float: left;">
<span> | </span> <span>How many months: </span>
<input type="text" name="TermLeaseMonths" id="TermLeaseMonths" size="1" />
</span>
</form>
JS
var previousNumberOfMonths = "0";
function ShowHideTextbox(elementId, show) {
var link = document.getElementById(elementId);
var textbox = document.getElementById("TermLeaseMonths");
if (show){
textbox.value = previousNumberOfMonths;
link.style.display = 'block';
}
else {
link.style.display = 'none';
previousNumberOfMonths = textbox.value;
document.getElementById("TermLeaseMonths").value = "0";
}
}
I have been creating a website to allow users to rate an image uploaded by another user. I have my code working great. The user is presented with the image, a button to flag the image, 10 radio buttons to choose their rating and a submit button (all within the same form).
My problem is that I would like to remove the submit button and process the form when the user clicks the radio button. The problem with this is that the flag button (image button) is also submitting the form. My code is below:
HTML
<form name="ratebox" action="Box.php?bid=<?php echo $boxId ?>" method="post">
<table style="margin: 0 auto;">
<tr>
<td colspan="2"><img src="img/boxes/<?php echo $boxId ?>.png" title="<?php echo $boxName ?>" height="350" width="350"></td>
</tr>
<tr>
<input
type="image"
name="flag_submit"
src="img/Flag.png"
onmouseover="this.src='img/Flag_Click.png'"
onmouseout="this.src='img/Flag.png'"
height="30"
width="30"
/>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center;">
1<input type="radio" name="rdoRate" value="1" >
2<input type="radio" name="rdoRate" value="2" >
3<input type="radio" name="rdoRate" value="3" >
4<input type="radio" name="rdoRate" value="4" >
5<input type="radio" name="rdoRate" value="5" >
6<input type="radio" name="rdoRate" value="6" >
7<input type="radio" name="rdoRate" value="7" >
8<input type="radio" name="rdoRate" value="8" >
9<input type="radio" name="rdoRate" value="9" >
10<input type="radio" name="rdoRate" value="10" >
</td>
</tr>
<tr><td colspan='4' align='center'><input type='submit' name='rate_submit' value='Rate'></td></tr>
</table>
</form>
PHP
if (isset($_POST['flag_submit_x']))
{
//Code to process the flagged image
}
if (!empty($_POST['rate_submit']))
{
//Code to process the rated image
}
Is there any way I can submit the form when a radio button is pressed and retrieve the value of the radio button that has been pressed?
Yes, you need to use Javascript for that. Using jQuery,
$('input[type=submit]').click(function(){
return false;
});
$('input[type=radio]').click(function(){
$('form').submit();
});
You need to learn how to use $.ajax() to transfer info to and from PHP. Read this.
Sure there is, why no attach a js function to each button and have it send the value to the server either ajax or regular.
function submitRate(image_id, rating){
//here you would do the magic:)
//maybe grab other form fields or just the rating
}
and in your buttons onclick="submitRate()"
1) remove the submit input.
2) make the form submit false:
<form id="ratebox" name="ratebox" action="" method="" onSubmit="return false;">
3)Put post code to the submit page:
$.post("Box.php?bid=<?php echo $boxId ?>", $("#ratebox").serialize());