Click here to Skip to main content
14,826,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Call the JS function in php before the database data load on the html.


What I have tried:

<pre><!DOCTYPE html>
<html lang="en">

<script  src="https://code.jquery.com/jquery-3.5.1.js"></script> 
 <script  src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js"></script> 
 <script  src="javascript.js"></script>

<head>
<meta charset="utf-8" />
<title>PHP & SQL Server (sqlsrv)</title>
//
// These are required files we must include these libraries 
//



<link rel='stylesheet' href="https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css">
 <link rel="stylesheet" href="foot.css">




</head>  
<body style="margin:20px auto">  
<div class="container">
<div class="row header" style="text-align:center;color:green">
<h3>Bootstrap Table With sorting,searching and paging using dataTable.js (Responsive)</h3>
</div>



<style>
<body {background-color: black;}
input{ 
     width:30%;
     height: 10%;
     border-radius:05px; 
     margin: 10px 0px 15px 0px;
     box-shadow: 1px 1px 2px 1px grey;
}
</style>
</head>
<body>


<h1> Consumables Table </h1>
<div>
<form action='./test.php' method='POST'>  
Search:  <input type="text" name="id" size ='40' placeholder="Value To Search" value='<?php echo $_GET['id']; ?>' /> 
<input type="Submit"  name="search" value="Search" />  <br />



<table id="example" class="display" style="width:100%">  
   <thead>
        <tr>
        <th>0</th>
            <th>1</th>
            <th>3</th>
            <th>4</th>
            <th>5</th>
            <th>6</th>
            <th>7</th>
          </tr>
    </thead>
    <tbody>



</form>
</div>  


<?php
ini_set('display_errors', 1);
error_reporting(-1);
 
$serverName = "APPSERVER"; 
$uid = "sa";  
$pwd = "xxxxxx";  
$databaseName = "Ramco";

$connect = array( "UID"=>$uid, "PWD"=>$pwd, "Database"=>$databaseName);

/* Connect using SQL Server Authentication. */  
$conn = sqlsrv_connect( $serverName, $connect);  
 
if( $conn === false )
 {die( print_r( sqlsrv_errors(), true)); 
}

if(isset($_POST['id']))
{
     $id = $_POST['id'];



$sql = "SELECT * FROM consumables where Project = '$id' ";
 
$params = array();
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$query = sqlsrv_query( $conn, $sql , $params, $options );


$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );

}
?>
     
<?php while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) ) { ?>

<form>


               <tr>                    
               <td><div align="center">  <?php echo $row[0]; ?> </div> </td>
               <td><div align="center">   <?php echo $row[1]; ?> </div></td>
               <td><div align="center">   <?php echo $row[3]; ?> </div></td>
               <td><div align="center">   <?php echo $row[4]; ?> </div></td>
               <td><div align="center">   <?php echo $row[5]; ?> </div></td>
               <td><div align="center">   <?php echo $row[6]; ?> </div></td>
               <td><div align="center">   <?php echo $row[7]; ?> </div></td>
  
               </tr>

                    
<?php } ?>


<?php } ?>


 
 
  </tbody>
<tfoot>
        <tr>
            <th>0</th>
            <th>1</th>
            <th>3</th>
            <th>4</th>
            <th>5</th>
            <th>6</th>
            <th>7</th>
              </tr>
</tfoot>

     </table> 

 
 
  </form>
</body>

</html>
Posted
Updated 25-Feb-21 4:38am

1 solution

If you want to load the page before the data is loaded (from the server side) then you need to use AJAX.

AJAX is typically a javaScript function so you have your wish.

See here, if this is new to you.[^]

   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900