0

-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" }
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Dipz
  • 15
  • 4
  • `var_dump` echoes by itself. So you see that at top of your output `string(1) "1"`. `var_dump()` doesn't return anything. – Jeff Sep 17 '17 at 13:30
  • if you want the string to be returned, so that you can contatanate it in your echo, use `var_export($var, true)` - But the structure and amount if information is different. – Jeff Sep 17 '17 at 13:31
  • and you will need to change `bar` to an array (`name='bar[]'`)! And change the ids of the inputs to be unique. – Jeff Sep 17 '17 at 13:34
  • I removed (SOLVED) from the title. If an answer solved it (if and when one does get posted), consider accepting the answer or post one yourself or delete the question. There's no need to add "solved" to the title and it shouldn't neither. – Funk Forty Niner Sep 17 '17 at 21:23

0 Answers0