0

Assuming in PHP file there're :

    function printAllRows(){
      for( everyrows in db){
         echo "<tr>";
         echo "<td> $_POST['..'] <td>";
         echo "<td> $_POST['..'] <td>";
         echo "<tr>"; 
      }

     }


function printSpecifRows($keyword){
  .....
 }

// When the page loads for the first time

 if ($db->preRow() >0){ // count rows in DB , if there already show them as table 
printAllRows();
}

At the same time , there is

<input type="text" name="keywoard"  />
<input type="submit" name="find" value="search" />

If the user enter the keyword press the button , then just show the rows match the keyword!

so :

if( $_POST["find"]){
 ?>

 <script>
 // first clear the current DOM table
  document.getElementById("myTable").innerHTML="";
 </script>

 <?php


 // then print again! according to printSpecifRows($keyword) 
 function printSpecifRows($keyword){
  .....
 }}

But the problem here is that JS is rendered first before PHP , so printSpecifRows($keyword) won't never be reached , any idea. I really appreciate your help. Thanks.

Dipen Shah
  • 1,911
  • 10
  • 29
  • 45

1 Answers1

3

You are massively over-complicating things. Get rid of the <script> entirely. There is no need or point in involving JS here.

Just change the PHP so it doesn't output all the data in the first place if you don't want it on the page.

if ($_POST["find"]){
    printSpecifRows($_POST["find"]);
} else {
    printAllRows();
}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 2
    it's good practice to use `if (isset($_POST["find"])){...` to avoid undefined index error, (or use `filter_input`) – andrew Jun 09 '15 at 16:33
  • oh my bloody sexy gf, My thought is so deep that i messed everything , thank you so much Quentin. And about XSS why so?? in simple explanation , i'm rookie :) thanks again – Plain_Dude_Sleeping_Alone Jun 09 '15 at 16:38
  • 1
    @febri23 About XSS: http://stackoverflow.com/questions/15755323/what-is-cross-site-scripting There are clear examples. –  Jun 09 '15 at 16:43