如何将模式框上选择的值传递给PHP?
我有初始化模式框的代码:如何将模式框上选择的值传递给PHP?,php,jquery,modal-dialog,Php,Jquery,Modal Dialog,我有初始化模式框的代码: <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(
<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> </th>
<th>Product</th>
</tr>
</thead>
<!-- Query for read mysql goes here (I skipped this line because it's not the main thing I'm gonna ask since it's 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>
var grid_modal_选项={
高度:“自动”,
宽度:“80%”,
莫代尔:对
};
函数showProductsModalBox(){
$(“#产品"模式"框”)。对话框(网格"模式"选项);
$(“#products_modal_box”).parent().appendTo('form:first');
}
产品
以及:
代码成功显示模式框。但是,如何将用户选择的“产品”返回到文本框“products\u id\u textbox”?谢谢。添加内联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>";
}
?>
您可以使用内联JS编写代码(或类似的代码)
//php
而($fetch=mysqli_fetch_数组($r)){
打印“”;
打印“选择”;
打印“{$fetch[0]}”;
打印“”;
}
//js
$(文档).ready(函数(){
...
$(“a[rel=dialog]”)。每个(函数(){
var id=this.getAttribute(“数据id”);
$('#products_id_textbox').val(id);
$(“#产品(模式)框”)。对话框('close');
});
@Mihailorga:效果很好!!很简单!:)如果你不介意的话,我还有一个问题,如何在我选择一个链接后自动关闭模式框?我已经修改了我的答案…$(“#产品"模式框”)。close();
像我上面说的那样添加抱歉…这是.dialog('close')
<?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>";
}
?>
// 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');
});