Note that there are some explanatory texts on larger screens.

plurals
  1. POFill multiple fields from click with jquery mysql
    primarykey
    data
    text
    <p>I do have a dynamic grid that shows items from MySQL table using PHP. I have a icon that is supposed to get data from MySQL based in the id of the current register and fill the fields in the form with related information:</p> <p>PHP/HTML grid</p> <pre><code>$output .='&lt;tr id="'.$id.'"&gt;'; $output .='&lt;td&gt;'.$description.'&lt;/td&gt;'; $output .='&lt;td&gt;'.$description_pt.'&lt;/td&gt;'; $output .='&lt;td align="right"&gt;US$'.number_format($price, 2, '.', ',').'&lt;/td&gt;'; $output .='&lt;td align="center"&gt;'.$a_c.'&lt;/td&gt;'; $output .='&lt;td align="center"&gt;'.$note.'&lt;/td&gt;'; $output .='&lt;td align="center"&gt;'.$note_pt.'&lt;/td&gt;'; $output .='&lt;td align="center" class="icon_grid"&gt;&lt;a class="editar" title="Open the register." href="#"&gt;&lt;img src="images/Write2.gif" width="16" height="16" /&gt;&lt;/a&gt;&lt;/td&gt;'; $output .='&lt;td align="center" class="icon_grid"&gt;&lt;a class="delete" title="Delete the register." href="#"&gt;&lt;img src="images/Trash.gif" width="16" height="16" /&gt;&lt;/a&gt;&lt;/td&gt;'; $output .='&lt;/tr&gt;'; </code></pre> <p>The HTML form:</p> <pre><code>&lt;div id="edita_cadastro"&gt; &lt;form name="form3" id="form3" method="post" action="" &gt; &lt;table width="50%" border="0" cellspacing="2" cellpadding="0"&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;h3&gt;Edit the current register&lt;/h3&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td align="right"&gt;&lt;label for "edt_description"&gt;&lt;img src="images/usa.jpg" width="18" height="15" /&gt;Description:&lt;/label&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="edt_description" id="edt_description" size="45" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td align="right"&gt;&lt;label for "edt_description_pt"&gt;&lt;img src="images/brazil.jpg" width="18" height="15" /&gt;Description:&lt;/label&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="edt_description_pt" id="edt_description_pt" size="45" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td align="right"&gt;&lt;label for "edt_price"&gt;Price:&lt;/label&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="edt_price" id="edt_price" size="35" placeholder="ex.: 1.00" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;input type="hidden" name="pack_id" id="pack_id" value="&lt;?php echo $pack_id; ?&gt;" /&gt; &lt;td&gt;&lt;input type="hidden" name="editar" value="editar" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="submit" name="submit" id="submit" value="Save" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p>Now, the PHP get_prices.php</p> <pre><code>include '../connect_to_mysql.php'; $item_id = $_POST['pk_id']; // Selected item Id $query = "SELECT * from packages_prices WHERE id='$item_id'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result, MYSQL_ASSOC); $description = $row['description']; $description_pt = $row['description_pt']; $price = $row['edt_price']; $arr = array( 'edt_description' =&gt; $description, 'edt_description_pt' =&gt; $description_pt, 'edt_price'=&gt; $price); echo json_encode( $arr ); </code></pre> <p>Finally, the jQuery script:</p> <pre><code>$(document).ready(function(e) { $("#tabela tr[id]").on("click", ".editar", function () { // Tabela is the table's id var obj = $(this).closest("tr[id]"); $.ajax({ type: "POST", url: 'get_prices.php', data: { pk_id: obj.attr("id")}, dataType: "json", success: function(data, evt) { if (data.success == "true") { $("#edt_description").val(data.edt_description); $("#edt_description_pt").val(data.edt_description_pt); $("#edt_price").val(data.edt_price); } else { alert('error'); } } }); }); </code></pre> <p>So, I don't know what may be wrong. The data is not coming and, of course, not filling the fields. Does anyone knows what is going on?</p> <p>Thank you</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.
 

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