-The code has been edited to be fully functional now-
I want to grab some doubles and ints from inputs and post them to php ultimately update'ing mysql rows. Please do tell if there is a better way to do it. (I wanted to do multidimentional arrays)
I have been looking around the web for 2 days and experimenting solely to make this work while thinking: "those stackflowers can probably fix this in 2 minutes" :C
-Thank you Jeff for all your help- Do I need to delete my "question"
"HTML part":
<?php
echo "<form id='shoplistform'>";
echo "<table id='tablecode'> "
. "<tr> "
. "<th id='RecTh'>Rec"
. "<button class='myButton' type='submit' value='Send'>Remember</button>"
. "</th>"
. "<th id='Ingredients' >Ingredients</th>"
. "<th id='Amount'>Amount</th>"
. "<th id='Lavest'>Lavest Pris</th>"
. "</tr>";
$stmt = $pdo->prepare("SELECT * FROM lists WHERE `email` = :email");
$stmt->execute(['email' => $email]);
$row = $stmt->fetchAll();
if (count($row) > 0) {
// output data of each row
for($i = 0; $i < count($row); $i++ ) {
$listamount = $row[$i]["amount"];
$listpris = $row[$i]["pris"];
echo "<tr class='tests' ><td>name: " . $row[$i]["name"]. "</td>"
. "<td> vare: " . $row[$i]["food"]. "</td>"
. "<td><input type='number' class='listamount' id='\"" . $row[$i]['id'] . "\"' name='bar[]' value='$listamount' > </input></td>"
. "<td><input style='display:none' type='number' class='listpris' value='$listpris' ></input>" . $row[$i]["pris"]. " kr " . $row[$i]['id']
. "</td></tr>";
}
}
else {
echo "0 list";
}
echo "</table></form>";
?>
jquery / ajax (from Stackflow: origin of code below)
$(document).ready(function(){
// Variable to hold request
var request;
// Bind to the submit event of our form
$("#shoplistform").submit(function(event){
// Prevent default posting of form - put here to work in case of errors
event.preventDefault();
// Abort any pending request
if (request) {
request.abort();
}
// setup some local variables
var $form = $(this);
// Let's select and cache all the fields
var $inputs = $form.find("input, button");
// Serialize the data in the form
var serializedData = $form.serialize();
alert(serializedData);
// Let's disable the inputs for the duration of the Ajax request.
// Note: we disable elements AFTER the form data has been serialized.
// Disabled form elements will not be serialized.
$inputs.prop("disabled", true);
// Fire off the request to /form.php
request = $.ajax({
url: "shoplistupdate.php",
type: "post",
data: serializedData,
});
// Callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
// Log a message to the console
//console.log("Hooray, it worked! >" + response + " >" + textStatus + " >" + jqXHR);
$("#txtHints").html(response);
});
// Callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// Log the error to the console
console.error(
"The following error occurred: "+
textStatus, errorThrown
);
});
// Callback handler that will be called regardless
// if the request failed or succeeded
request.always(function () {
// Reenable the inputs
$inputs.prop("disabled", false);
});
});
});
PHP
$bar = isset($_POST['bar']) ? $_POST['bar'] : null;
print_r($bar);
echo "<br><br>";
var_dump($bar);
The alert(serializedData) in jquery part says: "bar=>3&bar=>2&bar=>1". The correct values but...
PHP returns:
Array ( [0] => 3 [1] => 2 [2] => 1 )
array(3) { [0]=> string(1) "3" [1]=> string(1) "2" [2]=> string(1) "1" }