如何使用javascript将动态数据从html数据属性传递到查询? [英] How to pass dynamic data from html data-attribute to a query using javascript?
本文介绍了如何使用javascript将动态数据从html数据属性传递到查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个按钮,用于保存数据库中的数据
I have this button that hold data from database
<button title="View Conversation" type='button' class='btn btn-default btn-sm' data-toggle='modal' data-target='#viewConversationModal'
data-empproj_id="<?=$employeeproject['emproj_id'];?>"
data-empprojconvoid="<?=$employeeproject['convofeed_id'];?>"
**data-empprojconvotoempid**="<?=$employeeproject['toemployee_id'];?>"
**data-empprojconvofromemip**="<?=$employeeproject['fromemployee_id'];?>"
data-empprojconvoconversation='"<?=$employeeproject['conversation'];?>"' >
</button>
我需要在查询中使用数据
I need to use the data inside a query
<?php
$toemployee = data-empprojconvotoempid
$fromemployee = data-empprojconvofromemip
$convoQ = "SELECT * FROM projects as p
JOIN employeeprojects AS ep ON p.project_id = ep.project_id
JOIN employees AS e ON ep.employee_id = e.employee_id
JOIN clients AS c ON p.client_id = c.id
JOIN employeeprojects_conversation AS epc ON ep.employee_id = epc.toemployee_id
WHERE epc.toemployee_id=**$toemployee** AND epc.fromemployee_id=**$fromemployee**";
$displayConvoResult=mysqli_query($db, $convoQ);
;?>
然后发表一会儿陈述,以模态形式显示对话
then make a while statement to display conversation in the modal
<?php while($conversation=mysqli_fetch_array($displayConvoResult)){ ?>
<div class='row convorow'>
<div class='col-md-6 pull-left'>
<p style="font-size: smaller;">Messenger A :</p>
<textarea readonlyrows="4" cols="50"></textarea>
</div>
<div class='col-md-6 pull-right'>
<p style="font-size: smaller;">Messenger B :</p>
<textarea readonlyrows="4" cols="50"></textarea>
</div>
</div>
<?php } ;?>
到目前为止的
脚本
script so far
$('#viewConversationModal').on('show.bs.modal', function(con){
var button = $(con.relatedTarget);
//get data
var empprojconvotoempid = button.data('empprojconvotoempid');
var empprojconvofromemip = button.data('empprojconvofromemip');
});
推荐答案
为清楚起见:
- PHP在 后端 中运行
- JavaScript,HTML在 前端 (浏览器) 中运行
- PHP runs in the Back-end
- JavaScript, HTML run in the Front-end (Browser)
现在要将数据从前端发送到服务器,您可以执行以下操作:
Now to send the data to the server from the Front-End you can do something like:
$("button").click(_ => {
const me = $(this)
$.ajax({
url: window.location,
method: "post",
contentType: "application/json",
data: {
empprojToId: me.attr("data-empprojconvotoempid"),
empfromEmIp: me.attr("data-empprojconvofromemip"),
},
})
})
在您的情况下,您宁愿让按钮执行正常的发布并使用模态和新对话再次呈现整个页面?
It seems in your case, that you would rather have the button perform a normal post and render the entire page again with the modal, and new conversations?
如果是这样,则可以将所有数据以html格式放置:
If so, then you can put all your data in a html form:
<form method="post">
<input name="empproj_id" value="<?=$employeeproject['emproj_id'];?>">
...
<button type="submit">
</form>
<!--Insert Modal Code here-->
这篇关于如何使用javascript将动态数据从html数据属性传递到查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文