在模态窗口中将 javascript 传递给 php [英] Pass javascript to php in modal window

查看:26
本文介绍了在模态窗口中将 javascript 传递给 php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个模态窗口中将一个 javascript 变量传递给 php,所有这些都在同一页面上,即 edit.php.(稍后我会弄清楚如何用单独的页面来做到这一点).同时,这段 javascript 代码用于将变量传递给模态:

 

在模态窗口中,这是我一直在使用的代码:

 

在表单内部,我有我的 php 代码,我在其中成功地从 javascript 中检索了 groupId,并且可以使用输入标签将其打印到屏幕上.代码如下:

 ";回声组ID:".$id;$sql = "SELECT mygroup FROM mytable WHERE pk_tId = '" .$id ."'";$result = mysql_query($sql);如果(mysql_data_seek($result)){$row = mysql_fetch_row($result);}别的 {echo "无法搜索行:".mysql_error() ."\n";}?>

sql 语句在数据库中找不到 $id,因为 $id 被打印为输入标签.如何从输入标签中提取 groupId 以便我的 sql 语句可以工作?

请举例.提前致谢.

解决方案

我最终向模态 a href 添加了一个 onclick 函数,并创建了一个 cookie,一旦模态打开,我就会抓取它.我在关闭模态时使用 javascript 清除了 cookie.

I am trying to pass a javascript variable to php in a modal window, all on the same page, which is edit.php. (I'll figure out how to do it with separate pages later). In the meanwhile, this javascript code works to pass the variable to the modal:

 <script type="application/javascript">
 $(document).on("click", ".open-EditRow", function () {
   var myGroupId = $(this).data('id');
   $(".modal-body #groupId").val( myGroupId );
 });
 </script>

In the modal window, here is the code I have been working with:

 <div class="modal hide fade" id="myEditModal">
 <div class="modal-body">
 <form class="well-small" action="" method="POST" id="modalForm" name="modalForm">
   <?php ********* code *********** ?>
 </form>
 </div>
 </div>

Inside the form is where I have my php code where I successfully retrieve the groupId from the javascript and where I am able to print it to the screen with an input tag. Here is the code:

 <?php
   $id = "<input type=\"text\" name=\"groupId\" id=\"groupId\" value=\"\" />";
   echo "Group Id: " . $id;
   $sql = "SELECT mygroup FROM mytable WHERE pk_tId = '" . $id . "'";
   $result = mysql_query($sql);
   if(mysql_data_seek($result)){
     $row = mysql_fetch_row($result);
   }
   else {
     echo "Cannot seek row: " . mysql_error() . "\n";
   }
 ?>

The sql statement cannot find $id in the database because $id is being printed as an input tag. How can I extract the groupId out of the input tag so that my sql statement will work?

Example, please. Thank you in advance.

解决方案

I eventually added an onclick function to the modal a href and created a cookie which I grabbed once the modal was opened. I used javascript to clear the cookie when I closed the modal.

这篇关于在模态窗口中将 javascript 传递给 php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