Need help writing a code

ShortCircuit

Newbie
Joined
Jul 6, 2014
Messages
43
Reaction score
0
Hello,
Si I need to make a webpage with php, the idea is simple...I want from visitors to write thier name in a text box and when clicking on a button they get forwarded to link.com/[name].
I hope you get the idea, can't wait for your replies.
 
Just redirection? No saving? Nothing at all?

What's the point?
Yes no saving, just redirection.
My point is I want to ask for thier name and then show them a happy BD message or something
 
Ahh! That's simple - you can do that using URL parameters.

Here's your index file. It shows a single form field asking for the user's name with a Submit Button. It then submits a Get request to the file birthday.php via the parameter fname
HTML:
<html>
<head>
<title>Birthday Form</title>
</head>

<body>

<form action="birthday.php" method="get">

<input type="text" name="fname" placeholder="Please Enter Your Name here" />
<input type="submit" value=" Submit " />

</form>

</body>

</html>


Here's your birthday.php

PHP:
<html>
<head><title>Happy Birthday</title></head>

<body>

<?php

// Show the URL parameters name

$name = $_GET['fname'];

?>

<h1>Hey <?php echo $name; ?>, <br></h1>

Wish you a very happy Birthday

</body>

</html>

1. User lands on site.com/birthdaywishes.html - enters name and is redirected to site.com/birthday.php?fname=BlogPro

2. site.com/birthday.php shows the message "Hey BlogPro, Wish you a very happy Birthday".
 
Remember XSS ;-) maybe a bit too much at this stage I know ha
 
Yeah, you have to escape your user input or you're doomed.

birthday.php
Code:
<html>
<head><title>Happy Birthday</title></head>

<body>

<?php

// Show the URL parameters name

$name = htmlspecialchars($_GET['fname'], ENT_QUOTES | ENT_HTML401, 'UTF-8');

?>

<h1>Hey <?php echo $name; ?>, <br></h1>

Wish you a very happy Birthday

</body>

</html>
 
And you'd need some fancy url to get it at link.com/[whatername] after the redirection.
 
Simplified a bit.

PHP:
<html>
<head><title>Happy Birthday</title></head>

<body>

<h1>Hey <?= htmlentities($_REQUEST['name']) ?>, <br></h1>

Wish you a very happy Birthday

</body>

</html>

This whole thing could be done single page, with no server side at all..

HTML:
<html>
<head>
    <script>
        function bday(){
            document.getElementById('output').innerText = 'Happy Birthday ' + document.getElementById('name').value;

            document.getElementById('output').style.display = 'block';
            document.getElementById('form').style.display = 'none';
        }
    </script>
</head>

<body>
    <div id="form">
        <form>
            Name: <input type="text" id="name" required />
            <button type="button" onclick="name()">Submit</button>
        </form>
    </div>
    <div id="output" style="display:none;">

    </div>
</body>
</html>

as an aside: innerText and textContent are XSS safe - using innerHTML here would be a poor decision.
 
Last edited:
Back
Top