简体   繁体   中英

How to assign jQuery variable value to php variable

I'm using jQuery to generate table based on JSON output.In this code JSON value taking from a SoapClient . This working fine and here is my output

在此输入图像描述

Now i need to assign the ID column value to PHP variable and pass that PHP value to PHP funtion. I know Javascript works on the client side and PHP works on the server side. So then have any solution to do this. I used this code snippet to get 1st column value

<button onclick="myFunction(this.parentNode.parentNode.cells[0].textContent)" type="button" class="btn btn-success btn-sm">Get Value</button>

how can i assign this to PHP variable.

Here is my code

<?php
$client = new SoapClient('http://localhost:2327/ManageSemesterService.svc?wsdl');

$respoMS = $client->SelectBatchTimetableJSONPara(array('para' => "Batch 8"));
$valMS = $respoMS->SelectBatchTimetableJSONParaResult;

?>

<script>
    $(document).ready(function () {
    $('body').append('<div class="container" ></div><br>');

    var html = '<div class="container" ><table class="table table-striped"></div>';
    html += '<tr>';
    var flag = 0;

    var data2   =   <?php echo $valMS; ?>;
    $.each(data2[0], function(index, value){
        html += '<th>'+index+'</th>';
    });
    html += '</tr>';

     $.each(data2, function(index, value){

        html += '<tr>';

        $.each(value, function(index2, value2){

            if(value2 == "Java"){
                html += '<td style="background-color: #7e57c2;">'+value2+'</td>';
            }
            else{
                html += '<td>'+value2+'</td>';
            }

        });

        html += '<td><button onclick="myFunction(this.parentNode.parentNode.cells[0].textContent)" type="button" class="btn btn-success btn-sm">Get Value</button></td>';


        html += '</tr>';

     });

     html += '</table>';
     $('body').append(html);
     console.log(html);
});
</script> 

There are no possibilities of assigning a client data to server script data. You can always send data to server through Ajax and then you can process the data.

So you are needing to get the ID from a HTML Tag and pass it to PHP for Server Side processing?

Ajax is your friend! http://api.jquery.com/jquery.ajax/

$.ajax({
  url: "test.html",
  data: {this can be json}
  context: document.body
}).success(function() {
  $( this ).addClass( "done" );
});

That is a really rough example on how to do it, look at the Tech Docs for more info.

Essentially you will put your info to pass into PHP in the data section and in the url is the link to the page that parses the data into php.

The sucess function is activated if the url and data are successfully passed into php. You will need to put some error handler in your php to return if the data is not dealt with correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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