Xavi
Xavi

Reputation: 2594

Pass the td value to php page using Javascript

I have a HTML table the displays record from database.

Below is a screenshot of my table

enter image description here

There is a button in a TD (i.e column 5,7,9), when I click the button I want to perform function to display a popup box with html table.

Before that I want to pass the value of mcc and mnc and also want to pass the column name (i.e 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. And have separate php code to retrieve th and td from database.

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. Thanks

This is my HTML markup:

<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

<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

Upvotes: 0

Views: 1570

Answers (2)

Dharmesh Thanki
Dharmesh Thanki

Reputation: 420

First of all assign Id to table:

<table>

like:

<table id="contentTable">

Now use below code of 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!!!

Upvotes: 1

alex
alex

Reputation: 80

You could try to use something like "generic javascript":

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

Upvotes: 0

Related Questions