Reputation: 413
I have several forms with radio buttons on a page. My goal is to have the database updated immediately on the click of a radio button. Here is what I have put together so far, but I'm not sure where to go from here.
<? 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)); ?>
Am I anywhere close? Can someone help me through this?
Upvotes: 0
Views: 957
Reputation: 1544
This is untested, but you'd probably want to go with something like this (AJAX + jQuery). Would need quite a bit added to it to pass all your variables through properly.
$(":radio").click(function(){
$("#result").html(ajax_load);
$.post(
loadUrl,
{myvariable: "myvalue", myothervariable: "myothervalue"},
function(responseText){
$("#result").html(responseText);
},
"html"
);
});
Here's a good, supplementary read for you as well: 5 Ways to Make Ajax Calls with jQuery
Upvotes: 2
Reputation: 18595
Bind an ajax onclick event to send the command to your action script.
Inline php is not the way to go.
Upvotes: 0