Immediately update database on click of radio - php

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?

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

Bind an ajax onclick event to send the command to your action script.
Inline php is not the way to go.

Related

Automatic marking

Am puzzled about how to make a system that can automatically mark.
Am using a variable $count in PHP and is the one storing data from the checked radio button.
I have not tried to check on how to do it and am just afraid of how to do it.
I have the following codes which out puts the data in radio buttons
<?php
$get=mysql_query("select * from courses where courseid='".$cid."'");
while ($picked=mysql_fetch_array($get)) {
echo "<b>The questions for ".$picked['coursename']." are:</b><br><br>";
}
$query=mysql_query("select * from papers where cid='".$cid."'");
if (mysql_num_rows($query)>0) {
$count=1;
while ($row=mysql_fetch_array($query)) {
echo "Qn ".$count.". ".$row['question']."<br><input type='radio' name='".$count."' value='A'>".$row['A']."<br><input type='radio' name='".$count."' value='B'>".$row['B']."<br><input type='radio' name='".$count."' value='C'>".$row['C']."<br><input type='radio' name='".$count."' value='D'>".$row['D']."<br><br>";
$count++;
}
}else{
echo "No papar has been set yet for this paper.";
}
?>
<input type="submit" name="but" value="Submit Assessment">
and it outputs the following:
And the database is like:

Send form when with mysql_fetch_array while loop CLOSED

