PHP 和 JavaScript 组合变量问题

PHP and javascript combining variables issue

本文关键字:变量 问题 组合 JavaScript PHP      更新时间:2023-09-26

>我有一个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>