• Home
  • Forum
  • Hosting, Server e Sistemi
  • Database
  • MySQL

[MySQL] Inserimento multiplo da form multirighe

  • Creatore Discussione Creatore Discussione gandalf1959
  • Data di inizio Data di inizio 25 Mag 2018
Prec.
  • 1
  • 2
Primo Prec. 2 di 2

gandalf1959

Utente Attivo
21 Nov 2013
208
1
18
  • 21 Giu 2018
  • #21
Scusate, ma siccome non posso perdere ancora tanto tempo su questa cosa, avrei deciso di procedere in modo meno "intelligente" ma forse più veloce...
Torno alla mia pagina con 25 righe e 25 form, con 25 tasti submit...
Insomma una pagina così:
PHP:
<?php
session_start();
//se non c'è la sessione registrata
mb_internal_encoding('UTF-8');
if (!$_SESSION['autorizzato']) {
  echo "<h1>Area riservata, accesso negato.</h1>";
  echo "Per effettuare il login clicca <a href='index.php'><font color='blue'>qui</font></a>";
  die;
}
 
//Altrimenti Prelevo il codice identificatico dell'utente loggato

$cod = $_SESSION['cod']; //id cod recuperato nel file di verifica

$codicericetta = $_GET['codicericetta'];
$titolo = $_GET['titolo'];


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Inserimento Ricette per le Dispense</title>
<link href="backoffice.css" rel="stylesheet" type="text/css" />



</head>

<body>
<div id="inserimentoric" class="testi">
 
 

<table width="800px" border="1" cellspacing="0" cellpadding="0" bgcolor="#CCCCCC">
  <tr>
    <td colspan="4"><p align="center"><b><? echo $codicericetta; ?></b><br><? echo $titolo; ?></p><hr></td>
  </tr>

  <tr>
    <td colspan="4"><b>Ingredienti:</b><br></td>
  </tr>

  <tr>
    <td>Ingrediente</td>
    <td>Quantità</td>
    <td>Un.Mis.</td>
    <td></td>
  </tr>

  <tr>
    <form id="tabella" name="tabella" method="post" action="">
    <td><input name="ingrediente1" id="ingrediente1" type="text" size="50" /></td>
    <td><input name="quantita1" id="quantita1" type="text" size="4" /></td>
    <td><select name="misura1" id="misura1" />
        <option value="" selected="selected">===</option>
        <option value="Kg.">Kg</option>
        <option value="Gr.">Gr.</option>
        <option value="Lt.">lt</option>
        <option value="ml">ml</option>
        <option value="N.">N.</option>
        <option value="Bicchiere/i">Bicchiere/i</option>
        <option value="QB">QB</option>
      </select>
    </td>
    <td align="center"><input type="hidden" name="idricetta1" id="idricetta1" value="<? echo $codicericetta; ?>" /><br>
      <input name="conferma1" type="submit" id="conferma1" value=" Registra " />
      <div id="risultato"></div></p></td>
  </form>
  </tr>


  <tr>
    <form id="tabella2" method="post" action="">
    <td><input name="ingrediente2" type="text" size="50" /></td>
    <td><input name="quantita2" type="text" size="4" /></td>
    <td><select name="misura2" />
        <option value="" selected="selected">===</option>
        <option value="Kg.">Kg</option>
        <option value="Gr.">Gr.</option>
        <option value="Lt.">lt</option>
        <option value="ml">ml</option>
        <option value="N.">N.</option>
        <option value="Bicchiere/i">Bicchiere/i</option>
        <option value="QB">QB</option>
      </select>
    </td>
    <td align="center"><input type="hidden" name="idricetta2" id="idricetta2" value="<? echo $codicericetta; ?>" /><br>
      <input name="conferma2" type="submit" id="conferma2" value=" Registra " />
      <div id="risultato2"></div></p></td>
    </form>
  </tr>


