Jump to content

How do you get search box's input terms into meta tags?


man5
Go to solution Solved by man5,

Recommended Posts

Right now I am using a session to get search's input terms and place them into meta tags(title). It works; the only issue is that the search terms lag 1 page behind.

 

For eg.

 

If i search "flowers" on first try, it will show flowers in the metas(title). Next I input "treehouse" in search; instead of "treehouse" showing in the metas, it'll still show me the previous term "flowers".  Now if I do a third search with a new term, it won't show the new term in metas, it'll show me the previous term "treehouse".

 

Why is this happening? Am I doing something wrong?

 

This is only an issue with meta tags. Search itself works fine. It shows results on a page as it's suppose to.

Link to comment
Share on other sites

HI.

 

when do you set you session variable ?

 

Can you post your code here ?

 

 

Well my session_start(); is at the very begining of the page. My site setup is done in layers with header,head,footer,foot divded up; but here is a basic layout with my search session.

<?php init.php ?> //this includes the sesion_start();
<html>
<head>
	$searchsession	=	$_SESSION['searchsession'];
	
	<title><?php echo $searchsession; ?></title>
	<meta name="description" content="Search page description here." />
</head>
<body>

	<?php $value =  $_GET['search']; 
	
	  $_SESSION['searchsession']  =	$value;
	
	?>
	
	<form action="search.php?q=<?php echo $value; ?>" method="GET">
		<input type="search" name="search" maxlength="60" placeholder="Search...">
		<input type="submit" name="submit" value="Search" >
	</form>

</body>
</html>
Link to comment
Share on other sites

It's simply down to the order you're processing the page -

 

Step 1 -

$searchsession	=	$_SESSION['searchsession'];

Let's say $_SESSION['searchsession'] = '1'; This line is setting $searchsession = '1';

 

Step 2 -

<title><?php echo $searchsession; ?></title>

You echo $searchsession, so the title will be '1'.

 

Step 3 -

$value =  $_GET['search']; 

You assign the variable $value the same value as $_GET['search'].. let's say that value is '2'.

 

Step 4

$_SESSION['searchsession']  =	$value;

You change the existing $_SESSION['searchsession'] variable to '2'.

 

Step 5 -

<form action="search.php?q=<?php echo $value; ?>" method="GET">

You echo the $value variable in your form, this is '2' as it's come from the $_GET['search'] variable.

 

Look at the order things are happening above. What you're looking for is step 1 - 5 the variable to always be 2.

 

By changing the order of the 5 steps above you'll get what you want.

Edited by adam_bray
Link to comment
Share on other sites

  • Solution

It's simply down to the order you're processing the page -

 

Step 1 -

$searchsession	=	$_SESSION['searchsession'];

Let's say $_SESSION['searchsession'] = '1'; This line is setting $searchsession = '1';

 

Step 2 -

<title><?php echo $searchsession; ?></title>

You echo $searchsession, so the title will be '1'.

 

Step 3 -

$value =  $_GET['search']; 

You assign the variable $value the same value as $_GET['search'].. let's say that value is '2'.

 

Step 4

$_SESSION['searchsession']  =	$value;

You change the existing $_SESSION['searchsession'] variable to '2'.

 

Step 5 -

<form action="search.php?q=<?php echo $value; ?>" method="GET">

You echo the $value variable in your form, this is '2' as it's come from the $_GET['search'] variable.

 

Look at the order things are happening above. What you're looking for is step 1 - 5 the variable to always be 2.

 

By changing the order of the 5 steps above you'll get what you want.

 

 

Thanks for pointing me the right direction.  I was getting confused sessions and variables because I had divided up the pages/sections; but by playing around with it more, I finally came to a solution. It works now. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.