Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Tips to improve your code;</p> <ol> <li><p><strong>Don't mix PHP and Javascript code.</strong> Your asking for trouble and it just doesn't need to be done. If you need information from PHP available in your Javascript, then use <code>json_encode</code> -- don't write literally to the Javascript file like so:</p> <pre><code>var userId = "&lt;?php echo $_GET['user_id']; ?&gt;" var postId = "&lt;?php echo $_GET['post_id']; ?&gt;" </code></pre> <p>Instead have a PHP file that generates JSON object and have that loaded by a script tag:</p> <pre><code>&lt;script type="text/javascript" src="/get_json.php"&gt;&lt;/script&gt; </code></pre> <p>Inside get_json.php will be this magic:</p> <pre><code>&lt;?php $json['user_id'] = $_GET['user_id']; $json['post_id'] = $_GET['post_id']; echo json_encode($json); ?&gt; </code></pre></li> <li><p><strong>Don't repeat code.</strong> All your <code>f1</code> <code>f2</code> are all unneeded. Why not just put this into a Javascript array, and <code>.join('&lt;/td&gt;&lt;td&gt;')</code> and add <code>&lt;td&gt;</code> and <code>&lt;/td&gt;</code> onto the end. </p> <pre><code>var inputsArray = ['&lt;input name="email"/&gt;','&lt;input name="fullname"/&gt;']; var tdString = '&lt;td&gt;' + inputsArray.join('&lt;/td&gt;&lt;td&gt;') + '&lt;/td&gt;'; </code></pre> <p>Although I really don't see why you need PHP to help you create a form and HTML, especially as it's not even using any information specific within your PHP application. It's completely pointless.</p></li> <li><p><strong>Use selectors appropriately and cache.</strong></p> <pre><code>$('#'+par+' input[name="pkwiu[]"]').val(data.product_pkwiu); $('#'+par+' input[name="netto[]"]').val(data.product_netto); </code></pre> <p>Doing this over doing <code>$('#'+par+'</code> over and over is inefficient. Use the context parameter to help you</p> <pre><code>var $id = $('#' + par); $('input[name="pkwiu[]"]', $id).val(data.product_pkwiu); </code></pre> <p>Notice the second parameter of <code>$</code> is used, which is telling jQuery to select inputs within that context (id of an element in this case). This allows us cache that id so jQuery isn't constantly trying to find it through each iteration.</p></li> <li><p><strong>Don't tie yourself to your own HTML structure.</strong></p> <p>Avoid using parent parent parent parent parent parent, you get the idea. It'll cause headaches when you make even simple changes in your HTML structure</p> <pre><code>var par = $(this).parent().parent().attr('id')` </code></pre> <p>Instead it's far better to utilise the <code>.closest(selector)</code> (which travels up the DOM and stops when it reaches a match) <code>.parents(selector)</code> (which travels up the DOM and matches ALL elements for the selector up until the root, which is usually the body tag)</p> <p>Without seeing your HTML structure, it's hard to tell what <code>.parent().parent()</code> could be replaced with, but lets guess it's the form element:</p> <pre><code>var par = $(this).closest('form').attr('id')` </code></pre></li> </ol> <p>Ok it's getting a little long now. There's plenty of other things I can pick apart, but for now, that's all folks!</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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