<tr>
    <td colspan="3"><br><b>Procedimento:</b><br>
      <textarea name="ingredienti" cols="110" rows="35"></textarea>
    </td>
    <td align="center"><input type="hidden" name="idricetta" id="idricetta" value="<? echo $codicericetta; ?>" /><br>
      <input name="procedimento" type="submit" id="procedimento" value=" Inserisci procedimento " /></p></td>
  </tr>
</table>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="ajax_ricette.js"></script>

</div>
</body>
</html>

il file ajax_ricette:
Codice:
$(document).ready(function() {
    //Esegui la funzione quando il form 1 viene inviato
    $('#tabella').submit(function(e) {
        // Con il comando qui sotto evito che venga eseguito il comando "action" del <form>
        // Da notare che il parametro "e" si trova come parametro della funzione qui sopra
        e.preventDefault();

        //recupero il valore degli input
        var ingrediente = $('#ingrediente1').val();
        var quantita = $('#quantita1').val();
        var misura = $('#misura1').val();
        var idricetta = $('#idricetta1').val();

        //passo alla chiamata asincrona
        registrami(idricetta, ingrediente, quantita, misura);
    });


    //Esegui la funzione quando il form 2 viene inviato
    $('#tabella2').submit(function(e) {
        // Con il comando qui sotto evito che venga eseguito il comando "action" del <form>
        // Da notare che il parametro "e" si trova come parametro della funzione qui sopra
        e.preventDefault();

        //recupero il valore degli input
        var ingrediente2 = $('#ingrediente2').val();
        var quantita2 = $('#quantita2').val();
        var misura2 = $('#misura2').val();
        var idricetta2 = $('#idricetta2').val();

        //passo alla chiamata asincrona
        registrami2(idricetta2, ingrediente2, quantita2, misura2);
    });



    //la chiamata ajax
    function registrami(idricetta, ingrediente, quantita, misura) {
        $.ajax({
            url: 'registraingrediente.php',
            method: 'POST',
            data: {ric:idricetta, ingr:ingrediente, quant:quantita, mis:misura},
            dataType: 'text',
            success: function(feedback) {
                $("div#risultato").html(feedback);
                
            }
        });
    }

    function registrami2(idricetta2, ingrediente2, quantita2, misura2) {
        $.ajax({
            url: 'registraingrediente.php',
            method: 'POST',
            data: {ric:idricetta2, ingr:ingrediente2, quant:quantita2, mis:misura2},
            dataType: 'text',
            success: function(feedback) {
                $("div#risultato2").html(feedback);
                
            }
        });
    }

});

Questa struttura non è bella come quella di cui stavamo discutendo, e comunque ha un problema:
la prima riga mi registra correttamente il codice lezione (che arriva dalla pagina precedente) e il nome dell'ingrediente.
Nella colonna quantita mi mette zero (io inserisco un numero nel form) e nella colonna della misura invece di Kg o gr mi mette la quantita...
la seconda riga mi riporta correttamente il numero ricetta e basta...
Magari se risolvo questa cosa posso andare avanti.
Grazie per l'aiuto
 

marino51

Utente Attivo
28 Feb 2013
3.205
207
63
Lombardia
  • 22 Giu 2018
  • #22
mi spiace tu non riesca ad utilizzare lo script che ti ho postato,
provo a semplificare la sua gestione,
ho creato due files,

pagina1.php che contiene la componente html


pagina2.php che viene chiamato dalla pagina 1 e tratta i dati ricevuti da essa


