0

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

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

动作.php

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

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

4

2 回答 2

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

于 2012-10-02T21:30:07.510 回答
0

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

内联php不是要走的路。

于 2012-10-02T21:25:02.543 回答