Calling a JS scipt to run a php file and then post html to the relative row
<p>Follow this if you can...</p>

<p>Basically i have an order form (which begins with one row). </p>

<pre><code>&lt;form id="orderform" name"orderForm" action="/secure/delivery-details.html" method="post"&gt;
&lt;a  id="add"&gt;+&lt;/a&gt;
 &lt;table id="ordertable" width="533" border="0" cellspacing="0" cellpadding="2"&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td width="33%"&gt;Product Code (e.g 66203)&lt;/td&gt;
      &lt;td width="33%"&gt;Mtrs Required (e.g 10)&lt;/td&gt;
      &lt;td width="33%"&gt;Preview Image&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr class="item"&gt;
      &lt;td class="prodcode"&gt;&lt;input type="text" name="prodcode[]" id="prodcode" /&gt;&lt;/td&gt;
      &lt;td class="meterage"&gt;&lt;input type="text" name="meterage[]" id="meterage" /&gt;&lt;/td&gt;
      &lt;td class="imgsample"&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  &lt;button&gt;Submit&lt;/button&gt;
&lt;/form&gt;
</code></pre>

<p>Notice the link with an ID of "add". When checked this adds a new row to the table with the same ID.  Using the code below.</p>

<pre><code>    var counter = 0;
    //Order Form
    $("#add").click(function() {
        counter++;
        var cln = $('#ordertable tbody&gt;tr:last').clone(true);
//        cln.find("[id^='prodcode']").each(function(i, val) {
//          val.id = val.id.match(/^([^0-9]+)[0-9]*$/)[1] + "" + counter;
//      });
        cln.insertAfter('#ordertable tbody&gt;tr:last');
        $('#ordertable tbody&gt;tr:last input').val('');
        $('td.imgsample:last a').remove();
        return false;
    });

    //Check for image preview
    $("#prodcode").blur(function() {
        var $this = $(this);
        $this
            .closest('tr') // find the parent tr
            .find('td.imgsample') // find the imgsample in the row
            .html( $(this).attr('id')) // update the contents
            //.animate({'opacity':1},200);

        var imgsample = $this.closest('tr').find('td.imgsample')

        $.post('/public/themes/lbd/js/searchimage.php', //this page reads the image code and gives you the image location
                 { action: 'searchimage', imgreference: $(this).val() },
                function(data) {imgsample.html(data);}
                );
    });
</code></pre>

<p>My PHP in searchimage...</p>

<p><strong>When i currently enter a product code if it is invalid it only puts the productID in td.imsample and i want it to say INVALID CODE</strong></p>

<pre><code>//Find image based on Product Code
function findimage($imageToFind) { 

  require '../../../../config.php';
  $dbh = new PDO(DB_DSN, DB_USER, DB_PASS);

  $sql = "SELECT * FROM isproducts WHERE prodCode = ".strtoupper($imageToFind).""; 
  $stmt = $dbh-&gt;query($sql);
  $obj = $stmt-&gt;fetch(PDO::FETCH_OBJ);
  $count = $stmt-&gt;rowCount();
  if($count &gt; 0) {
      $sql2 = "SELECT * FROM imageindex WHERE filename LIKE '".strtoupper($imageToFind)."-%'"; 
      $stmt2 = $dbh-&gt;query($sql2);
      $obj2 = $stmt2-&gt;fetch(PDO::FETCH_OBJ);
      echo ($stmt2-&gt;rowCount() == 1 ? '&lt;a href="'.URL_PUBLIC.$obj2-&gt;path.'/'.$obj2-&gt;filename.'" class="customGal imgfound"&gt;&lt;span&gt;'.$obj2-&gt;path.'/'.$obj2-&gt;filename.'&lt;/span&gt;&lt;/a&gt;&amp;nbsp;&lt;a href="#" class="del"&gt;-&lt;/a&gt;' : 'No Image Available');    
  } else { 
    echo 'Invalid Code';
  }

}

//Call Function
findimage($_POST['imgreference']);
</code></pre>
