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;

14
  • what do you mean "pass value from JS to PHP"? You can't do that Commented Dec 14, 2018 at 3:42
  • 1
    You have SQL injection and XSS vulnerabilities. Commented Dec 14, 2018 at 3: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='<script>document.writeln(Business_ID)</script>' Commented Dec 14, 2018 at 3: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. Commented Dec 14, 2018 at 3:53
  • 1
    Possible duplicate of What is the difference between client-side and server-side programming? Commented Dec 14, 2018 at 5:28

2 Answers 2

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>";
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Appreciate that you have explained the problem instead of just putting code.
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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.