?
Solved

Search results problem

Posted on 2010-01-05
15
Medium Priority
?
285 Views
Last Modified: 2013-12-13
Dear Experts,

I have a search page on my website, which provides the user with a textbox and a button at the top of the page.  The user can type in a phrase and then click the button which goes and gets relievant results.

The results are clickable hyperlinks which when clicked, add themselves to an array on the server for later processing.  For example, if I click on the first 3 links, those 3 links go into an array on the server.

Anyway, the problem is, each time I click on the links, the searched results disappear and the textbox where I typed the search phrase is cleared.  How do I retain the text in the textbox and how do I keep the search results displayed on screen as I click on the database generated hyperlinks?

No AJAX please.
0
Comment
Question by:narmi2
15 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26180319
Interesting.  Please post the code that causes this issue.  Please use the "code" snippet for that.  Thanks, ~Ray
0
 

Expert Comment

by:tamilperiyasamy
ID: 26180329
Just assign the following to the value of your search text box.
If you are using POST method use->
<input type="text" value="<?=$_POST['name_of_search_text_box']?>" />

If you are using GET method use->
<input type="text" value="<?=$_GET['name_of_search_text_box']?>" />
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26180360
Before you do this:

<input type="text" value="<?=$_POST['name_of_search_text_box']?>" />

You might want to see if the values in $_POST are set at all, or you may get a notice thrown into the middle of the input statement :-(
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:VanHackman
ID: 26181867
"No AJAX please."

Why?

I think that using some AJAX, may be some Jquery, it could be get it easily.
0
 
LVL 1

Author Comment

by:narmi2
ID: 26185432
@ tamilperiyasamy:

I don't understand how I can use your method when I have a form which uses the POST method, but the hyperlinks use GET.

@ VanHackman:

I cannot use AJAX because I need to make 2 different versions, one without javascript for devices which do not support javascript and a javascript version for devices which do support javascript.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26185447
Please post the code that causes this issue.  Please use the "code" snippet for that.  Thank you, ~Ray
0
 
LVL 1

Author Comment

by:narmi2
ID: 26185763
@ Ray_Paseur:
0
 
LVL 1

Author Comment

by:narmi2
ID: 26185778
Code:
<?php include 'include/config.php'; ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title></title>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
	</head>
	
	<body>
		<form id="form1" method="post" action="search.php">	
		
			<input class="txtSearch" type="text" name="search_val" />
			<input type="submit" value="search"/><br /><br />
			
			<?php
				$mystring = "";
				$search_val = "";

				$search_val = $_POST["search_val"];
	
				$query = "SELECT * FROM `table_name` WHERE `col1` LIKE '%$search_val%'";
				$result = mysql_query($query, $connection) or die (mysql_error());
	
				while ($row = mysql_fetch_array($result))
				{
					extract($row);
		
					$mystring .= "<a href=\"?col3=$col3&col2=$col2&col1=$col1&col0=$col0\">$col1 - $col0</a><br />";
				}

				echo $mystring;
				mysql_close($connection);
			?>

		</form>
	</body>
</html>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26185913
It looks like you need a complete application redesign.  This will get you the part about keeping the search string in the input box, but consider what you're asking for...

"how do I keep the search results displayed on screen as I click on the database generated hyperlinks?"

Every page that is linked would have to have a page header that carried this search form.  You would need to bring the search string along - probably in your session array.

The other way you might try this is through the use of IFrame.  The search box could be at the top and the main page links could all point to the iframe below.

HTH, ~Ray
<?php include 'include/config.php';
error_reporting(E_ALL ^ E_NOTICE);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
 
<body>
<form id="form1" method="post" action="search.php"> 
 
<input class="txtSearch" type="text" name="search_val" value="$_POST['search_val']" />
<input type="submit" value="search"/><br /><br />
 
<?php
$mystring   = "";
$search_val = $_POST["search_val"];
 
$query  = "SELECT * FROM `table_name` WHERE `col1` LIKE '%$search_val%'";
$result = mysql_query($query, $connection) or die (mysql_error());
 
while ($row = mysql_fetch_assoc($result))
{
     extract($row);
     $mystring .= "<a href=\"?col3=$col3&col2=$col2&col1=$col1&col0=$col0\">$col1 - $col0</a><br />";
}

echo $mystring;
?>
</form>
 /body>
</html>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26185918
Sorry - fix line 15...
<input class="txtSearch" type="text" name="search_val" value="<?php echo $_POST['search_val']; ?>" />

Open in new window

0
 
LVL 1

Author Comment

by:narmi2
ID: 26185975
When the links are clicked, is it not possible to automatically do the search again based on what was typed in the textbox?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26186150
Maybe - we could use the session handler to keep the information so that when you come back from the other pages we could restore the contents of the search box.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 26186208
Try this and see if it is well-behaved.
<?php 
// START THE SESSION 
session_start();

include 'include/config.php';

// SUPPRESS NOTICES
error_reporting(E_ALL ^ E_NOTICE);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<form id="form1" method="post" action="search.php"> 
<input class="txtSearch" type="text" name="search_val" value="<?php echo $_SESSION['search_val']; ?>" />
<input type="submit" value="search"/><br /><br />
 
<?php
$mystring   = "";

// ESCAPE THE INPUT VALUE FOR USE IN THE QUERY
$search_val = mysql_real_escape_string($_POST["search_val"]);

// SAVE THE INPUT VALUE IN THE SESSION FOR LATER REUSE
$_SESSION["search_val"] = $search_val;

// RUN THE QUERY
$query  = "SELECT * FROM `table_name` WHERE `col1` LIKE '%$search_val%'";
$result = mysql_query($query, $connection) or die (mysql_error());

// ITERATE OVER THE RESULTS SET
while ($row = mysql_fetch_assoc($result))
{
     extract($row);
     $mystring .= "<a href=\"?col3=$col3&col2=$col2&col1=$col1&col0=$col0\">$col1 - $col0</a><br />";

}

// SHOW THE LINKS
echo $mystring;

// END OF PHP - CLOSE OUT THE PAGE
?>
</form>
 /body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:narmi2
ID: 26384910
Is it possible to convert the links into submit buttons?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26486447
Yes, it is possible to convert the links into submit buttons.  Here is an example of how you might do that.  Untested but probably valid in principle.  Best regards, ~Ray
// LINK
<a href="http://example.com/search.php?q=hello+kitty">Search for Hello Kitty</a>

// SUBMIT BUTTON
<form method="get" action="http://example.com/search.php">
<input type="hidden" name="q" value="hello kitty" />
<input type="submit"          value="Search for Hello Kitty" />
</form>

Open in new window

0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question