Furkan Akgün
Furkan Akgün

Reputation: 15

I couldnt get datas from each html table which returns with PHP

I am trying to build small projects to improve my skills. And im stuck at a poing. if someone can help me i will be glad. (I know there is SQL injection , its just practice)

Here is My Js function that is trying to get value from PHP side.

function selectUser(element){

  var customerid = $("td[data-form-id]", element).data("form-id");
  var customername = $("td[data-customer-name]", element).data("customer-name");
  var customertel = $("td[data-customer-tel]", element).data("customer-tel");
  var devicemodel = $("td[data-device-model]", element).data("device-model");
  var acceptdate = $("td[data-accept-date]", element).data("accept-date");
  
    console.log(customerid);
    console.log(customername);
    console.log(customertel);
    console.log(devicemodel);
    console.log(acceptdate);
  }

Here is my PHP Code and this is working fine like.

$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
$formId = $row['form_id'];
$customerName = $row['customer_name'];
$customerTel = $row['customer_tel'];
$deviceModel = $row['device_modal'];
$acceptDate = $row['accept_date'];

/*Here i give each row as id thats is shown customer's id. And when i click a row i can see in the console.*/

echo "<tr onclick='selectUser(this);' data-id='" . $formId . "'>"; 
        echo "<td data-form-id='" . $formId . "'>" . $formId . "</td>";
        echo "<td data-customer-name='" . $customerName . "'>" . $customerName . "</td>";
        echo "<td data-customer-tel='" . $customerTel . "'>" . $customerTel . "</td>";
        echo "<td data-device-model='" . $deviceModel . "'>" . $deviceModel . "</td>";
        echo "<td data-accept-date='" . $acceptDate . "'>" . $acceptDate . "</td>";
        echo "<td><a>Detail</a></td>";
echo "</tr>";
}

But What i am trying to do is to read values when click on "Detail" part click not the row.

And i taking my function to Like this and it returns in console "undefined values":

<td onclick='selectUser(this);' data-id='" . $formId . "'><a>Detail</a></td>

And here is the how it looks like all codes:

echo "<tr>";
        echo "<td data-form-id='" . $formId . "'>" . $formId . "</td>";
        echo "<td data-customer-name='" . $customerName . "'>" . $customerName . "</td>";
        echo "<td data-customer-tel='" . $customerTel . "'>" . $customerTel . "</td>";
        echo "<td data-device-model='" . $deviceModel . "'>" . $deviceModel . "</td>";
        echo "<td data-accept-date='" . $acceptDate . "'>" . $acceptDate . "</td>";
        echo "<td  onclick='selectUser(this);' data-id='" . $formId . "'><a>Detail</a></td>";
echo "</tr>";

Upvotes: 0

Views: 52

Answers (1)

faizan siddiqui
faizan siddiqui

Reputation: 84

Try this, this is using Jquery. also include the cdn jquery link.

echo "<tr onclick='selectUser(this);' data-id='" . $formId . "'>"; 
    echo "<td class='form_id_class' data-form-id='" . $formId . "'>" . $formId . "</td>";
    echo "<td class='customer_name_class' data-customer-name='" . $customerName . "'>" . $customerName . "</td>";
    echo "<td class='customer_tel_class' data-customer-tel='" . $customerTel . "'>" . $customerTel . "</td>";
    echo "<td class='device_model_class' data-device-model='" . $deviceModel . "'>" . $deviceModel . "</td>";
    echo "<td class='data_acc_date_class' data-accept-date='" . $acceptDate . "'>" . $acceptDate . "</td>";
    echo "<td class='detail_btn'><a>Detail</a></td>";
    echo "</tr>";

$('.detail_btn').onClick(function(){
    var form_id = $(this).closest('tr').find('.form_id_class').val();
    console.log(form_id);
});

Upvotes: 1

Related Questions