1

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">&nbsp;</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">&nbsp;</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">&nbsp;</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');
}
?>
pangpp
  • 155
  • 3
  • 12
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jan 14 '13 at 11:14

2 Answers2

0

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">&nbsp;</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">&nbsp;</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">&nbsp;</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'];
Roopendra
  • 7,674
  • 16
  • 65
  • 92
  • the form is not submitted in this way... I tried deleting '$submit = $_POST['submit'];', but only the first 3 radio buttons are working, please see my update. – pangpp Jan 15 '13 at 15:57
  • and I use id for updating database '$query= "UPDATE plan SET state='$state' WHERE id='$id'";' . – pangpp Jan 15 '13 at 17:17
  • I think you are facing this problem because you are using same form id and name multiple time in a one page. as per your old code I have test this with one form.If two row available then it will generate two form s – Roopendra Jan 15 '13 at 17:27
  • 1
    yes, I think that might be the point, but still no luck. I'm not sure if I wrote these right, html: `
    "...>`; then in javascript: `$('[id^="formnobtn"]')...;`
    – pangpp Jan 17 '13 at 17:00
0

well, with radio buttons you do not need to add the id to the all, just make sure the names are the same. something like this :

<form id="formnobtn" action="nobtn.php" method="POST" data-ajax="false">
 <input type="radio" name="r" id="id" value="A" class='custom' data-theme="a" <?php if ($statE == 'A'): ?>checked='checked'<?php endif; ?>><label for="rA">&nbsp;</label>
 <input type="radio" name="r" value="B" class='custom' data-theme="c" <?php if ($statE == 'B'): ?>checked='checked'<?php endif; ?>><label for="rB">&nbsp;</label>
 <input type="radio" name="r" value="C" class='custom' data-theme="f" <?php if ($statE == 'C'): ?>checked='checked'<?php endif; ?>><label for="rC">&nbsp;</label>
</form>

So just remove the hidden input, and in javascript:

 $('input[type="radio"]').change(e=>{
  $('form').submit();
})

Another option is to change the radio buttons into regular buttons and create an active class to show the selected CSS something like:

<form ....>
   <input type="hidden" name="id" data-group="x" value="<? echo "$id";?>">
   <button <?php if ($statE == 'A'): ?>class='active'<?php endif; ?> data-value="A" data-group="x">A</button>
   <button <?php if ($statE == 'B'): ?>class='active'<?php endif; ?> data-value="B" data-group="x">B</button>
   <button <?php if ($statE == 'C'): ?>class='active'<?php endif; ?> data-value="C" data-group="x">C</button>
</form>

and on submit:

$('button[data-value]').click(e=>{
  e.preventDefault();
  $(`input[data-group="${e.target.dataset.group}"]`).val(e.target.dataset.group);
 $('form').submit();
})