pagina 1.php
HTML:
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Documento senza titolo</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type='text/javascript'>
    $(document).ready(function() {
        var currentItem = 1;
        AddRow(currentItem);

        $('#addnew').click(function(){
            currentItem++;
            AddRow(currentItem);
        });

        $('#removelast').click(function(){
            if (currentItem > 1) {
                var rowID = '#row'+currentItem
                $(rowID).remove();
                currentItem--;
                $('#items').val(currentItem);
            }
        });

        function AddRow(currentItem)
        {
            var strToAdd = NewRow(currentItem)
            $('#data').append(strToAdd);
            $('#items').val(currentItem);
        };

        function NewRow(currentItem)
        {
            text
='<tr id="row'+currentItem+'">'
+'<td><input name="ingrediente[]" type="text" size="50" /></td>'
+'<td><input name="quantita[]" type="text" size="4" /></td>'
+'<td><select name="misura[]">'
+'<option value="===" selected="selected">scegli unit&agrave; di misura</option>'
+'<option value="Kg">Kg</option>'
+'<option value="Gr">Gr</option>'
+'<option value="Lt">lt</option>'
+'<option value="ml">ml</option>'
+'<option value="N">N.</option>'
+'<option value="Bicchiere">Bicchiere/i</option>'
+'<option value="QB">QB</option>'
+'</select>'
+'</td>'
+'</tr>';
            return text;
        };
    });
    </script>
  </head>
  <body>
    <form name='myform' method='POST' action="Pagina2.php">
      <table class="dd" width="100%" id="data">
        <tbody>
        </tbody>
      </table>
      <input type="submit" name="submit"                     value="Submit Values" />
      <input type="button" name="addnew"     id="addnew"     value="Add new item" />
      <input type="button" name="removelast" id="removelast" value="Remove last item" />
      <input type="hidden" name="items"      id="items"      value="0" />
    </form>
  </body>
</html>

pagina2.php
PHP:
<?php
if(!empty($_POST['submit']))
{
    // ho ricevuto i dati perchè è stato cliccato il bottone 'Submit Values' della pagina 1

    // qui devo mettere il trattamento dei dati ricevuti ricordando che,

    $items = $_POST['items'];  // numero delle righe presenti

    echo "ho ricevuto " . $items . " righe<br /><br />";

    for($r = 0; $r < $items; $r++)
    {
        $ingrediente = $_POST['ingrediente'][$r];
        $quantita    = $_POST['quantita'][$r];
        $misura      = $_POST['misura'][$r];

        echo "riga : " . $r . " ingrediente : " . $ingrediente . " quantita : " . $quantita . " misura : " . $misura . "<br />";
    }
}
else
{
    echo "NON HO RICEVUTO DATI CON il bottone 'Submit Values !'";
    die();
}
?>

se vuoi spendere ancora qualche minuto, per la prova usa i nomi dei file che ho assegnato
 

gandalf1959

Utente Attivo
21 Nov 2013
208
1
18
  • 22 Giu 2018
  • #23
così in effetti funziona.
e siccome è molto meglio che scrivere 25 volte la stessa cosa (anche con il copia e incolla....) ti ringrazio, personalizzo e uso i tuoi file.
Grazie moltissimo!
Alla prossima
 
Prec.
  • 1
  • 2
Primo Prec. 2 di 2
Devi accedere o registrarti per poter rispondere.

Discussioni simili

P
Query Mysql per inserimento multiplo
  • paolofru
  • 28 Gen 2012
  • MySQL
Risposte
4
Visite
5K
MySQL 29 Gen 2012
borgo italia
K
form Inserimento record mysql
  • Kiko74b
  • 21 Gen 2023
  • PHP
Risposte
2
Visite
1K
PHP 25 Gen 2023
WmbertSea
E
Inserimento dati da PHP in tabella MySQL
  • etto.demu
  • 1 Giu 2019
  • PHP
Risposte
5
Visite
3K
PHP 3 Giu 2019
zorro
G
inserimento csv in tabella mysql; problema con struttura
  • giacomo9783
  • 29 Gen 2019
  • PHP
Risposte
11
Visite
3K
PHP 30 Gen 2019
marino51
K
[PHP + MYSQL ] Inserimento dati in database da form dinamico
  • keyz23
  • 13 Set 2018
  • PHP
Risposte
13
Visite
12K
PHP 11 Ott 2019
marino51
A
[RISOLTO]Inserimento Immagini da pc a MySql
  • Alex@1983
  • 8 Mag 2018
  • PHP
