Update from checkbox to wrong ID in php - php

issue for days now.
I have a form with multiple checkboxes, I will never know how many rows will be in the form, and I am aware that an empty checkbox will not post to the processing page. I have also tried providing a hidden value to the checkbox to no avail...
I know there is a straightforward method to this, please assist:
The following code is on the form:
echo "<td><center><input type='checkbox' checked='checked' name='jce_pnt_id[]' value='$jce_pnt_id' onclick='return false;' /><br>$jce_pnt_id</center></td>";
echo "<td title='Task Complete ?'><center><input type='hidden' name='tr_g[]' value='25'>";
if($jce_ins_complete < '100' || $jce_ins_act_complete == '0')
{
echo "<input type='checkbox' name='tr_g[]' id='$jce_pnt_id' value='100'>";
}
else
{
echo "<input type='checkbox' name='tr_g[]' id='$jce_pnt_id' value='100' checked='checked'>";
}
echo "</center></td>";
Here is a screenshot (look at the red circled column):
Here is the processing snippet (not working):
$jce_pnt_id = $_POST['jce_pnt_id'];
$tr_g = $_POST['tr_g']; // jce_pnt_act_complete
foreach ($tr_g as $id => $trg) {
//if(empty($trg)) { $trg = "0"; }
$queryg = "UPDATE `jce_pnt` SET `jce_pnt_act_complete` = '$trg' WHERE `jce_pnt_id` = '$jce_pnt_id[$id]'";
$resultg = mysqli_query($cxn,$queryg);
}
* The value writes to the wrong ID in the database.

Related

how to return php variable to Jquery function to be used to multiply values

Trying to have several checkboxes multiply and add where there are multiple products. would it be possible to use a for statement in jquery against the number of rows in a database or have it run without it?
I've reduced the code i had to a compact version to add up checkboxes and multiply against a multiplier that is a POST from a previous page.
now this script won't run for any of the products and wandering what could be used to bridge the problem between client side and server side script.
I'm confused about the method needed to use a php variable in a jquery function. I thought of use an onclick to call function for a checkbox clicked but im trying to run this on DOM ready. If anyone can perhaps tell me in the simplest terms how this may be possible I will happily do more research.
<?php
//CONNECTION, QUERY AND MISC ///
$count = mysqli_num_rows($result);
$i = 1;
while ($row = mysqli_fetch_assoc($result))
{
$kilo1 = $row['kilo1'];
$kilo2 = $row['kilo2'];
$price1 = $row['price1'];
$price2 = $row['price2'];
$price3 = $row['price3'];
$price4 = $row['price4'];
$multiplier = 2;
$qri = "qr"."$i";
$userdaily = "userdaily"."$i";
$usertotal = "usertotal"."$i";
$pricef1 = "price1"."$i";
$pricef2 = "price2"."$i";
echo "<form enctype='multipart/form-data' name='contactform' method='POST' action='something.php'>";
echo "<input type='hidden' name='multiplier' id='multiplier' value='$multiplier'>";
echo "<input type='checkbox' name='checkbox1' id='$qri' value='$price1'>";
echo "<input type='checkbox' name='checkbox2' id='$qri' value='$price2'>";
echo "<input type='checkbox' name='checkbox3' id='$qri' value='$price3'
class='insure'>";
echo "<input type='checkbox' name='checkbox4' id='$qri' value='$price4'
class='insure'>";
echo "<input type='checkbox' name='checkbox5' id='$qri' value='$kilo1'
class='kilo'>";
echo "<input type='checkbox' name='checkbox6' id='$qri' value='$kilo2'
class='kilo'>";
/////TOTALS VISIBLE AND HIDDEN/////
echo "<span id='$userdaily'></span>";
echo "<span id='$usertotal'></span>";
echo "<input type='hidden' name='pricef1' id='$pricef1' value=''>";
echo "<input type='hidden' name='pricef2' id='$pricef2' value=''>";
echo "<input type='submit'>";
if($i%4==0 || $i == $count) echo '<br>';
$i++;
}
?>
<script>
$(function()
{
$("input.kilo").change(function()
{
$("input.kilo").not(this).prop("checked", false);
});
$("input.insure").change(function()
{
$("input.insure").not(this).prop("checked", false);
});
$(document).ready(function()
{
//get the values of the selected options
var counter = parseInt("<?php echo $count;?>");
for (i = 1; i <= counter; i++)
{
let v = $.map($("input[id^='qr+i+']:checked"), function(t)
{
return parseFloat($(t).val());
});
let s = v.reduce((a, b) => a + b);
//sum them up to a daily total
console.log(v);
$("#<?php echo $userdaily;?>").text('R' + s + '/day');
$("#<?php echo $usertotal;?>").text('R' + s *
parseFloat($("#multiplier").val()) + '/day');
$("#<?php echo $pricef1;?>").val(s);
$("#<?php echo $pricef2;?>").val(s * parseFloat($("#multiplier").val()));
}
});
});
</script>
I have a list of product each with a daily total and a total of the number of days, each product has completely different prices. I tried to create this in a fiddle and it works find without the use of variables, I realise that Jquery and php are different in their uses and I'm not sure how to work around this.
As treyBake mentionned, if you need to pass information from PHP to jQuery back to PHP and so forth, use hidden DOM elements and make Ajax requests.
This will also allow you to update your data in real time.
Okay so for instance you can hide your number count in a hidden input
<input type="hidden" value="<?php echo $count;?> " name="countrows" id="countrows">
Now you can retrieve in your jQuery :
var counter = $("#countrows").val();
Get value of checked checkbox with is.checked :
var x = $("#checkbox").is(":checked");

