Hello:

I have a bit of an interesting delima:

I have a form with select box which is dynamically db populated and based on the first selection, additional data populates two other linked boxes (a input and a textarea). This process happens in a table row.

here is the source code of the selection process:

<tr><td><select name='select_1' style='width:200px; color:#003399; text-align:center; font-size:1em' onChange="switch_select(); switch_text();"> 
      <option>-- Select an Item --</option><option>5 A Series Blades</option><option>Balls</option><option>TT Tables-Mini</option><option>Bottle Water</option><option>TT Tables -- Reg</option><option>Gatorate</option><option>Blade combo</option><option>Membership Renewal</option><option>1 hr Private Lesson (Adlt)</option><option>1 hr Private Lesson (Chld)</option><option>Group Lesson (Adlt)</option><option>Group Lesson (Chlidren)</option><option>Membership (Couples-2)</option><option>Membership (Family -Up 4)</option><option>Robot Play</option><option>Gift Certificate</option><option>Gift Card Add Value</option><option>Club T-Shirts</option><option>Member Credit</option><option>Other</option><option>Rubber - 1615 PIPS </option><option>Rubber -Volant 3</option><option>6 A Series Blades</option><option>Blades Only -China QI</option><option>Blades Only -CQ 1</option><option>Ross-Action Blade</option><option>Ross-Classic Blade</option></select> </td>     
     <select name='select_2' onChange="switch_text();" style='display:none' disabled='true'>
    <option>You need to select a category</option>
    <option></option>
    <option></option>
  </select><td><textarea name='mytextarea' rows='2' cols='40' class='expand10-1000' style='color:#003399; text-align:left; font-size:1.1em; border-left: none; border-right: none; border-bottom: none'></textarea><td>
            <td valign='top' width='17%'><input  type='text' name='qty'  class="qty" size='3' maxlength='3' value='' class='combo3' rel='code_id' title='' style='color:#003399; text-align:left; font-size:1.1em; border-left: none; border-right: none; border-bottom: none'></td>
            <td valign='top'  width='17%'><input  type='text' name='cost'  class="cost" size='6' maxlength='6' value='0.00' class='combo3' rel='code_id' title='' style='color:#003399; text-align:left; font-size:1.1em; border-left: none; border-right: none; border-bottom: none'></td>
            </tr>

The user has the option of adding an additional row which is similarily structured to give the user the option of selecting a different item (with its description an price) for each row.

Now here is the problem; the adding row mechanism is done with ajax using the following code:

$('#addrow').click(function(){
        $('.item-row:last').after('`//INSERT THE CONTENT OF THE PHP HERE//`');
        if ($('.delete').length > 0) { $('.delete').show(); }
        bind();
    });

I want to insert the content of an external php file containing the dynamic linked field into the ajax above in the section outlined in red.

Sorry for the long post...

here is the external php file:

<?php
echo"
    <script type='text/javascript' src='js/jquery-1.3.2.min.js'></script>
    <script language='JavaScript'> ";?>
var num_of_cats = 17; // This is the number of categories, including the first, blank, category.
var open_in_newwindow=1; //Set 1 to open links in new window, 0 for no.
<?php
include '../datalogin.php';// make sure this is available to make connection to db

$result = mysql_query("SELECT * FROM products");
echo "var option_array = new Array(num_of_cats);";

$count=1;
echo"option_array[0] = new Array(\"Please Select a Merchandise\");";
while($row = mysql_fetch_array($result))
  {
echo"option_array[".$count."] = new Array(\"--select One--\",\"\",\"\");";
$count++;
}
$result2 = mysql_query("SELECT * FROM products");

echo"var text_array = new Array(num_of_cats);";

$count=1;
echo  "text_array[0] = new Array(\"Please Select a Merchandise\");";
while($row_1 = mysql_fetch_array($result2))
  {
echo "text_array[".$count."] = new Array(\"".$row_1['product_desc']."\");";
$count++;

}

$result3 = mysql_query("SELECT * FROM products");

echo "var text_array2 = new Array(num_of_cats);";

$count=1;
echo "text_array2[0] = new Array(\"Please Select a Merchandise\");";
while($row_2 = mysql_fetch_array($result3))
  {
echo "text_array2[".$count."] = new Array(\"".$row_2['unit_cost']."\");";
$count++;

}

?>
<?php
echo"
var options = 0;
function switch_select()

{
  for (loop = window.document.PaymentForm.select_2.options.length-1; loop > 0; loop--)
  {
    window.document.PaymentForm.select_2.options[loop] = null;
  }
  for (loop = 0; loop < option_array[window.document.PaymentForm.select_1.selectedIndex].length; loop++)
  {
    window.document.PaymentForm.select_2.options[loop] = new Option(option_array[window.document.PaymentForm.select_1.selectedIndex][loop]);
  }
    window.document.PaymentForm.select_2.selectedIndex = 0;
}

