更新单选按钮值而不提交按钮

问题描述 投票:1回答:1

我有更新无线电值的问题。当我使用提交按钮提交表单时,它工作正常,但是当我使用javascript时,数据库中的值不会更新。我正在使用jquery mobile。有人可以帮忙吗?谢谢!

更新的代码,现在只有前3个单选按钮正在工作

为每个任务动态生成3个单选按钮:

<?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:

$(document).ready(function() {
$('input[type="radio"]').click(function(){
    if ($(this).is(":checked"))
    $('form#formnobtn').submit();
});
})

更新数据库:

<?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');
}
?>
php javascript jquery-mobile radio-button
1个回答
0
投票

请试试这个: -

我为单选按钮检查值创建了一个隐藏值。它会在表格帖子上发送单选按钮检查值。我不知道为什么你在这里使用id这就是为什么我在你的PHP代码中创建一个$ radio_checked_id变量。我希望这会对你有所帮助。

为每个任务动态生成3个单选按钮:

<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:

$(document).ready(function() {
$('input[type="radio"]').click(function(){
  var checked_radio_id = $(this).val();
      $('#checked_id').val(checked_radio_id);
    }
$('form#formnobtn').submit();
});
})

更新数据库:

<?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!";
}
}
?>

我不确定以下行是否正确,'submit'实际上是提交按钮的名称,当有提交按钮时,但我不知道如何纠正它...

$submit = $_POST['submit'];
© www.soinside.com 2019 - 2024. All rights reserved.