0

I have a set of HTML input elements which will be called iteratively by a PHP file so that their values can be submitted to a database. At design-time, I do not currently know how many versions of the element there will be.

I have an HTML/JavaScript form, where the number of rows in a table can be altered. When reading the table, I want to read all values from the elements stored in one of the rows, and use them to create an SQL query. I would then iterate this process so that there is a query for each row.

All the HTML elements are labeled to finish with the row number, which I would use to tell the process which element is required. The issue I'm finding is that when I try to do this, I get a blank value.

To add some context, here is my code:

JavaScript (A snippet from a smaller second table following the same process) Example:

   var indexAdd = 1;
   function addRow(){
    var table = document.getElementById("addExtDet");
    var row2=table.insertRow(table.rows.length);
    
    var cell7=row2.insertCell(0);
        cell7.innerHTML = indexAdd;

    var cell8=row2.insertCell(1);  
    var t8=document.createElement("input");
            t8.id = "addGrpInfo:"+indexAdd;
    t8.className = "tablebox";
            cell8.appendChild(t8);

        var cell9=row2.insertCell(2);
        var t9=document.createElement("input");
            t9.id = "addGrpBus:"+indexAdd;
    t9.className = "tablebox";
            cell9.appendChild(t9);

    indexAdd++;
   }

PHP:

    //Add the groups specified by the user. This while loop iterates for each group added.
        $query = "";
        $grpCount = $_POST['numGrps']; //This is a hidden value in the HTML code which gives the number of rows.

        for($i=1; i<=$grpCount; $i++) {
         $grpNum = $_POST['addGrpNum:'.$i];
         $grpTime = $_POST['addGrpTime:'.$i];
         $grpLoc = $_POST['addGrpLoc:'.$i];
         $grpLead1 = $_POST['addGrpLead1:'.$i];
         $grpLead2 = $_POST['addGrpLead2:'.$i];
         $grpSpace = $_POST['addGrpSpc:'.$i];
         $grpInfo = $_POST['addGrpInfo:'.$i];
         $grpBus = $_POST['addGrpBus:'.$i];

         //Example, the variable '$grpTime' appears to be empty/blank.
         echo $grpTime;

         $query = //Where each 'Insert Into Table' query will be added.
  }
  • I think you have your basic concept incorrect about what PHP or HTML is. HTML isn't a programming language in the sense that it doesn't allow for: input, processing, output. HTML cannot be "called". PHP on the other hand, does not care about what you've done with HTML or JavaScript (not to be confused with Java). PHP cannot invoke HTML nor JavaScript. The only way to interact with PHP is through AJAX/HTTP requests, which does not involve HTML. – Daniel Cheung Sep 21 '20 at 17:18
  • I can see right away a glaring issue and you should see it as well with the color changes in your code posted above. You are missing a single quote on the line where you are setting `$grpTime`. – dmikester1 Sep 21 '20 at 17:18
  • @dmikester1 You're absolutely right. Corrected that, and that will save me a lot of time down the line. Thank you! Though it doesn't affect the issue at hand :( – William Heritage Sep 21 '20 at 17:22
  • 2
    JavaScript is not the same thing as Java, at alll – GrumpyCrouton Sep 21 '20 at 17:23
  • If you name your repeated elements `name="someName[]"` then someName will be an array in PHP when SUBMITTING the inputs using a form. Alternatively use form.serialize and ajax to the server – mplungjan Sep 21 '20 at 17:23
  • Your code on the `$grpTime` line is still not correct. Your colon needs to go inside the quotes and you need to concat the `$i` with a period. – dmikester1 Sep 21 '20 at 17:28
  • @dmikester1 Once again, thank you. Another headache passed. I've tried that but still to no avail – William Heritage Sep 21 '20 at 17:33
  • . @mplungjan - Your idea sounds interesting; would you mind explaining a little further for a weak mind? – William Heritage Sep 21 '20 at 17:33
  • `var t8=document.createElement("input");t8.id = "addGrpInfo:"+indexAdd; t8.name="addGrpInfo[]";` plus https://stackoverflow.com/questions/3314567/how-to-get-form-input-array-into-php-array – mplungjan Sep 21 '20 at 17:35
  • @mplungjan Thank you, I'll give that a good look! – William Heritage Sep 21 '20 at 17:41

0 Answers0