I'm having problem to update radio values. When I use a submit button to submit the form, it works fine, but when I use javascript, the values are not updated in database. And I'm using jquery mobile. Can anybody help? thanks!
UPDATED CODE, now only the first 3 radio buttons are working
dynamically generates 3 radio buttons for each task:
<?php
include 'connection.php';
$query = "SELECT*FROM plan";
$result = mysql_query($query);
$num = mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$id=mysql_result($result, $i, "id");
$task=mysql_result($result, $i, "task");
$state=mysql_result($result, $i, "state");
?>
<form id="formnobtn" action="nobtn.php" method="POST" data-ajax="false">
<input type="hidden" name="id" value="<? echo "$id";?>">
<input type="radio" name="r" id="rA" value="A" class='custom' data-theme="a" <?php if ($statE == 'A'): ?>checked='checked'<?php endif; ?>><label for="rA"> </label>
<input type="radio" name="r" id="rB" value="B" class='custom' data-theme="c" <?php if ($statE == 'B'): ?>checked='checked'<?php endif; ?>><label for="rB"> </label>
<input type="radio" name="r" id="rC" value="C" class='custom' data-theme="f" <?php if ($statE == 'C'): ?>checked='checked'<?php endif; ?>><label for="rC"> </label>
<div data-role="collapsible" name="ud_c" value=" <?echo "$task";?> "><h3><?echo "$task";?></h3></div>
</form>
<?php
$i++;
}
?>
javascript which should submit the form on check of a radio button:
$(document).ready(function() {
$('input[type="radio"]').click(function(){
if ($(this).is(":checked"))
$('form#formnobtn').submit();
});
})
update database:
<?php
$id = $_POST['id'];
if (isset($_POST['r'])){
$state = $_POST['r'];
echo $state;
include 'connection.php';
$query= "UPDATE plan SET state='$state' WHERE id='$id'";
mysql_query($query);
mysql_close();
header('Location: nobtn.php');
}
?>
Please try this:-
I have created a hidden value for radio button checked value. It will send radio button checked value on form post. I dont know why you used id here that's why I am creating a $radio_checked_id variable in your php code.I hope this will help you.
dynamically generates 3 radio buttons for each task:
<form id="formnobtn" action="nobtn.php" method="POST" data-ajax="false">
<input type="hidden" name="id" value="<?php echo $id =(isset($id)) ? $id : '';?>">
<input type="radio" name="r" id="rA" value="A" class='custom' data-theme="a" <?php if ($statE == 'A'): ?>checked='checked'<?php endif; ?>><label for="rA"> </label>
<input type="radio" name="r" id="rB" value="B" class='custom' data-theme="c" <?php if ($statE == 'B'): ?>checked='checked'<?php endif; ?>><label for="rB"> </label>
<input type="radio" name="r" id="rC" value="C" class='custom' data-theme="f" <?php if ($statE == 'C'): ?>checked='checked'<?php endif; ?>><label for="rC"> </label>
<div data-role="collapsible" name="ud_c" value="<?php echo $task =(isset($task)) ? $task : '';?>"><h3><?php echo $task =(isset($task)) ? $task : '';?></h3></div>
<input type="hidden" name="checked_id" id="checked_id" value="">
</form>
javascript which should submit the form on check of a radio button:
$(document).ready(function() {
$('input[type="radio"]').click(function(){
var checked_radio_id = $(this).val();
$('#checked_id').val(checked_radio_id);
}
$('form#formnobtn').submit();
});
})
update database:
<?php
$submit = $_POST['submit'];
$id = $_POST['id'];
$radio_checked_id = $_POST['checked_id'];
if($submit){
if (isset($_POST['r'])){
$state = $_POST['r'];
echo $state;
include 'connection.php';
$query= "UPDATE plan SET state='$state' WHERE id='$id'";
mysql_query($query);
echo "Record Updated";
mysql_close();
header('Location: nobtn.php');
}
else {
echo "Please select a radio button!";
}
}
?>
I'm not sure if the following line is right, 'submit' is actually the name of submit button when there is a submit button, but I have no idea how to correct it..
$submit = $_POST['submit'];