Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I use PHP to seed a dynamic table in a form with a users previously submitted answers?
    primarykey
    data
    text
    <p>It's a complicated title, I know. I think an example would best illustrate what I mean:</p> <p>Let's say I'm asking the user for their favorite Rock albums: albumName and albumArtist. I have a table with two columns that starts out with only one row. When the user clicks a button, a javascript function adds another row with the appropriate input fields in the appropriate cells. Then all of this data is stored in a MySQL table. This all works fine.</p> <p>When the user comes back, I'd like to re-render the table, inserting all of the user's submitted values. However, when I call the same JS function using PHP <code>&lt;echo&gt;</code> it renders above the form. How can I fix this? </p> <p>Sample code below: </p> <p>Javascript in head:</p> <pre><code>function addInputCell(newRow, cellNum, size, maxLength, fieldID, inputID, value) { var newCell = newRow.insertCell(cellNum); var elem = document.createElement('input'); elem.type = 'text'; elem.size = size; elem.maxlength = maxLength; elem.name = fieldID + inputID; elem.value = value; newCell.appendChild(elem); alert(elem.value); } function add3CellRow(fieldID, size1, maxlength1, size2, maxlength2, value) { var table = document.getElementById(fieldID); var lastRow = table.rows.length; if(lastRow &lt;22){ var newRow = table.insertRow(lastRow - 1); var cellLeft = newRow.insertCell(0); var textNode = document.createTextNode(lastRow - 1); cellLeft.appendChild(textNode); addInputCell(newRow, 1, size1, maxlength1, fieldID, 'Name[]', value); addInputCell(newRow, 2, size2, maxlength2, fieldID, 'Artist[]', value); } else { alert('You can only enter 20 values in this field'); } } </code></pre> <p>HTML in body:</p> <pre><code>&lt;table width="450" border="0" id='album' cellspacing="0" cellpadding="0"&gt; &lt;thead&gt; &lt;tr&gt; &lt;td width="3"&gt;&lt;/td&gt; &lt;td width="90%" align="center"&gt;&lt;b&gt;Album Name&lt;/b&gt;&lt;/td&gt; &lt;td width="10%" align="center"&gt;&lt;b&gt;Album Artist&lt;/b&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tr&gt; &lt;td align="right"&gt;1&lt;/td&gt; &lt;td&gt;&lt;input type="text" size="20" name="albumName[]" maxlength="20" value='&lt;?php echo($onfile['albumName']);?&gt;'/&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" size="20" name="albumArtist[]" maxlength="20" value='&lt;?php echo($onfile['albumArtist']);?&gt;'/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td align="right"&gt;&lt;input type="button" value="Add Concern" onClick="add3CellRow('album',20,20,20,20 ,'');"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>PHP in body:</p> <pre><code>&lt;?php echo ("&lt;script type='text/javascript'&gt;"); //Get the data in the rock table where userID matches $result = mysql_query("SELECT * FROM rock WHERE userID = '$_SESSION[userID]'"); while($onfile = mysql_fetch_array($result)) //Set the data into an array { echo ("add3CellRow('rock',20,20,20,20, '$onfileConcern[albumName]');"); } echo ("&lt;/script&gt;"); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload