Hi guys,

I have a page that echos fields from a database based on various text and combo box selections. I am trying to get it so that when the page is refreshed, the echoed vales are still displayed, even if the form fields are reset.

Im under the impression that i need to use cookies to acheive this however all the code I find via google only lets me display cookie values.

The code i use to display the database values is as follows:

<?php
				mysql_connect ("localhost", "pegasus","123456")  or die (mysql_error());
				mysql_select_db ("partslist");

				if ($term == "") {
				echo "";
				}
				else {
 
				$sql = mysql_query("select * from $table where part like '$term'");
  
				while ($row = mysql_fetch_array($sql)){
			?>
			<div id="display-part"><?php echo $row['part'] .'&nbsp;'.$table; ?></div>
			<div id="display-partno"><?php echo $row['partno']; ?></div>
			<div id="display-alternative"><?php echo $row['alternative']; ?></div>
			<?php
			}
			}
			?>

All help is greatly appreciated.

Dani AI

Generated

Useful options to keep the echoed database results visible after a refresh are to (A) re-run the query on page load using a stored search term, or (B) persist the rendered UI on the client and restore it after reload. pointed to server-side storage and suggested AJAX — both are valid. 's unset($term) would remove the stored value and is the opposite of what you want.

Server-side (session) approach — save the last search term and re-run the query on every load. This keeps results even if the form resets. Example pattern (use at top of your PHP file):

<?php
session_start();

$mysqli = new mysqli('localhost','dbuser','dbpass','partslist');

if (!empty($_POST['term'])) {
  $_SESSION['last_term'] = trim($_POST['term']);
}

$term = $_SESSION['last_term'] ?? '';

if ($term !== '') {
  $stmt = $mysqli->prepare(
    "SELECT part, partno, alternative FROM parts_table WHERE part LIKE CONCAT('%', ?, '%')"
  );
  $stmt->bind_param('s', $term);
  $stmt->execute();
  $res = $stmt->get_result();
  while ($row = $res->fetch_assoc()) {
    echo '<div>'.htmlspecialchars($row['part']).'</div>';
  }
}
?>

Client-side (localStorage + AJAX) — perform the search with AJAX, render results into a results container, and save input.value and results.innerHTML into localStorage. On DOMContentLoaded, restore those values so the UI looks the same after refresh:

<script>
document.addEventListener('DOMContentLoaded', function(){
  var inpt = document.getElementById('term-input');
  var out = document.getElementById('results');
  if (localStorage.lastTerm) inpt.value = localStorage.lastTerm;
  if (localStorage.lastResults) out.innerHTML = localStorage.lastResults;
  // after AJAX success: localStorage.lastTerm = inpt.value; localStorage.lastResults = out.innerHTML;
});
</script>

Notes and troubleshooting: call session_start() before any output; prefer mysqli or PDO with prepared statements (avoid deprecated mysql_*); escape HTML with htmlspecialchars() to prevent XSS; consider using GET (?term=...) if you want bookmarkable URLs; store only non-sensitive UI state in localStorage and provide a clear/reset action for users.

Recommended Answers

All 6 Replies

You could display it via sessions instead of cookies which I find much more convenient. Or you could integrate the whole thing with AJAX if you know your way around JavaScript.

Member Avatar for Member #334542

You can retain the values even after refresh using AJAX

Hi guys thanks for the replys.

gunnarflax: I have spent all morning trying to keep the values with sessions however I have had no luck.

rajarajan07: do you know any example code that could help me with this. I have tried to search google however I cant seem to find anything.

Thanks

just unset the $term at the end of your code like,

unset($term);

If your problem has been solved, please also mark the thread as solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.