PHP table pass to a var in javascript





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







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.










share|improve this question




















  • 1





    alert("Row index is: " + x); no need of .value

    – suresh bambhaniya
    Nov 23 '18 at 15:27













  • Could you elleborate more on your question? what are you getting from this code, an error or what?

    – Anna Jeanine
    Nov 23 '18 at 15:28











  • The alert shows that the value is undefined

    – Matheus Santos
    Nov 23 '18 at 15:29











  • I need the value of the first collumn

    – Matheus Santos
    Nov 23 '18 at 15:30











  • undefined because a <td> does not have a value attribute

    – RiggsFolly
    Nov 23 '18 at 15:31




















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.










share|improve this question




















  • 1





    alert("Row index is: " + x); no need of .value

    – suresh bambhaniya
    Nov 23 '18 at 15:27













  • Could you elleborate more on your question? what are you getting from this code, an error or what?

    – Anna Jeanine
    Nov 23 '18 at 15:28











  • The alert shows that the value is undefined

    – Matheus Santos
    Nov 23 '18 at 15:29











  • I need the value of the first collumn

    – Matheus Santos
    Nov 23 '18 at 15:30











  • undefined because a <td> does not have a value attribute

    – RiggsFolly
    Nov 23 '18 at 15:31
















0












0








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.










share|improve this question
















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.







javascript php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 17:44









iLuvLogix

1,673726




1,673726










asked Nov 23 '18 at 15:26









Matheus SantosMatheus Santos

62




62








  • 1





    alert("Row index is: " + x); no need of .value

    – suresh bambhaniya
    Nov 23 '18 at 15:27













  • Could you elleborate more on your question? what are you getting from this code, an error or what?

    – Anna Jeanine
    Nov 23 '18 at 15:28











  • The alert shows that the value is undefined

    – Matheus Santos
    Nov 23 '18 at 15:29











  • I need the value of the first collumn

    – Matheus Santos
    Nov 23 '18 at 15:30











  • undefined because a <td> does not have a value attribute

    – RiggsFolly
    Nov 23 '18 at 15:31
















  • 1





    alert("Row index is: " + x); no need of .value

    – suresh bambhaniya
    Nov 23 '18 at 15:27













  • Could you elleborate more on your question? what are you getting from this code, an error or what?

    – Anna Jeanine
    Nov 23 '18 at 15:28











  • The alert shows that the value is undefined

    – Matheus Santos
    Nov 23 '18 at 15:29











  • I need the value of the first collumn

    – Matheus Santos
    Nov 23 '18 at 15:30











  • undefined because a <td> does not have a value attribute

    – RiggsFolly
    Nov 23 '18 at 15:31










1




1





alert("Row index is: " + x); no need of .value

– suresh bambhaniya
Nov 23 '18 at 15:27







alert("Row index is: " + x); no need of .value

– suresh bambhaniya
Nov 23 '18 at 15:27















Could you elleborate more on your question? what are you getting from this code, an error or what?

– Anna Jeanine
Nov 23 '18 at 15:28





Could you elleborate more on your question? what are you getting from this code, an error or what?

– Anna Jeanine
Nov 23 '18 at 15:28













The alert shows that the value is undefined

– Matheus Santos
Nov 23 '18 at 15:29





The alert shows that the value is undefined

– Matheus Santos
Nov 23 '18 at 15:29













I need the value of the first collumn

– Matheus Santos
Nov 23 '18 at 15:30





I need the value of the first collumn

– Matheus Santos
Nov 23 '18 at 15:30













undefined because a <td> does not have a value attribute

– RiggsFolly
Nov 23 '18 at 15:31







undefined because a <td> does not have a value attribute

– RiggsFolly
Nov 23 '18 at 15:31














3 Answers
3






active

oldest

votes


















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>








share|improve this answer


























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

    – Matheus Santos
    Nov 23 '18 at 15:36











  • see my edit. I added a runnable code.

    – Unamata Sanatarai
    Nov 23 '18 at 15:39



















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>








share|improve this answer































    0














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



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


    gl, pb






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449309%2fphp-table-pass-to-a-var-in-javascript%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      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>








      share|improve this answer


























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

        – Matheus Santos
        Nov 23 '18 at 15:36











      • see my edit. I added a runnable code.

        – Unamata Sanatarai
        Nov 23 '18 at 15:39
















      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>








      share|improve this answer


























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

        – Matheus Santos
        Nov 23 '18 at 15:36











      • see my edit. I added a runnable code.

        – Unamata Sanatarai
        Nov 23 '18 at 15:39














      0












      0








      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>








      share|improve this answer















      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>








      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>





      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>






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 23 '18 at 15:37

























      answered Nov 23 '18 at 15:29









      Unamata SanataraiUnamata Sanatarai

      4,52431943




      4,52431943













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

        – Matheus Santos
        Nov 23 '18 at 15:36











      • see my edit. I added a runnable code.

        – Unamata Sanatarai
        Nov 23 '18 at 15:39



















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

        – Matheus Santos
        Nov 23 '18 at 15:36











      • see my edit. I added a runnable code.

        – Unamata Sanatarai
        Nov 23 '18 at 15:39

















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

      – Matheus Santos
      Nov 23 '18 at 15:36





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

      – Matheus Santos
      Nov 23 '18 at 15:36













      see my edit. I added a runnable code.

      – Unamata Sanatarai
      Nov 23 '18 at 15:39





      see my edit. I added a runnable code.

      – Unamata Sanatarai
      Nov 23 '18 at 15:39













      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>








      share|improve this answer




























        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>








        share|improve this answer


























          1












          1








          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>








          share|improve this answer













          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>








          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>





          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>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 15:38









          The fourth birdThe fourth bird

          25.5k81630




          25.5k81630























              0














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



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


              gl, pb






              share|improve this answer




























                0














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



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


                gl, pb






                share|improve this answer


























                  0












                  0








                  0







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



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


                  gl, pb






                  share|improve this answer













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



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


                  gl, pb







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 15:35









                  PauloBuenoPauloBueno

                  12




                  12






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449309%2fphp-table-pass-to-a-var-in-javascript%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()