Sending jquery-ui selectable data using form - php

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

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");

Update from checkbox to wrong ID in 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.

How do I use $_SESSION to store items in an array and trigger a button swap?

so I'm trying to store shopping cart items by there ID. Once this happens a button needs to appear "Remove from Cart", if I click this button a new button will appear "Add to Cart" but I'm a little bit lost on it from this point. Im new on here so please excuse my mistakes. My code is below:
session_start();
$items[] = $_POST['add'];
if (isset($_POST['add'])) {
$_SESSION['cart'][]=$_POST['add'];
} else if (isset($_POST['remove'])) {
unset ($_SESSION['cart'][$_POST['remove']]);
}
foreach($vend as $vendID=> $items) {
echo "<form action='vend.php' method='post'>";
echo "<article id ='vend-$vendID'>";
echo "<h1 class = 'item-h1' id = 'h1'>{$items['title']}</h1>";
echo "<div class ='item-no'>";
echo "<p class = 'pro-id'><b>Product ID: </b>{$vendID}</p></div>";
echo "</div>";
echo "<div class ='img-div'>";
echo "<img src=../images/{$items['img']} alt='' height='196' width='200'></div>";
echo "<div class='item-p'>";
echo "<p>{$items['desc']}</p></div>";
echo "<div class='pricing'>";
echo "<p><b>Price: $</b>{$items['price']}</p></div>";
//echo "<button name='add' type='submit' value='$vendID'>Add to Cart</button>";
if(isset($_POST['add']) && ($_SESSION['cart'] == $vendID)) {
echo "<button name='remove' type='submit' value='$vendID'>Remove from Cart</button>";
}
else {
echo "<button name='add' type='submit' value='$vendID'>Add to Cart</button>";
}
In your case here what should be done:
session_start();
/* Check if $_SESSION['cart'] exists */
if (!isset($_SESSION['cart'])) {
/* Init cart as empty array */
$_SESSION['cart'] = [];
}
if (isset($_POST['add']) && 0 < $_POST['add']) {
/* Add product id to session cart */
$_SESSION['cart'][$_POST['add']] = 1;
} else if (isset($_POST['remove']) && 0 < $_POST['remove']) {
/* Remove product id from session cart */
unset($_SESSION['cart'][$_POST['remove']]);
}
// I check with `0 < $id` because ids are always positive
foreach($vend as $vendID=> $items) {
echo "<form action='vend.php' method='post'>";
echo "<article id ='vend-$vendID'>";
echo "<h1 class = 'item-h1' id = 'h1'>{$items['title']}</h1>";
echo "<div class ='item-no'>";
echo "<p class = 'pro-id'><b>Product ID: </b>{$vendID}</p></div>";
echo "</div>";
echo "<div class ='img-div'>";
echo "<img src=../images/{$items['img']} alt='' height='196' width='200'></div>";
echo "<div class='item-p'>";
echo "<p>{$items['desc']}</p></div>";
echo "<div class='pricing'>";
echo "<p><b>Price: $</b>{$items['price']}</p></div>";
if(isset($_SESSION['cart'][$vendID])) {
// you have `$vendID` in session cart - then show Remove button
echo "<button name='remove' type='submit' value='$vendID'>Remove from Cart</button>";
} else {
// you DON'T have `$vendID` in session cart - then show Add button
echo "<button name='add' type='submit' value='$vendID'>Add to Cart</button>";
}
// Also don't forget to close `</form>`
}
To remove an item from your cart you are accessing the item based on your $vendID variable. Which would require your item to be stored in $_SESSION['cart'] as an array:
$_SESSION['cart'][0] = "item 1";
$_SESSION['cart'][1] = "item 2";
...
but later in your code you are accessing $_SESSION['cart'] as if it is just a value not an array
adding an item in your code is done with the following line:
$_SESSION['cart'][]=$_POST['add'];
This results in something like this:
//empty cart
$_SESSION['cart'][0] = $vendid;
Turns out I needed a underscore , i had $POST instead of $_POST and it works now, thanks all :)

input type=submit not returning anything

Team, could you please help me on this? I am trying to get the values from a form to alter those values on mysql. It is a sell operation that requires a value and this will be put in the database.
I have a check box that allows the user to select which objects he wants to be sold them according to this selection I am creating a new table bellow with objects selected and POST methods for the values. However this is not doing exactly what I need, I do not know how to relate each value to the correct object. Could you help ? This is the part that I am stuck at, more specifically on second isset:
if(isset($_POST['publicar'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
echo "<tr><td>Sites a Publicar</td><td>Valor</td></tr>";
foreach($_POST['check_list'] as $selected){
echo"<tr><td>".$selected."</td><td><input type=number name=valor_site[]></td></tr>";
$_SESSION['lista_public[]']=$selected;
}
echo"<tr><td><input type='submit' name='submit_valor' value='Submit' /></td></tr>";
}
}
if(isset($_POST['submit_valor'])){
if(!empty($_POST['valor_site'])){
foreach($_POST['valor_site'] as $valor_site){
echo $selected." tem valor ".$valor_site;
}
}
}
Use array
$temp = array()
if(isset($_POST['publicar'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
echo "<tr><td>Sites a Publicar</td><td>Valor</td></tr>";
foreach($_POST['check_list'] as $selected){
echo"<tr><td>".$selected."</td><td><input type=number name=valor_site[]></td></tr>";
$temp[]=$selected;
}
$_SESSION['lista_public'] = $temp;
echo"<tr><td><input type='submit' name='submit_valor' value='Submit' /></td></tr>";
}
}
if(isset($_POST['submit_valor'])){
if(!empty($_POST['valor_site'])){
$i = 0;
$temp = $_SESSION['lista_public'];
foreach($_POST['valor_site'] as $valor_site){
echo $temp[$i++]." tem valor ".$valor_site;
}
}
The problem is this line:
$_SESSION['lista_public[]']=$selected;
This overwrites the value stored in $_SESSION['lista_public[]'] every time. What I'm suggesting is:
$_SESSION['lista_public'] = array();
if(isset($_POST['publicar'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
echo "<tr><td>Sites a Publicar</td><td>Valor</td></tr>";
foreach($_POST['check_list'] as $selected){
echo"<tr><td>".$selected."</td><td><input type=number name=valor_site[]></td></tr>";
$_SESSION['lista_public'][] = $selected;
}
echo"<tr><td><input type='submit' name='submit_valor' value='Submit' /></td></tr>";
}
}
if(isset($_POST['submit_valor'])){
if(!empty($_POST['valor_site'])){
$i = 0;
foreach($_POST['valor_site'] as $valor_site){
echo $_SESSION['lista_public'][$i++]." tem valor ".$valor_site;
}
}
}

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