Hi

I am looking to create a personalised web page, based on the web address I am going to give somebody. So say I have the web address beanburger.co.uk and was going to send you an email and your name was barbara I would give you the url beanburger.co.uk?name=Barbara (or something like that). Could someone help me with the code that would bring the name from the url and put it into the pages so that when you went to it the page might say "Hey Barbara".

Thanks

Dani AI

Generated

A concise, practical addendum to ’s question and the replies from and : both JS approaches shown will work, but two important points were not covered clearly—output must be escaped to avoid XSS, and server-side handling is safer when the value will be used for anything beyond simple display (logs, DB, filenames, etc.). Below are safe, modern options and quick troubleshooting notes.

Server-side (recommended for safety)

<?php
// index.php
$name = isset($_GET['name']) ? trim($_GET['name']) : '';
if ($name !== '') {
  // normalize and strip tags
  $name = mb_convert_encoding($name, 'UTF-8', 'UTF-8');
  $name = strip_tags($name);
  // basic title-case (requires mbstring)
  $name = mb_convert_case(mb_strtolower($name, 'UTF-8'), MB_CASE_TITLE, 'UTF-8');
  echo 'Hey ' . htmlspecialchars($name, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
} else {
  echo 'Hey there!';
}

Client-side (modern, safe DOM use)

<script>
const params = new URLSearchParams(window.location.search);
let name = (params.get('name') || '').trim();
if (name) {
  name = name.split(/\s+/).map(s => s[0].toUpperCase() + s.slice(1).toLowerCase()).join(' ');
  document.getElementById('welcome').textContent = 'Hey ' + name;
} else {
  document.getElementById('welcome').textContent = 'Hey there!';
}
</script>

Nice-to-have: friendly path URLs via a rewrite rule (example .htaccess)

RewriteEngine On
RewriteRule ^([^/]+)/?$ /index.php?name=$1 [L,QSA]

Troubleshooting & cautions

  • Always escape output: use htmlspecialchars server-side or textContent client-side; never inject raw HTML.
  • PHP auto-decodes query values; JavaScript parsing may need decodeURIComponent or URLSearchParams.
  • For multi-word names use %20 (or encodeURIComponent) in links.
  • If multibyte names are expected, enable PHP mbstring and prefer mb_ functions.

These steps tie back to the JS suggestions by and the native-DOM note by while adding safe, production-ready handling and a server-side option for stronger guarantees.

Recommended Answers

All 2 Replies

function getParam(paramName) {
    var url = window.location;
    var params = url.substring(url.indexOf("?") + 1 , url.length).split("&");
    for(var i = 0; i < params.length; i++) {
        var param = params[i].split("=");
        if(param[0] == paramName) return param[1];
    }

    return null;
}

var userName = getParam("name");

$("#welcome").html("Hey " + userName);

Hope this will work :)

Thanks!

Member Avatar for Member #905211

Please note that Luckychaps answer requires jQuery.

line 14 can be replaced with:

document.getElementById('welcome').innerHTML = userName;

wlecome is the id of a div or other element on the page.

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.