Search code examples
phptextboxhtml-table

PHP passing values from generated textboxes


I'm creating a PHP web application in combination with SQL to order stuff. I'm randomly generating textboxes based on the amount of records I have. I want to pass the individual values to insert to the database. I want each cell to have a textbox in which I can input values and pass them with that cell. So if I want to order 2 of cell one I want to pass that value into a database.

Here's the code:

$i = 0;
        if ($AanbodsTabel === false){
            die(mysql_error());
        }
        while ($row = mysqli_fetch_array($AanbodsTabel)){
            echo "<tr><td width = 4%>". htmlentities($row['Fustcode']) . "</td>";
            echo "<td width = 8%>" . htmlentities($row['cultivar']) . "</td>";
            echo "<td width = 6%>" . htmlentities($row['AantalFust']) . "</td>";
            echo "<td width = 6%>" . htmlentities($row['AantalPerFust']) . "</td>";
            echo "<td width = 6%>" . htmlentities($row['AantalStelen']) . "</td>";
            echo "<td width = 4%><input type ='text' id='Bestelbox' value='' name='AantalBestellen".$i."'></td>";
            echo "<td width = 6%>" . htmlentities($row['AantalFustBesteld']) . "</td>";
            echo "<td width = 6%>" . htmlentities($row['AantalStelenBesteld']) . "</td>";
    135:    echo '<td width = 8%><a href="Home.php?aanbodid='. htmlentities($row['aanbodid']) .'&hoeveelheid='. $_GET["AantalBestellen$i"]. '"'
                    . ' class = "btnBestel">Bestel</a></td></tr>';
            $i = $i + 1;
        }

The error I'm getting is undefined index. I guess this is because the name isn't unique. But whatever I do I still get the error.

I'm getting:

undefined index: AantalBestellen0 on line 135;


Solution

  • you can use javascript for example note i will bold the code you need, the rest is context. note this is from a similar project of mine but you should get the point.

    <div id="meldingen_top_div">
               <?php 
    
            echo "<div class='alert_Top_Div_Left_Outer'>
                 <input type='submit' value='Openstaande Meldingen' class='btnLoad' name='' onclick='toggleThis(" . '"' . "#meldTableTop" . '"' . "), toggleThis(".'"'.".alert_Top_Div_Left_Inner".'"'.")'/>
    
           <div class='alert_Top_Div_Left_Inner'>
           <table class='tableRed' id='meldTableTop'><caption/><thead/><tfoot/><tbody>";
    
            $sqlVII = "SELECT Meldingen.Melding_id, FORMAT(Meldingen.Melding_datum, 'dd-mM-yyyy') AS Melding_datum, Producten.Product_naam, Leveranciers.Leverancier_naam, Meldingen.Melding_notitie 
    FROM MovieWorld.dbo.Producten,
     MovieWorld.dbo.Meldingen,
     MovieWorld.dbo.Categorieen,
      MovieWorld.dbo.Leveranciers
     WHERE Producten.Product_id = Meldingen.Product_id AND Producten.Categorie_id = Categorieen.Categorie_id AND Categorieen.Leverancier_id = Leveranciers.Leverancier_id AND Producten.Product_actief = 1 AND Meldingen.Melding_actief = 1
    ";
    
            $resV= $db->executeQuery($sqlVII);
                        $i=1;
    
                        if($resV!=FALSE){
                        while($arrx = sqlsrv_fetch_array($resV)){   
    

    Code wich is relevant for you:

        echo "<tr>
    
        <td><p class='meldDate'>{$arrx['Melding_datum']}</p></td>
        <td>{$arrx['Product_naam']}</td>
        <td>{$arrx['Leverancier_naam']}</td>   
        <td><input id='txt{$i} type='text' name='thisdoesntevenmatter' value='..'/></td>
        <td><img src='somerandompath.png' onclick='getTxt(".'"txt{$i}"'.")'/></td>
     </tr>"
    
    
    
        $i=$i+1;
                        }
    
    
    
                        }else{}echo '</tbody></table></div></div>';
               ?>
            </div>
    

    and the Javascript function

    function getTxt(id){
        var tempId = document.getElementById(id).value;
    return tempId;
    }
    

    however.. you can also send the value back into a php variable, OR use AJAX to pass it to an PHP class to insert it into the Database.

    edit

    Your quotes in line 135: seem incorrect. try this:

        echo '<td width = 8%><a href="Home.php?aanbodid={htmlentities($row['aanbodid']) }&hoeveelheid={$_GET['AantalBestellen$i']}"'
                        . ' class = "btnBestel">Bestel</a></td></tr>';