点击收音机立即更新数据库

时间:2012-10-02 21:23:57

标签: php forms onclick radio

我在页面上有几个带单选按钮的表单。我的目标是通过单击单选按钮立即更新数据库。这是我到目前为止所放在一起的内容,但我不确定从哪里开始。

<? include 'dbconnect.inc.php';?>
<script type="text/javascript">function do_submit(){document.forms['decision'].submit();}</script>

<? $result = $mysqli->query("SELECT * FROM items");
   while( $row = $result->fetch_assoc() ){ ?>
   <form name='decision' method='post' action='action.php'>
       <label>Keep:</label><input type='radio' name='dec' value='keep' onchange='do_submit()' <? if($row['Dec1']=='keep'){echo "checked='checked'";} ?> >
       <label>Donate:</label><input type='radio' name='dec' value='donate' onchange='do_submit()' <? if($row['Dec1']=='donate'){echo "checked='checked'";} ?> >
       <label>Sell:</label><input type='radio' name='dec' value='sell' onchange='do_submit()' <? if($row['Dec1']=='sell'){echo "checked='checked'";} ?> >
       <label>Trash:</label><input type='radio' name='dec' value='trash' onchange='do_submit()' <? if($row['Dec1']=='trash'){echo "checked='checked'";} ?> >
       <label>Give To:</label><input type='text' name='dec' size='15' onchange='do_submit()' <? if($row['Dec1']!='keep' || $row['Dec1']!='donate' || $row['Dec1']!='sell' || $row['Dec1']!='trash'){echo $row['Dec1'];} ?> >
       <input type='hidden' name='Id' value='<? echo $row['Id']; ?>' />
   </form>
   <? } ?>

action.php的

<? $mysqli->query("UPDATE items SET  Dec1 =  '{$_POST['dec']}' WHERE Id = '{$_POST['$Id']}'") or die(mysqli_error($mysqli)); ?>

我在附近吗?有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

这是未经测试的,但您可能想要使用类似的东西(AJAX + jQuery)。需要相当多的添加才能正确传递所有变量。

$(":radio").click(function(){  
    $("#result").html(ajax_load);  
    $.post(  
        loadUrl,  
        {myvariable: "myvalue", myothervariable: "myothervalue"},  
        function(responseText){  
            $("#result").html(responseText);  
        },  
        "html"  
    );  
}); 

这也是一本很好的补充阅读材料:5 Ways to Make Ajax Calls with jQuery

答案 1 :(得分:0)

绑定ajax onclick事件以将命令发送到您的操作脚本。

内联php不是最佳选择。