Multiple php isset in one page

I'm trying to achieve multiple pagination in one page.
The pagination for all the results, is working.
But except from the main results, i want to include different options.
For example, in the image below i have three more options,
Newest, Reputation, Oldest
So, i want multiple pagination for all these options.
In the page that i want to display the results, i have multiple isset(), in order to achieve that.
<div id='paginationUsers'>
<?php
//for all the results that displaying first.
if(isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 1;
}
echo "<input type='hidden' id='pageAll' value='".$page."'>";
// for the Newest results
if(isset($_GET['pageNewest'])) {
$pageNewest = $_GET['pageNewest'];
} else {
$pageNewest = 1;
}
echo "<input type='hidden' id='newPage' value='".$pageNewest."'>";
//for the Oldest results
if(isset($_GET['pageOldest'])) {
$pageOldest = $_GET['pageOldest'];
} else {
$pageOldest = 1;
}
echo "<input type='hidden' id='oldPage' value='".$pageOldest."'>";
//for the Reputation results
if(isset($_GET['pageReputation'])) {
$pageReputation = $_GET['pageReputation'];
} else {
$pageReputation = 1;
}
echo "<input type='hidden' id='reputationPage' value='".$pageReputation."'>";
?>
</div>
Ofcourse, in that way cannot work as you can see in the code.
So, is it possible to include multiple isset in one page?
Try this code:
<?php
function getGET($name) {
if (isset($_GET[$name]) {
return $_GET[$name];
} else {
return 1;
}
}
function genInput($id, $name) {
return "<input type='hidden' id='". $id ."' value='". getGET($name) ."'>";
}
echo "<input type='hidden' id='pageAll' value='". getGet('page')."'>";
echo genInput('newPag', 'pageNewest');
echo genInput('oldPage', 'pageOldest');
echo genInput('', 'pageReputation');

Sending jquery-ui selectable data using form

I need to send some POST data using jquery-ui selectable in a form.
Checked object will be stored in my database after clicking "Aggiorna" button.
I use the external Checked() function to preleve the last configuration by my database in order to dinamically choose the default selected items.
if(isset($_POST['Aggiorna'])) // button name
{
global $Gruppo_Sensori,$Gruppo_Elettrovalvole,$id_GS,$id_GE, $G;
// recupero le informazioni dal form
for($i=0;$i<count($G); $i++)
{
if(isset($_POST[$G[$i]]))
{
// UPLOAD $_POST[$G[$i]] on my database
unset($_POST[$G[$i]]); // clear the $_POST data
}
}
echo "<form action='' method='POST' name='programmazione' id='programmazione'>";
echo"<table>";
for($j=0;$j<7;$j++)
{
echo "<tr>";
echo"<td>$G[$j]</td>";
echo"<td>";
for($i=0;$i<24;$i++)
{
$k=$i+1; //calcolo l'estremo superiore dell'intervallo
$value = array($j,$i,0,$j,$k,0);
$nome = $G[$j];
if(Checked($j,$i,0,$j,$k,0)) {
$checked = "checked";
} else {
$checked="";
}
echo "<input type='checkbox' name='$nome"."[]' value='". implode(',',$value). "' $checked/> $i:$k";
}
echo"</td>";
echo "</tr>";
}
echo"</table>";
echo "<tr><td></td><td><input type='submit' value='Aggiorna' name='Aggiorna'></input>
This code works, but now I would like to use jquery to do the same work, for a obviuos graphical reason.
I have replaced the checkbox with
if(Checked($j,$i,$Minuto,$Giorno_Finale,$Ora_Finale,$Minuto_Finale))
{
echo "<li class='ui-state-default ui-selected' id='$id'>$i</li>"; // selected
$_POST[$nome][] = implode(',',$value); // adding items to $_POST in order to reinsert it on database
}
else
{
echo "<li class='ui-state-default' id='$id'>$i</li>"; // unselected
}
I can't understand why it don't work
An other problem will be to add other option than the defaults to $_POST using jquery
A demo is visible here under Programmazione tabs >> clicking on Clock images
User: guest, pass: guest
Thank you very much for your support

Using a generic if statement for all values of an array in PHP

I have this php statement that collects data from a database table. It loops over all rows and separates them into tables of 2 (home team and away team for that game). I also have radio buttons so the user can pick which team they would like to win for each game. There are 9 games.
$games = array();
for($num = 1; $num <= 9; $num++)
{
$games[$num] = "<table border='1'><tr><b><h4>Game ".$num."</h4></b><th>Home</th><th>Draw</th><th>Away</th></tr>";
}
while ($row = mysql_fetch_array($rs))
{
for($num = 1; $num <= 9; $num++)
{
$games[$num] .=
" <tr>
<td><input type='radio' id='home".$num."' name='game".$num."' value='".$row['home'.$num]."' > ".$row['home'.$num]."</td>
<td>Draw <br /><input type='radio' id='draw".$num."' name='game".$num."' value='0'></td>
<td>".$row['away'.$num]."<input type='radio' id='away".$num."' name='game".$num."' value='".$row['away'.$num]."'></td>
</tr>";
}
}
On submit, i want to be able to do something for each game via SELFSUBMIT.
So if a user selects home team for game n, i want to display a message.
if($_SERVER["REQUEST_METHOD"] == "POST")
{
if($_POST['game".$num."'] = ('home'.$num))
{
echo ('home'.$num);
}
}
Thats my wrong php code. Please tell me if i can workaround this.
You might want to use jquery
$(document).ready( function(){
$("input[id^=home]").on("click", function(){
$("#spanDisplay").html( $(this).attr("id") );
//you can also display $(this).attr("name") or $(this).val() to print the value
});
});
In your html, you can use span or div to display the value of id for example.
So in your span
<span id="spanDisplay"></span>
UPDATE: using PHP only
$games[$num] .= "<form name="aForm" method="POST" action="afterSubmit.php">";
for($num = 1; $num <= 9; $num++){
$games[$num] .= "<tr><td><input type='radio' id='home".$num."' name='game".$num."' value='".$row['home'.$num]."'> ".$row['home'.$num]."</td>
<td>Draw <br /><input type='radio' id='draw".$num."' name='game".$num."' value='0'></td>
<td>".$row['away'.$num]."<input type='radio' id='away".$num."' name='game".$num."' value='".$row['away'.$num]."'></td></tr>";
}
$games[$num] .= "</form>";
You will need to surround your input with the form tag, submit and post it to the next page for instance afterSubmit.php. So in afterSubmit.php page, you can put something like this:
for($num = 1; $num <= 9; $num++){
if( isset( $_POST['game".$num."'] ) ){
echo $_POST['game".$num."']; //echo the value
}
}

How to delete an item into an array with a button

I have a code with an array that saves what i need each time I press a button, so these items saved into an array I showed later with a erase buttons, but I don't know how to delete it, so there is the part of the code that shows what I meant:
echo "<table border=1>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Nom Activitat</td>
<td>Nom Tipus Activitat</td>
<td>Tipus Tarifa</td>
<td>Temps/km</td>
<td>Preu</td>";
echo "</tr>";
for ($x=0;$x<count($savedArray[4]);$x++){
echo "<tr>";
echo " <td>".$savedArray[0][$x]."</td>";
echo " <td>".$savedArray[1][$x]."</td>";
echo " <td>".$savedArray[2][$x]."</td>";
echo " <td>".$savedArray[3][$x]."</td>";
echo " <td>".$savedArray[4][$x]."</td>";
echo " <td><input type='submit' onclick='eliminar(".$savedArray[0][$x].",".$savedArray[1][$x].",".$savedArray[2][$x].",".$savedArray[3][$x].",".$savedArray[4][$x].")' class='carritoElim' value='elim'></td>";
echo "</tr>";
}
a pic with all the forms:
the other pic that shows the items on the array:
Anyone knows how to delete the selected row that references the item on the array with the delete button? Thanks
In the client side, the following code works.
for ($x=0;$x<count($savedArray[4]);$x++){
echo '<tr id="line' . $x . '">';
echo " <td>".$savedArray[0][$x]."</td>";
echo " <td>".$savedArray[1][$x]."</td>";
echo " <td>".$savedArray[2][$x]."</td>";
echo " <td>".$savedArray[3][$x]."</td>";
echo " <td>".$savedArray[4][$x]."</td>";
echo " <td><input type='submit' onclick='eliminar('line" . $x . "')' class='carritoElim' value='elim'></td>";
echo "</tr>";
}
function eliminar($id) {
var elem = document.getElementById($id);
elem.parentNode.removeChild(elem);
}
However, if you want to delete column in the server side also, the ajax codes should be added.
php is a server side language, what meant once your response is sent out to the client(browser), under most case all your "saved" data/variables will be lost.
I am not a fan of what your language of choice for this task but here is a way to work around.
The trick is to scan the whole table of data when the user clicked the eliminate button, then send all the data back to the sever and recreate the array with the selected row taken out. Then print the whole array back to html format and send it to user again.
The critical part to of this method is that you NEED to print everything you wish to retain in every response, so that you could scan them all back in to your server side program.
I've some sample code for your reference, but I didn't pay much attention when I wrote them so expect bugs and incorrect syntax, this is just for pure demostration purpose to show you the idea.
the sample code to print the table you've shown:
echo "<form action='".$_PHP_SELF."' method='post'>";
echo "<input type='hidden' name='type' value='yourarrayname'/>";
echo "<table border=1>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Nom Activitat</td>
<td>Nom Tipus Activitat</td>
<td>Tipus Tarifa</td>
<td>Temps/km</td>
<td>Preu</td>";
echo "</tr>";
for ($x=0;$x<count($savedArray[4]);$x++){
if(strcasecmp($savedArray[0][$x],"-999")!=0){
echo "<tr>";
echo "<td><input type='text' name='0_".$x."' value='".$savedArray[0][$x]."'/></td>";
echo "<td><input type='text' name='1_".$x."' value='".$savedArray[1][$x]."'/></td>";
echo "<td><input type='text' name='2_".$x."' value='".$savedArray[2][$x]."'/></td>";
echo "<td><input type='text' name='3_".$x."' value='".$savedArray[3][$x]."'/></td>";
echo "<td><input type='text' name='4_".$x."' value='".$savedArray[4][$x]."'/></td>";
echo "<td><button name='elim' value='e".$x."' type='submit'>elim</button></td>";
echo "</tr>";
}
}
echo "</table>";
echo "</form>";
and here's the sample code to handle the incoming data, put this after you created the array but before the printing code:
if($_POST!=null){
if(strcasecmp($_POST['type'],"yourarrayname")==0){
for ($x=0;$x<count($savedArray[4]);$x++){
if(strcasecmp($_POST['elim'],"e".$x)!=0){
$savedArray[0][$x] = $_POST['0_'.$x];
$savedArray[1][$x] = $_POST['1_'.$x];
$savedArray[2][$x] = $_POST['2_'.$x];
$savedArray[3][$x] = $_POST['3_'.$x];
$savedArray[4][$x] = $_POST['4_'.$x];
}
if(strcasecmp($_POST['elim'],"e".$x)==0){
$savedArray[0][$x] = "-999";
$savedArray[1][$x] = "-999";
$savedArray[2][$x] = "-999";
$savedArray[3][$x] = "-999";
$savedArray[4][$x] = "-999";
}
}
}
}

Categories