简体   繁体   English

使用Javascript将td值传递到php页面

[英]Pass the td value to php page using Javascript

I have a HTML table the displays record from database. 我有一个HTML表,显示记录来自数据库。

Below is a screenshot of my table 下面是我的桌子的屏幕截图

在此处输入图片说明

There is a button in a TD (ie column 5,7,9), when I click the button I want to perform function to display a popup box with html table. button in a TD有一个button in a TD (即第5、7、9列),当我单击该按钮时,我要执行功能以显示带有html表的弹出框。

Before that I want to pass the value of mcc and mnc and also want to pass the column name (ie if i clicked the button near 5xx i want pass 5xx if 6xx I want to pass 6xx) to the page where there is a query to display the table. 在此之前,我想传递mcc and mnc的值,也想传递列名(即如果我单击5xx附近的按钮,我想传递5xx如果6xx我想传递6xx)到要查询的页面显示表格。 And have separate php code to retrieve th and td from database. 并有单独的php代码从数据库中检索th和td。

I tried but I don't know how to pass this value to javascript then to php page that contains query to display the table. 我试过了,但我不知道如何将此值传递给javascript,然后传递给包含查询以显示表的php页面。 Thanks 谢谢

This is my HTML markup: 这是我的HTML标记:

<table>
    <th style="text-align:center;width:92px">MCC</th>
    <th style="text-align:center;width:92px">MNC</th>
    <th style="text-align:center;width:92px">MNP</th>

    <?
    $ColumnNames = mysql_query("SELECT column_name FROM information_schema.COLUMNS WHERE table_name = 'supplierprice' AND column_name NOT
    IN ('supp_price_id','region', 'country', 'networkname', 'mcc', 'mnc', 'mnp'
    )") or die("mysql error"); 

    $columnArray=array();
    $i = 0;

    while($rows = mysql_fetch_array($ColumnNames))
    {

    $columnArray[]=$rows[0];

    echo "<th>" . $columnArray[$i] . " <span> <img id='logo'  src='/image/Picture2.png' style='margin:-62px -21px -9px 32px'></span></th>";
    echo "<th style= 'width:20px;'></th>";            
                                   $i++;
    }
                    ?>

    <?php    
    $sql = mysql_query("SELECT * FROM supplierprice ");    
        while($rows=mysql_fetch_array($sql))
    {    
    if($alt == 1)
            {
               echo '<tr class="alt">';
               $alt = 0;
            }
            else
            {
               echo '<tr>';
               $alt = 1;
            }           
    echo '  <td class="edit region '.$rows["supp_price_id"].'">'.$rows["region"].'</td>
            <td class="edit country '.$rows["supp_price_id"].'">'.$rows["country"].'</td>
            <td class="edit networkname '.$rows["supp_price_id"].'">'.$rows["networkname"].'</td>
            <td id="mcc" class="edit mcc '.$rows["supp_price_id"].'">'.$rows["mcc"].'</td>   
            <td id="mnc" class="edit mnc '.$rows["supp_price_id"].'">'.$rows["mnc"].'</td>
            <td class="edit mnp '.$rows["supp_price_id"].'">'.$rows["mnp"].'</td>';           

    $ColumnNames = mysql_query("SELECT column_name FROM information_schema.COLUMNS WHERE table_name = 'supplierprice' AND column_name NOT
    IN ('supp_price_id','region', 'country', 'networkname', 'mcc', 'mnc', 'mnp'
    )") or die("mysql error"); 
    $columnArray=array();
    $i=0;
    while($rows1=mysql_fetch_array($ColumnNames))
    {               
    $columnArray[]=$rows1[0];    
    echo '<td  width="0px;" class="edit '.$columnArray[$i].' '.$rows["supp_price_id"].'">'.$rows[$columnArray[$i]].'</td>';  
    echo '<td><input type="button" onclick="myFunction()" value="" /></td>';        
       $i++;
        }            
         echo '</tr>';
                }  ?>
</table>

Javascript Java脚本

<script>

function myFunction() {

 // Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "testpage2/config.php";
var mcc = document.getElementById("mcc").value;
var mnc = document.getElementById("mnc").value;
var vars = "mcc="+mcc+"&mnc="+mnc;
hr.open("POST", url, true);
hr.send(vars); 

        }
</script>

but it not passing the value of mcc and mnc also the column 但它没有传递MCC和MNC的值也列

First of all assign Id to table: 首先将ID分配给表:

<table>

like: 喜欢:

<table id="contentTable">

Now use below code of jQuery: 现在使用下面的jQuery代码:

$(document).ready(function ()
{
  $('#tableId').find('th').each(function ()
  {
    $(this).click(function ()
    {
       var thValue = $(this).html();
       alert(thValue) // here is value of th on you have click
    });
  });
});

Cheers!!! 干杯!!!

You could try to use something like "generic javascript": 您可以尝试使用类似“通用javascript”的内容:

<?php //some stuff
$mcc = "some value";
?>
<script type="text/javascript">
var myVariable = <?php echo $mss; ?>
</script>

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

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