PHP 和 JavaScript 组合变量问题
PHP and javascript combining variables issue
>我有一个PHP文件,我在其中显示一个html表(HTML),其中包含来自mysql表的数据。
<?php
include 'connexion.php';
session_start();
$idfirm = $_REQUEST['idfirm'];
$namefirm = $_REQUEST['namefirm'];
表中的行具有
<tr class="clickableRow"...
然后在行单击(javascript)时,我希望能够调用(POST)另一个PHP文件,该文件将根据表行中的某些信息显示其他信息。我很难做到这一点。
到目前为止,我所做的是:
echo '
...
jQuery(document).ready(function($) {
$(".clickableRow").click(function() {
var tableData = $(this).children("td").map(function() {
return $(this).text();
}).get();
// So I can access the field data either like this
var myID = $.trim(tableData[0]);
// or like this:
var name = $(this).closest(''tr'').find(''td:eq(1)'').text();
alert(myID);';
echo" // here I would need to access some variables that I received above in PHP from POST
var namefirmx = '{$namefirm}';
var idfirmx = '{$idfirm}';
";
// and here I would like to call another PHP file (with POST) that will display info about the employee
那么,我怎样才能将 POST 发送到另一个 php 文件并发送 POST 变量:name,namefirmx,idfirmx,myID
我不确定如何进行开机自检。
我相信除了javascript之外,没有其他方法可以调用POST。(请记住,必须在单击行时进行开机自检)
请在这里帮助我...
php 脚本的快速示例,缺少一些 html 标签,但我想你会得到一张图片:
<?php
print_r($_POST);
?>
<script src="http://codeorigin.jquery.com/jquery-1.10.2.js"></script>
<script>
jQuery(document).ready(function($) {
$(".clickableRow").click(function() {
$(this).find("form").submit();
});
});
</script>
<table border="1">
<tr class="clickableRow">
<td>
<form method="post"><input type="hidden" name="p1" value="v1"><input type="hidden" name="p2" value="v2"></form>
v1
</td>
<td>v2</td>
</tr>
</table>
我使用SO上一个问题解决了我的问题。我不确定如何处理这个问题。无论如何,答案是:在javascript中添加一个函数,并使用我的变量调用它。函数为:
function post_to_url(path, params, method) {
method = method || "post"; // Set method to post by default if not specified.
// The rest of this code assumes you are not using a library.
// It can be made less wordy if you use one.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
for(var key in params) {
if(params.hasOwnProperty(key)) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
}
您可以随时在 js 代码中添加 php 变量,如下所示:
<?php
$x="ABC";
?>
<script language="javascript">
jsX="<?php echo $x;?>";
alert(jsX);
</script>
相关文章:
- 简单的变量问题
- 局部全局变量问题 - JavaScript
- 递归承诺调用 - 内存范围变量问题
- 开关变量问题
- 简单的 Jquery 变量问题
- Javascript全局变量问题
- Javascript-通过引用传递变量问题
- AJAX请求在jQuery中获取函数,变量问题
- JSON JavaScript 未定义的变量问题
- PHP 和 JavaScript 组合变量问题
- JQuery Ajax 填补了 JavaScript 变量问题
- jQuery.ParseJSON 变量问题
- Ajax变量问题
- socket.io-使用命名空间时出现套接字变量问题
- AngularJS与SignalR$范围变量问题
- 使用事件处理程序(基本Javascript)时出现全局变量问题
- JavaScript 声明变量问题
- JavaScript变量问题的澄清
- jQuery变量问题
- JS -访问全局变量问题