PHP和javascript结合变量问题

时间:2013-11-10 16:22:08

标签: javascript php jquery post

我有一个PHP文件,我在其中显示一个包含mysql表数据的html表(HTML)。

<?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

那么,如何对另一个php文件进行POST并发送POST变量:name,namefirmx,idfirmx,myID

我不确定如何进行POST。

我相信除了javascript之外没有其他方法可以调用POST。 (请记住,必须在行单击时进行POST)

请帮帮我......

3 个答案:

答案 0 :(得分:1)

您可以随时在js代码中添加php变量,如下所示:

<?php 
$x="ABC";
?>
<script language="javascript">
jsX="<?php echo $x;?>";
alert(jsX);
</script>

答案 1 :(得分:1)

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>  

答案 2 :(得分:1)

我使用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();
        }