function switch_text()
{
  window.document.PaymentForm.mytextarea.value = text_array[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex];
 window.document.PaymentForm.cost.value = text_array2[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex];
  //window.document.PaymentForm.gift_card.value = text_array3[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex];
  //window.document.PaymentForm.qty.value = text_array4[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex];

  }
function box()
{
  if (window.document.PaymentForm.select_2.selectedIndex == 0)
  {
    alert(\"Sorry, you have to select an item\");
  } else {
    if (open_in_newwindow==1)
    window.open(url_array[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex],\"_blank\");
    else
    window.location=url_array[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex]
  }
}
function set_orig()
{
  window.document.PaymentForm.select_1.selectedIndex = 0;
  window.document.PaymentForm.select_2.selectedIndex = 0;
}
window.onload=set_orig
</script> ";
            include '../datalogin.php';
$get_products = "select id as id_num, items as display_name2 from products order by id_num"; 
$get_products_res = mysql_query($get_products) or die (mysql_error()); 

if (mysql_num_rows($get_products_res) < 1) 
  { 
       // no records 
       $display_block .="<p><em>Sorry, no records to select</em></p>"; 
    } 

  else 
   { 
    echo" <form name='PaymentForm' onSubmit='return false;'>
    <tr><td><select name='select_1' style='width:200px; color:#003399; text-align:center; font-size:1em' onChange=\"switch_select(); switch_text();\"> 
      <option>-- Select an Item --</option>"; 
 while ($recs2 = mysql_fetch_array($get_products_res)) 
      { 
          $id_num = $recs2['id_num']; 
          $display_name2 = stripslashes($recs2['display_name2']); 

 //$display_block .= "<option value=\"$id_num\">$display_name2</option>"; 
 echo "<option>$display_name2</option>"; 

                     } 
      }
  echo "</select> </td>"; 

 echo "     
     <select name='select_2' onChange=\"switch_text();\" style='display:none' disabled='true'>
    <option>You need to select a category</option>
    <option></option>
    <option></option>
  </select>";
            echo"<td><textarea name='mytextarea' rows='2' cols='40' class='expand10-1000' style='color:#003399; text-align:left; font-size:1.1em; border-left: none; border-right: none; border-bottom: none'></textarea><td>
            <td valign='top' width='17%'><input  type='text' name='qty'  class=\"qty\" size='3' maxlength='3' value='' class='combo3' rel='code_id' title='' style='color:#003399; text-align:left; font-size:1.1em; border-left: none; border-right: none; border-bottom: none'></td>
            <td valign='top'  width='17%'><input  type='text' name='cost'  class=\"cost\" size='6' maxlength='6' value='0.00' class='combo3' rel='code_id' title='' style='color:#003399; text-align:left; font-size:1.1em; border-left: none; border-right: none; border-bottom: none'></td>
            </tr>
            </form>";

            ?>

I hope this is doable and that I have describe my intention properly. Any thoughts would be appreciated!

Mossa

Dani AI

Generated

Short summary for (and thanks for the pointer): the core issue is trying to inject server-generated markup that also contains inline <script> and single-form JavaScript into a page that will host multiple rows. That pattern is brittle (duplicate IDs, global functions that assume one set of controls, echoed <script> blocks that may not run as expected). Two reliable approaches are: return a pure HTML row fragment from PHP and append it, or return JSON and build the row on the client.

Recommended steps and best practices

  • Make the server endpoint return only the row HTML (no <script> tags) or return JSON. Give inputs array-style names (for example name="quantity[]") or unique identifiers so multiple rows submit correctly. Avoid duplicating script includes in the fragment — load jQuery and your JS once on the page.
  • Use data attributes for per-option metadata (for example data-desc and data-price) or expose a JSON product catalogue via json_encode(); both avoid extra round-trips when filling description/price.
  • Use delegated event handlers so dynamic rows inherit behavior without manual rebinds.

Minimal client-side pattern (concept only)

// fetch a ready-made row fragment
$(document).on('click', '#add-row', function(){
  $.get('row-fragment.php', function(html){
    $('.row-item:last').after(html);
  });
});

// handle selects inside any row
$(document).on('change', '.product-select', function(){
  var $row = $(this).closest('.row-item');
  var $opt = $(this).find('option:selected');
  $row.find('.desc').val($opt.data('desc') || '');
  $row.find('.price').val($opt.data('price') || '');
});

Troubleshooting & cautions

  • Do not echo JS initialization from the fragment; use delegated handlers instead.
  • Escape server output (htmlspecialchars) and use PDO or mysqli instead of deprecated mysql_* functions.
  • If the catalog is large, serve product metadata as JSON and build options client-side to keep the DOM light.
    These changes will make adding rows via AJAX robust and maintainable.

Recommended Answers

All 2 Replies

post this question PHP forum you will get replies

Thanks, anand01. I have posted in php forum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.