Form Submit without Page Refresh

Hello,

I am currently using the following code:

<?php
include('includes/config.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
From this page, you can search for server names.  You do not have to use a Wildcard.  I've done that for you. :)
<br /><br />
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">  
Server Search: <input type="text" name="term" /><br />  
<input type="submit" name="submit" value="Submit" />  
</form>  

<?php
if (isset($_POST['submit'])) {

$term = $_POST['term'];	

$sql = "SELECT * FROM tblDeviceListing WHERE SystemName LIKE '%".$term."%'";
$r_query = sqlsrv_query($conn, $sql);

/*
while ($row = mssql_fetch_array($r_query)) {
echo 'System Name: ' .$row['SystemName'];
}
*/

while( $row = sqlsrv_fetch_array( $r_query, SQLSRV_FETCH_ASSOC) ) {
    echo $row['SystemName']. " - ".$row['Location']." - ".$row['Services']. " - " .$row['PrimaryIPAddress']. " - " .     $row['iLOAddress']. " - " .$row['RackNumber']."<br />";
}	
}
?>
</body>
</html>

however, I want, when they click Search to just display the data in the same div it’s already in, and not refresh the whole page.

Thanks in advance.

Can you use an iframe for the data? Should be safe if its from the same location as the page it’s put in.

Perhaps there are some AJAX solutions I’m not aware of.

Change the submit button to an input type=“button” and give it an onclick event that will call a function.

Said function should create an XMLHttpRequest(), serialize the form data into an array, and use the XHR to post the data to a processing PHP page. Don’t forget to use server-side form validation, and then sanitize the input against SQL injection. You can use the server responseText to display form validation errors or other messages on the page that contains the form by creating a DIV or other element for displaying the server responses.

HTH,

:slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.