0

Passing a table cell value from javascript variable into php variable.

<script>
  $(document).on('ready',function(){
    $('#business_list_table').on('click','.view_btn',function (){
      $.ajax({
      url: "test.php",
      method: "POST",
      data:{business_id : "6510-1"},
      success: function (data){
        $('#business_permit_table').html(data);
        }
      });
    });
  });

<?php
$business_id = $_GET["business_id"];
echo $business_id;
Shadow
  • 9
  • 2
  • what do you mean "pass value from JS to PHP"? You can't do that – yqlim Dec 14 '18 at 03:42
  • 1
    You have SQL injection and XSS vulnerabilities. – SLaks Dec 14 '18 at 03:43
  • 1
    I don't see any injection vulnerability. Only XSS. The only query I see is a static string: `SELECT * FROM business_tb WHERE Business_ID=''` – Paul Dec 14 '18 at 03:50
  • I think you're mixing up server-side code (PHP) and client side JavaScript which runs in the user's browser only after the PHP code has output the HTML and JavaScript. If you want to send another HTTP request to your server from client side code you should look into ajax. – Paul Dec 14 '18 at 03:53
  • i just directly used select to shorten the code but the actual i converted it to json. – Shadow Dec 14 '18 at 03:53
  • i tried to directly put a value document.writeln(sample)"; echo $sample; it works fine but when i tried to get a vlue from a table cell it echo's undefine – Shadow Dec 14 '18 at 03:58
  • That's the same as doing `document.writeln(sample)'; ?>`. You're outputting a script and then when that script runs client side it prints the value of `sample`. – Paul Dec 14 '18 at 04:02
  • Try running `echo "
    $sample
    ";` You can't run client-side JavaScript on your server, you can only output scripts that then run in the browser.
    – Paul Dec 14 '18 at 04:04
  • so what's the correct way sir?what can you advice? – Shadow Dec 14 '18 at 04:07
  • Do some reading up on "ajax". You can use it to make additional HTTP requests to your server from client-side JavaScript. You can send any data you want from the browser that way. – Paul Dec 14 '18 at 04:09
  • okay sir thank you – Shadow Dec 14 '18 at 04:10
  • 1
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Rotimi Dec 14 '18 at 05:28
  • i'd used ajax and my problem was solved. thanks @Paulpro. – Shadow Dec 14 '18 at 08:04
  • You're welcome. Glad to hear that you got it working. – Paul Dec 14 '18 at 17:53

2 Answers2

2

You cannot use JS variable directly to PHP like that. use ajax instead:

JS

$("#business_list_table").on('click', '.view_btn', function post() {
    // get the current row
    var currentRow = $(this).closest("tr");
    var Business_id_value= currentRow.find("td:eq(1)").text(); // get current row 2nd T;

    $.post('', {Business_ID: Business_id_value}, function(result){
        $('table tbody').html(result);
    });

});

PHP

if (isset($_POST['Business_ID'])) {
    $Business_ID = $_POST['Business_ID'];

    $conn = mysqli_connect("localhost", "root", "", "bpsystem");
    if ($conn->connect_error) {
        die("Database connection failed:" . $conn->connect_error);
    } else {
        $sql = "SELECT * FROM business_tb WHERE Business_ID='$Business_ID';";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            // output data of each row
            while ($row = $result->fetch_assoc()) {
                echo "<tr >";
                echo "<td>BUSINESS NAME</td>";
                echo "<td>" . $row['Business_Name'] . "</td>";
                echo "</tr>";
                echo "<tr >";
                echo "</tr>";
            }
        }
    }
}
ACD
  • 1,431
  • 1
  • 8
  • 24
0

You can use the query string to pass the variable to PHP. Like this,

$("#business_list_table").on('click', '.view_btn', function post() {
    // get the current row
    var currentRow = $(this).closest("tr");
    var Business_id_value= currentRow.find("td:eq(1)").text(); // get current row 2nd T;
    window.location.href = 'http://your_url?b_id=' + Business_id_value;

});

Now you can access the Business_id_value varible in your PHP script using $_GET['Business_id_value']

Ropali Munshi
  • 2,757
  • 4
  • 22
  • 45