Xavi Alsina
Xavi Alsina

Reputation: 653

Personalized thanks message on contact form

I have got the following code to make a contact form works:

<?php
session_name("fancyform");
session_start();


$_SESSION['n1'] = rand(1,20);
$_SESSION['n2'] = rand(1,20);
$_SESSION['expect'] = $_SESSION['n1']+$_SESSION['n2'];


if(isset($_SESSION['sent']))
{
    $success='<p>GThanks for contacting us! We'll reply you asap!</p>';
    $css='<style type="text/css">.demo-form{display:none;}.thanks{display:block;}</style>';

unset($_SESSION['sent']);
}
?>

The problem there is that I would want to add a personalized message.

Example: My name is Xavi, so if I type on the name field: Xavi, I will get a reply like this one: Thanks for contacting us Xavi! We'll reply you asap!

I dont know how to solve it, since I tried the following values but without any result:

&name, 'name', ['name'], '['name']'

Contact form label:

<label for="name">Name:</label>
<input type="text" placeholder="Nombre" id="name" name="name" data-parsley-trigger="change" required tabindex="1" autocomplete="off" value="<?php echo (isset($_SESSION['post']['name']) ? $_SESSION['post']['name'] : ''); ?>" >

Upvotes: 3

Views: 94

Answers (2)

MrCode
MrCode

Reputation: 64526

In the same place where you define $_SESSION['sent'] = true, add another session variable to store the name:

$_SESSION['name'] = $_POST['name'];

Then, use it in your message:

$success="<p>GThanks for contacting us " . htmlspecialchars($_SESSION['name']) . "! We'll reply you asap!</p>";

Side note: you have an issue with the quotes because you wrap the string in single quotes, but use another single quote in the word We'll.

Upvotes: 3

Neekobus
Neekobus

Reputation: 2038

You can access to the form data by using the $_REQUEST array :

echo $_REQUEST['name'];

Upvotes: 1

Related Questions