Risposte
15
Visite
3K
PHP 11 Mag 2018
macus_adi
Inserimento data su database MYSQL
  • paloppa
  • 27 Feb 2018
  • PHP
Risposte
2
Visite
2K
PHP 27 Feb 2018
paloppa
M
Inserimento Array prelevato da Database in Php in un altra tabella mysql
  • marcoft94
  • 21 Mag 2017
  • PHP
Risposte
0
Visite
1K
PHP 21 Mag 2017
marcoft94
M
G
mysql - problemi inserimento dati nel database, consigli.
  • giorgio85
  • 25 Mag 2016
  • MySQL
Risposte
3
Visite
2K
MySQL 26 Mag 2016
marino51
S
Php e mysql, estrazione da una tabella e inserimento in un'altra tabella
  • SiKei2
  • 20 Mag 2016
  • PHP
Risposte
14
Visite
6K
PHP 20 Mag 2016
SiKei2
S
K
Consiglio su inserimento mysql
  • Kelly
  • 14 Apr 2015
  • PHP
Risposte
4
Visite
1K
PHP 14 Apr 2015
Kelly
K
M
Invio mail con destinatari da mysql con inserimento di nome e cognome
  • Madera
  • 17 Feb 2015
  • PHP
Risposte
6
Visite
5K
PHP 20 Feb 2015
Madera
M
C
inserimento o eliminazione record mysql senza cambio pagina
  • crashall
  • 12 Nov 2014
  • Ajax
Risposte
9
Visite
4K
Ajax 19 Nov 2014
crashall
C
S
Inserimento dati in colonna mysql
  • SardaFilm
  • 19 Lug 2014
  • PHP
Risposte
10
Visite
2K
PHP 22 Lug 2014
SardaFilm
S
G
Inserimento array proveniente da un modulo di registrazione con PHP e MySQL
  • Garaux
  • 20 Feb 2014
  • PHP
Risposte
6
Visite
2K
PHP 21 Feb 2014
Garaux
G
G
Inserimento loop in una INSERT query in PHP e MySQL
  • Garaux
  • 16 Gen 2014
  • PHP
Risposte
2
Visite
2K
PHP 17 Gen 2014
Garaux
G
O
Inserimento multilplo di un ciclo for in un database mysql
  • orp
  • 23 Dic 2013
  • PHP
Risposte
0
Visite
3K
PHP 23 Dic 2013
orp
O
O
[risolto] Inserimento nuovo campo in mysql. Problema
  • orp
  • 17 Ott 2013
  • PHP
Risposte
6
Visite
3K
PHP 19 Ott 2013
orp
O
N
Inserimento valori da Joomla a database MySql tramite PHP
  • nofcfro
  • 16 Ott 2013
  • Joomla
Risposte
0
Visite
3K
Joomla 16 Ott 2013
nofcfro
N
Modifica tabelle mysql - inserimento immagini
  • Fanki Sib
  • 23 Set 2013
  • PHP
Risposte
1
Visite
2K
PHP 23 Set 2013
borgo italia
Condividi:
Facebook X (Twitter) LinkedIn WhatsApp e-mail Condividi Link
  • Home
  • Forum
  • Hosting, Server e Sistemi
  • Database
  • MySQL
  • Italiano
  • Termini e condizioni d'uso del sito
  • Policy Privacy
  • Aiuto
  • Home
Community platform by XenForo® © 2010-2024 XenForo Ltd. | Traduzione a cura di XenForo Italia
Menu
Accedi

Registrati

  • Home
  • Forum
    • Nuovi Messaggi
    • Cerca...
  • Novità
    • Featured content
    • Nuovi Messaggi
    • Ultime Attività
X

Privacy & Transparency

We use cookies and similar technologies for the following purposes:

  • Personalized ads and content
  • Content measurement and audience insights

Do you accept cookies and these technologies?

X

Privacy & Transparency

We use cookies and similar technologies for the following purposes:

  • Personalized ads and content
  • Content measurement and audience insights

Do you accept cookies and these technologies?