简体   繁体   English

将php数据/变量传递给模式

[英]passing php data/variable to a modal

I have some data being created(generated from database) using a while loop. 我有一些数据正在使用while循环创建(从数据库生成)。 Each set of data carries some form of id and its corresponding button is added to it. 每组数据都带有某种形式的id,并向其添加了相应的按钮。 Now, when I click a button, I'd like to pass the id(single php variable) of the data related to this button to a modal window(that has a form) that pops and also make it the value of an input field. 现在,当我单击一个按钮时,我想将与此按钮相关的数据的id(单个php变量)传递给弹出的模态窗口(具有形式),并使其成为输入字段的值。 Here's my code so far: 到目前为止,这是我的代码:

The data from database: 来自数据库的数据:

<?php while ($data = mysqli_fetch_assoc($data_set)) { ?>
<div class="w3-section w3-row w3-card-2">
  <div class="w3-half">
    <div class="apartment-image w3-card-4">
      <header class="w3-container">
        <h4><?php echo $data['data_name']; ?></h4>
      </header>
      <div class="w3-container">
        <img src="<?php echo "images/".$data['Image1']; ?>" class="w3-image" alt="<?php echo $data['dataimgname']; ?>">
      </div>
      <footer class="w3-contanier w3-border-top">
        <div class="w3-border-right">
          <button class="w3-btn w3-btn-block" type="button" name="btnBook" id="modalOpener" onclick="document.getElementById('book').style.display='block';">
            Book or Request Tour
          </button>
        </div>

The Modal: 模态:

<div id="book" class="w3-modal">
<div class="w3-modal-content">
  <header class="w3-container">
    <span onclick="document.getElementById('book').style.display='none'" class="w3-closebtn">
      &times;
    </span>
    <h2>Book or Request Tour</h2>
  </header>
  <div class="w3-container">
    <form class="w3-form" action="bookntour-handler.php" method="post">
      <div class="w3-group">
        <label class="w3-label" for="fullname">Full Name:</label>
        <input class="w3-input" type="text" name="fullname" value="">
      </div>
      <label class="w3-label" for="email">E-mail:</label>
      <input class="w3-input" type="email" name="email" value="">
      <label class="w3-label" for="telephone">Telephone:</label>
      <input class="w3-input" type="text" name="telephone" value="">
      <label class="w3-label" for="dataname">Data Name:</label>
      <input class="w3-input" type="text" name="dataname" value="">
      <div class="w3-group">
        <input class="w3-btn" type="submit" name="btnSubmit" value="Submit">
      </div>
    </form>
  </div>
  <footer>
  </footer>
</div>

Like I said earlier, I want to pass the id associated with the button but from my code there is nothing that mentions about "id" so let's use. 就像我之前说的,我想传递与按钮关联的id,但是在我的代码中没有提到“ id”,因此让我们使用。

<?php echo $data["data_name"]; ?>

When I open the modal window, I'd like this variable to be made as the value for input: 当我打开模式窗口时,我希望将此变量作为输入值:

 <label class="w3-label" for="dataname">Data Name:</label> <input class="w3-input" type="text" name="dataname" value=""> 

I have looked at a couple of options so far but most of them seem unnecessary in my case or I simply don't understand them. 到目前为止,我已经研究了几种选择,但是对于我来说,其中大多数似乎都是不必要的,或者我根本不理解它们。 Like this and this and this . 喜欢这个这个这个 This seems like a workable solution but it requires me to fetch the whole data again, from the database(using AJAX) which I do not want. 似乎是一个可行的解决方案,但它需要我再次从不需要的数据库(使用AJAX)中获取全部数据。 I just want that one value. 我只想要一个值。

I am using W3CSS for my layouts and creating the modal. 我将W3CSS用于布局并创建模态。 A solution in jQuery/JavaScript or PHP(or both) will be appreciated. jQuery / JavaScript或PHP(或两者)中的解决方案将不胜感激。 Thank you! 谢谢!


Update 更新

So, I somehow managed to solve part of the problem. 因此,我设法设法解决了部分问题。 I added this function to my external JavaScript file: 我将此功能添加到我的外部JavaScript文件中:

 function showModal() { document.forms["bookntourForm"]["apartmentno"].value = document.getElementsByClassName("modalOpener")[0].getAttribute("value"); document.getElementById('book').style.display='block'; } 

Then the code for the button that calls the above function looks like this: 然后,调用上述函数的按钮的代码如下所示:

 <button class="w3-btn w3-btn-block modalOpener" type="button" name="btnBook" onclick="showModal()" value="<?php echo $data['data_name']; ?>"> Book or Request Tour </button> 

This brings up a new problem. 这带来了一个新问题。 Whenever I open the modal, the value for <input class="w3-input" type="text" name="dataname" value=""> on the modal is the same for all modals although they are different when each button is generated. 每当我打开模态时,模态上的<input class="w3-input" type="text" name="dataname" value="">对于所有模态都是相同的,尽管每个按钮都不同产生。

You can pass your id through ajax to the backend and retrieve necessary data regarding that ID by using a query, Then set values to the necessary place inside the success method. 您可以通过ajax将ID传递到后端,并通过使用查询来检索有关该ID的必要数据,然后将值设置为成功方法内的必要位置。 As you say, I don't think as a good suggestion to use data_name instead of id. 如您所说,我不建议使用data_name代替id。 you can set your id in hidden field and use it. 您可以在隐藏字段中设置您的ID并使用它。 because "data_name" will not be good solution to retrieve data by unique field. 因为“ data_name”不是按唯一字段检索数据的好方法。

I don't know if I have done this in a proper manner, but it was as simple as this. 我不知道我是否以适当的方式完成了此操作,但这很简单。 I changed the value of onclick attribute of the button to: 我将按钮的onclick属性的值更改为:

  <button class="w3-btn w3-btn-block modalOpener" type="button" name="btnBook" onclick="showModal('<?php echo $apartment["ApartmentNo"]; ?>')"> Book or Request Tour </button> 

The showModal() function in the external JS now looks like this: 现在,外部JS中的showModal()函数如下所示:

 var aptNo; function showModal(aptNo) { document.forms["bookntourForm"]["apartmentno"].value = aptNo; document.getElementById('book').style.display='block'; } 

It now works the way I wanted. 现在可以按照我想要的方式工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM