0

I have this PHP code and I am trying to alert a table that receives its values via sql. Here's my code:

    <script src="jquery.min.js">
    </script>
    <script>
    function bus(x){
        alert("Row index is: " + x.value);
    }
    </script> 
    $sql="SELECT  ID, NOME, CPF FROM cadastro WHERE NOME LIKE '%" . $name .  "%'"; 
    $result=mysqli_query($conn,$sql);
    while($row=mysqli_fetch_array($result)){
                $ID =$row['ID'];
                $NOME=$row['NOME'];
                $CPF=$row['CPF'];
    echo '<table style="width:100%">';
    echo "<tr>";
    echo "<th> ID </th>";
    echo "<th> NOME </th>";
    echo "<th> CPF </th>";
    echo "</tr>";
    echo '<tr style="cursor: pointer;">';
    echo "<td onclick='bus(" . $ID . ")' class='tab'>" . $ID . "</td>";
    echo "<td onclick='bus(" . $ID . ")' class='tab'>" . $NOME . "</td>";
    echo "<td onclick='bus(" . $ID . )' class='tab'>" . $CPF . "</td>";
    echo "</tr>";
    echo "</table>";

I need to get the value from the table and serve it to a javascript function.

7
  • 1
    alert("Row index is: " + x); no need of .value Commented Nov 23, 2018 at 15:27
  • Could you elleborate more on your question? what are you getting from this code, an error or what? Commented Nov 23, 2018 at 15:28
  • The alert shows that the value is undefined Commented Nov 23, 2018 at 15:29
  • I need the value of the first collumn Commented Nov 23, 2018 at 15:30
  • undefined because a <td> does not have a value attribute Commented Nov 23, 2018 at 15:31

3 Answers 3

1

If x is a Number, then x.value will give you undefined and you should just use:

alert("Row index is: " + x);

To get the value of the column, you could pass this to the function instead of the id and then get the innerHTML:

<td onclick="bus(this)" class="tab">column 1</td>

function bus(elm){
    alert(elm.innerHTML);
}

function bus(elm) {
  alert(elm.innerHTML);
}
<table>
  <tr>
    <td onclick="bus(this)" class="tab">column 1</td>
    <td onclick="bus(this)" class="tab">column 2</td>
    <td onclick="bus(this)" class="tab">column 3</td>
  </tr>
</table>

Sign up to request clarification or add additional context in comments.

Comments

0

You only have one error. onclick is passing the ID, so the following code will do exactly what you need:

<script>
function bus(x){
    alert("Row index is: " + x);
}
</script>  

I removed the .value from your javascript.

function bus(x){
alert("Value is " + x);
}
<table>
<tr>
<td onclick="bus(44)">44</td>
<td onclick="bus(44)">Lorem</td>
<td onclick="bus(44)">ipsum</td>
</tr>
</table>

1 Comment

I need the ID in alert, but i don't receive him. The alert shows "undefined"
0

In this case x is not an object, just a plain value so:

alert("Row index is: " + x);

gl, pb

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.