简体   繁体   English

将变量传递给 modal as <span>,然后需要在 SQL 查询中使用解析的变量</span>

[英]Passing variable to modal as <span>, then needing to use the resolved variable in SQL query

am grateful for any and all help here please.我很感激这里的任何和所有帮助。

I'm passing a data-id value to a bootstrap modal form.我正在将数据 ID 值传递给引导模式表单。 The data-id is generated from a while loop. data-id 是从 while 循环生成的。 Here is the line from within the loop...这是循环内的行...

echo "<div class='col-2'><a href='' data-toggle='modal' data-target='#modaleditprice' class='btn btn-primary btn-sm' data-id='".$bids['PriceID']."'><i class='far fa-edit'></i></a></div>";

Then I have some jquery which sends the data to the modal form...然后我有一些 jquery 将数据发送到模态表单......

$('#modaleditprice').on('show.bs.modal', function (e) {
var SentPriceID = $(e.relatedTarget).attr('data-id');
$(this).find('.ModalPriceID').text(SentPriceID);
});

Finally, here is the php and html code within the modal form itself...最后,这是模态表单本身中的 php 和 html 代码......

$PriceID = "<span class='ModalPriceID'></span>";
$priceSQL = "SELECT * FROM prices WHERE PriceID = ".$PriceID;
$priceQRY = $conn->query($priceSQL);

... ...

<div class="modal-header">
<h4 class="modal-title">Edit Price - <?php echo $PriceID; ?></h4>
<button type="button" class="close" data-dismiss="modal" aria label="Close">
<span aria-hidden="true">×</span>
</button>
</div>

 <div class="modal-body">
 <form method="POST" action="editprice.php">
 etc... 

The form is populated with the values retrieved from the SQL query.该表单填充有从 SQL 查询中检索到的值。 The form works fine if I can get the SQL query to work... The issue is that I cannot parse the query above successfully within the php segment.如果我可以让 SQL 查询工作,该表单就可以正常工作......问题是我无法在 php 段中成功解析上面的查询。 The html segment renders the correct value of $PriceID ie it first renders as the span code, which then shows the correct price ID value once the html is completed loading. html 段呈现正确的 $PriceID 值,即它首先呈现为跨度代码,然后在 html 完成加载后显示正确的价格 ID 值。 But I need to use this as a resolved variable beforehand.但是我需要事先将其用作已解析的变量。 Any ideas?有任何想法吗?

edited: I'm using two php files which may be the problem here ie I include the modal file based on a condition in my main file...编辑:我正在使用两个 php 文件,这可能是这里的问题,即我根据主文件中的条件包含了模态文件...

if (isset($_SESSION['UserRole']) && ($_SESSION['UserRole'] == 'Trader' || $_SESSION['UserRole'] == 'Viewer'))   {
// Proceed if Viewer or Trader else boot out
include_once '../../'.$config["paths"]["secured"].'pricemodals.php';

It looks like your sql statement will resolve to the following看起来您的 sql 语句将解析为以下内容

SELECT * FROM prices WHERE PriceID = <span class='ModalPriceID'></span>;

since you are string to $PriceID and not a reference.因为您是 $PriceID 的字符串而不是参考。 It also looks like you are attempting to do actions on the page within the php after the page is rendered.在页面呈现后,您似乎还试图在 php 中的页面上执行操作。 You will need to either create a small form within the modal that sends the values as form data eg $_POST/$_GET where the page will essentially refresh and process those items on the top of the page if they exist, https://www.w3schools.com/php/php_forms.asp您需要在模态中创建一个小表单,将值作为表单数据发送,例如 $_POST/$_GET,其中页面基本上会刷新并处理页面顶部的这些项目(如果它们存在), https://www .w3schools.com/php/php_forms.asp

or better yet use AJAX to send the query asynchronously and react to the response without a page load.或者更好的是使用 AJAX 异步发送查询并在没有页面加载的情况下对响应做出反应。 https://www.w3schools.com/jquery/jquery_ref_ajax.asp https://www.w3schools.com/jquery/jquery_ref_ajax.asp

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

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