I have got a while loop that runs through all the records of the database printing them on a table. Now i also have some checkboxes within that very loop that I want to use to submit a form when clicked. Now when I click the checkbox it will indeed submit the form thanks to a Jquery script I found, BUT whenever i submit it it submits with the ID of the first record of the table.This Image
shows the table, as you see the first record has ID 34. Now every checkbox I click will send the $id 34.
This does not happen with normal submit buttons.
Is there a way I can submit with the individual userID's
while ($openResInfo = mysql_fetch_array($openResQuery))
{
$id = $openResInfo[0];
$complete = $openResInfo[7];
?>
<form id='resComplete' action='dashboard_openReserveringen_PHP.php' method='GET'>
<?php
echo "<input type='hidden' name='userID' value='$id'>";
?>
<input type="hidden" name="complete" value="0" >
<input id='complete' type='checkbox' name='complete' value='1' onchange='$("#resComplete").submit();' <?php if($complete == 1){echo "checked";}?>>
</form>
I'm sorry if i'm not very clear with the explanation it is quite hard to explain this situation. Thank you guys!
Probably your problem is that you are submiting the same form always and its because you create a form for each row but it has the same id
For you the easy way is to put each form with the id cointaining the unique value of the row and doing submit with that.
Something like this
while ($openResInfo = mysql_fetch_array($openResQuery))
{
$id = $openResInfo[0];
$complete = $openResInfo[7];
?>
<form id='resComplete_<?php echo $id; ?>' action='dashboard_openReserveringen_PHP.php' method='GET'>
<?php
echo "<input type='hidden' name='userID' value='$id'>";
?>
<input type="hidden" name="complete" value="0" >
<input id='complete' type='checkbox' name='complete' value='1' onchange='$("#resComplete_<?php echo $id; ?>").submit();' <?php if($complete == 1){echo "checked";}?>>
</form>
It looks like the <form> your creating has a static id, so ALL forms will have id='resComplete'. The jQuery submit function will grab the first element with id='resComplete' and submit it. You need to make it unique for every form and make the onchange='$("#resComplete").submit();' code match it.
Eg.
<?php
while ($openResInfo = mysql_fetch_array($openResQuery))
{
$id = $openResInfo[0];
$complete = $openResInfo[7];
?>
<form id='resComplete-<?php echo $id; ?>' action='dashboard_openReserveringen_PHP.php' method='GET'>
<?php
echo "<input type='hidden' name='userID' value='$id'>";
?>
<input type="hidden" name="complete" value="0" >
<input id='complete' type='checkbox' name='complete' value='1' onchange='$("#resComplete-<?php echo $id; ?>").submit();' <?php if($complete == 1){echo "checked";}?>>
</form>
Better yet, use jQuery to find out what form it's in by chaning the onchange to something like:
<input id='complete' type='checkbox' name='complete' value='1' onchange='$(this).closest('form').submit();' <?php if($complete == 1){echo "checked";}?>>

php code to make a fieldset invisible in html page

I have a fieldset in the html page.I need the field set to be invisible during a specific radio button click.
$cat is the radiobutton name,Dessert is the value.This radio button is inside the field set
The following code does not work.
How to do this through php code.I don't need to be done with javascript or jquery.
Here is the Code :
<?
if((!empty($cat) && $cat==="Dessert"))
{
echo "<fieldset id='typeradio' hidden='hidden'>";
echo"success";
} >?
How can i do this ?
You can disable it if you don't want it to work
<fieldset id="typeradio" disabled>
or hide it using CSS
<fieldset id="typeradio" style="display: none;">
Try This
It will only work if you post back your page and set the $cat property there otherwise you will have to do it via Jquery or JavaScript or ajax request. By post back i mean full page reload and server will do all checks and apache server will generate Html for this if condition
<?
if((!empty($cat) && $cat==="Dessert"))
{
echo "<fieldset id='typeradio' style='display:none'>";
echo"success";
} >?
//this example.php
<?
if(isset($_POST['opt'])){
$cat=$_POST['opt'];
if($cat==="Dessert")
{
echo "<form name="frm" action='example.php' method="post"><fieldset id='typeradio' hidden='hidden'><input type='radio' name='opt' value="Dessert" onclick="this.form.submit();"></fieldset></form>";
echo"success";
}
}
else
{
echo "<form name="frm" action='example.php' method="post"><fieldset id='typeradio' ><input type='radio' name='opt' value="Dessert" onclick="this.form.submit();"></fieldset></form>";
}
>?
//there was some quotes mistakes in previous code try this
<?php
if(isset($_POST['opt'])){
$cat=$_POST['opt'];
if($cat==="Dessert")
{
echo "<form name='frm' action='example.php' method='post'><fieldset id='typeradio' hidden='hidden'><input type='radio' name='opt' value='Dessert' onclick='this.form.submit();'></fieldset></form>";
echo"success";
}
}
else
{
echo "<form name='frm' action='' method='post'><fieldset id='typeradio' ><input type='radio' name='opt' value='Dessert' onclick='this.form.submit();'></fieldset></form>";
}
?>

Need javascript confirm box dialogue & delete images when press ok

<?php
require "../db/dbconfig.php";
$gal=mysql_query("select * from gallery");
$numrows=mysql_num_rows($gal);
if($numrows>=1)
{
echo "<form action='delete.php' method='post' name='f2' id='f2'>";
echo '<table id="rqst" style="display:block;" border="0" cellpadding="12" cellspacing="3" width="500px">';
echo "<tr><th>Tick to select</th><th>Images in Gallery</th></tr>";
while($row=mysql_fetch_array($gal)
{
$imgfile=$row['ImgName'];
$Image="<img src=gallery/".$imgfile." width='230px' height='150px'/>";
$img_name=$imgfile;
echo "<tr>";
echo "<td><input type='checkbox' name='imgs[]' value='$img_name'></td><td>".$Image."</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan='3' align='right'>";
echo "<input type='submit' value='Delete' name='del'></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
?>
This is my code....This will display images from gallery and checkboxes associated with them. When I click delete button with unchecked checkboxes an alert should come like this "Please check at least one checkbox"..How to do that??
My next problem is,,when I click delete button after checked checkbox, alert should come like this=" Do you want to delete?? "...If clicked Ok,the image must be deleted else do nothing...Please help ...Thanks in advance....
check this below link for validation using jquery:
http://jsfiddle.net/susheel61/U3Unk/2/
<form id="form">
First name: <input type="checkbox" name="firstname" class="check"><br>
<button>test</button>
</form>
$("button").on("click",function(e){
var status = $(".check").is(":checked");
e.preventDefault();
if(!status){
alert("this is not checked");
}
});
Yes, you can do it by javascript or jquery to validate whether your atleast one checkbox is select or not. So, for that you need to give a common class for all checkbox as example
<input type="checkbox" name="firstname" class="addchk">
Now in your submit button call a javascript function which validate the matter.
<input type='button' value='Delete' name='del' onclick='delete_checked()' />
Now write a function to validate whether any checkbox is selected.
function delete_checked()
{
var status = $(".addchk").is(":checked");
e.preventDefault();
if(!status){
alert("this is not checked");
}
else
{
// SUbmit yout form
}
}

Multiple Checkboxes with same name validator

I have this form with multiple checkboxes with same name and all of checkboxes are read from a database:
echo "<FORM action='check.php' method=POST>"; ?>
<div style='color:#414141; font-size:15px;'>
<?php
while($rows=mysql_fetch_array($result)){
?>
<input name="check[]" type="checkbox" id="checkk[]" value="<? echo $rows['ID']; ?>"><? echo $rows['checkboxname']; ?>
<BR>
<?php
}
echo "</div>";
echo "<input name='' type='submit' id='get' value='Next'>";
echo "</FORM>";
I need a form validation to the next file.When I click on Submit Button(Next),show me the next page only if I checked a checkbox.I preffer a javascript validation method.
You can add a click event listener for the submit button and check if atleast one checkbox is checked and submit if true.
Something like below should work,
Try below,
echo "<input name='' type='submit' id='get' value='Next' onclick='return validateCheckbox()'>";
<script>
function validateCheckbox () {
var checkboxes = document.getElementsByName('check[]'); //selected by name since OP wanted to select by name
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
return true; // found 1 checked checkbox
}
}
return false; //none checked, so cancel submit
}
</script>

Categories