Sitemap / Advertise

Information



Tags



Share

How to pass information to the title of a web page in PHP

Advertisement:


read_later

Read Later

Keywords



Keywords



read_later

Read Later

Information

Tags

Share





Advertisement

Advertisement




Definition

In this tutorial, I will show you how to display information in the title of a web page after sending data via HTML form input or button elements. In that regard, depending on the user's action, you can use the title of a web page to notify or inform the user by merely adding information to the title.

The HTML Title element (<title>) defines the document's title that is shown in a browser's title bar or a page's tab. It only contains text; tags within the element are ignored.

The contents of a page title can have significant implications for search engine optimization (SEO). In general, a longer, descriptive title performs better than short or generic titles. The content of the title is one of the components used by search engine algorithms to decide the order in which to list pages in search results. Also, the title is the initial "hook" by which you grab the attention of readers glancing at the search results page(1).

Code

📄 In PHP, define the default status variable as the title.

📄 If the data form-data variable is received and not empty, then change the status variable with it securely by executing the htmlentities function.

📄 In HTML, create a form element to send the data variable via a POST request by using the method attribute.

📄 Finally, print the status variable as the title of the web page.


-------------- PHP --------------

<?php
  
  // Get data to display in the title of the web page.
  $status = "How to pass information to the title of a web page in PHP";
  if(isset($_POST["data"]) && !empty($_POST["data"])){
	  $status = htmlentities($_POST["data"]);
  }
  
?>

-------------- HTML --------------

<form method="POST">
<input type="text" name="data" placeholder="Enter information..."></input>
<button type="submit" style="background-color:#002699;color:white;border:2px solid white;border-radius:5px;font-weight:bold;cursor:pointer;">Submit</button>
</form>

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

Result:

Send information to be diplayed in the title of the page:

References

(1) https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title