How to save a search result?

I am working on my personal website where I want to save the search result of my clients, limited to this particular session .

I use the PHP platform and Javascripts .

Here is an example of what I'm looking at for sure:

It saves your previously found domain name for this particular session so that the user can make a decision by comparing these results.

Thank.

EDIT - Good Thanks for all your answers and suggestions.

But if you notice the example above

It looks like a script , loading new content on the same page without updating and keeping the previous content search <div> as it is.

How to achieve this using javascripts or some kind of div layer ????

+3
source share
3 answers

UPDATE STANDARD

. , , AJAX . PHP. jquery, . : http://docs.jquery.com/Tutorials , , (http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Rate_me:_Using_Ajax).

AJAX ( search.php) HTML , HTML-. , , ( ).

, AJAX . / , JS.

UPDATE END

, . . , , .

PHP , . , . , , cookie .

( , , ), . ,

<?php session_start(); ?>

PHP, , . , - . :)

$_SESSION ( , ). , , , $_SESSION['search'] ..

, :

<html>
...
<form action="search.php" method="post">
Search: <input type="text" name="searchQuery" />
<input type="submit" value="Search" />
</form>
...
</html>

search.php. , - , HTML . , , .

search.php :

<?php
if (!empty($_POST['searchQuery'])) //we have a new search
{
  $result = do_search($_POST['searchQuery']);
}
?>

, , ($result variable). do_search() - , , -. , "" , , .

function do_search($searchQuery)
{
  ...
  return $result;
}

, . . :

<?php
session_start(); //Starting session

//let create session variable used to store results
if (!isset($_SESSION['searches']))
  $_SESSION['searches'] = array();

if (!empty($_POST['searchQuery'])) //we have a new search
{
  if (isset($_SESSION['searches'][$_POST['searchQuery']]) //User already searched on this value, delete previous result from sesion
  {
    unset($_SESSION['searches'][$_POST['searchQuery']]);
  }
  $result = do_search($_POST['searchQuery']);
  //Let add new search on the begining of session array to make iterations easier.
  $result = array($_POST['searchQuery'] => $result); //convert result to same format as session table
  $_SESSION['searches'] = array_merge($result, $_SESSION['searches']);
}
?>

$result, , -

foreach ($_SESSION['searches'] as $query => $result)
{
  ...//display of single result
}

, . , , . , . , :)

- , . .

if (isset($_SESSION['searches'][$_POST['searchQuery']]) //User already searched on this value
{
  $result = $_SESSION['searches'][$_POST['searchQuery']];
  unset($_SESSION['searches'][$_POST['searchQuery']]);
}
else
{
  $result = do_search($_POST['searchQuery']);
}

, , PHP

http://pl.php.net/manual/en/book.session.php

. :)

+2

script (s):

if (!isset($_SESSION['previous_searches']) || !is_array($_SESSION['previous_searches'])) {
  $_SESSION['previous_searches'] = array();
}

[]
, , .
[/]

, , script :

$_SESSION['previous_searches'][] = $_GET['what_ever_your_search_value_might_be'];

[]

[/]

$_SESSION['previous_searches']

+1

- - -, , ( - var StoredSearch = []; ). , $_SESSION AJAX / JavaScript PHP.

0

Source: https://habr.com/ru/post/1750642/


All Articles