How to pass selected value in modal field of PHP?

I have code to initialize a modal window:

<script> var grid_modal_options = { height: 'auto', width: '80%', modal: true }; function showProductsModalBox() { $("#products_modal_box").dialog(grid_modal_options); $("#products_modal_box").parent().appendTo('form:first'); } </script> <div id="products_modal_box" title="Products" style="display: none;"> <div class="in"> <div class="grid-12-12"> <form id="products_modal_box_form" action="#" method="post"> <table> <thead> <tr> <th>&nbsp;</th> <th>Product</th> </tr> </thead> <!-- Query for read mysql goes here (I skipped this line because it not the main thing I'm gonna ask since it run well) /--> <tbody> <?php //read the results while($fetch = mysqli_fetch_array($r)) { print "<tr>"; print " <td><a href='#'>Choose</a></td>"; //--> How to return the value of $fetch[0]? print " <td>" . $fetch[0] . "</td>"; //$fetch[0] == Product ID print "</tr>"; } ?> </tbody> </table> </form> </div> </div> </div> 

and

 <input type='text' id='products_id_textbox' name='products_id_textbox' /> <a href='#' onclick='showProductsModalBox(); return false;'>Choose products</a> 

The code successfully displays the modal block. But how to return the "Product" selected by the user in the text box "products_id_textbox"? Thanks.

+1
source share
2 answers

add inline javascript:

 <?php while($fetch = mysqli_fetch_array($r)) { print "<tr>"; print " <td><a href=\"javascript:;\" onclick=\"$('#products_id_textbox').val('".$fetch[0]."');$('#products_modal_box').dialog('close');\">Choose</a></td>"; //--> How to return the value of $fetch[0]? print " <td>" . $fetch[0] . "</td>"; //$fetch[0] == Product ID print "</tr>"; } ?> 
+1
source

Instead of using inline JS, you can write like this (or something like this);

 // php while($fetch = mysqli_fetch_array($r)) { print "<tr>"; print " <td><a rel='dialog' data-id='{$fetch[0]}'>Choose</a></td>"; print " <td>{$fetch[0]}</td>"; print "</tr>"; } // js $(document).ready(function(){ ... $("a[rel=dialog]").each(function(){ var id = this.getAttribute("data-id"); $('#products_id_textbox').val(id); $('#products_modal_box').dialog('close'); }); 
+1
source

Source: https://habr.com/ru/post/1440224/


All Articles