php - How to add preformatted text on text fields by clicking a button from another page


newbie here. Using php. So I have a button. When clicked will go to a contact page that I have already done. But I need to automatically put text into a text field for the message. All one has to do is click send as it is already formatted for them. How do I make the button automatically add text into the textfield(s)? I now realize it probably needs an onlick event or anything similar on the button that will post values on some text fields on the contact page being called. I hope I have given a better description of my problem. thanks guys.

Say this is my button:

<input type="submit" name="button" id="button" value="Submit" />

on submit will go to contact page and fill in the values of some textfields I need preformatted.

here's my sample mailer script.

if(isset($_POST['_save'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $company = $_POST['company'];
    $message = $_POST['message'];
    $subject = $_POST['subject'];

    if (empty($name) || empty($email) || empty($subject) || empty($message)) {
        if (empty($name))
            $error['name'] = "Please enter your Full Name";
        if (empty($email))
            $error['email'] = "Please enter a valid Email Address";
        if (empty($company))
            $error['company'] = "Please enter Your Company Name";
        if (empty($subject))
            $error['subject'] = "Please Write a Subject";

    else { //if not empty

        $headers="From: {$email}\r\nReply-To: {$email}"; //create headers for email
        $content="Name: ".$name."\r\n\r\nCompany: ".$company."\r\n\r\nSubject: ".$subject."\r\n\r\nMessage: ".$message;
        mail('[email protected]',$subject,$content,$headers); //mail the message;
        $success = "Thank you! You're email has been sent.";



I am not sure but if i understand correctly your requirement. then probably you want this.

  1. You are at x page and page x has page has a button.
  2. When you click a button you go to contact page.
  3. You may passing some data using get or post method, you want to display that message on textarea of contact page.

If i am right and above scenario is correct then you can do like this:

<textarea name="message"><?php echo $_request['message']; ?></textarea>

$_request can get both post or get data.





on your previous page (eg. the input form for mail) use this;

<textarea name="message"> <?php echo 'the text message'; ?> </textarea>

or simply,

<textarea name="message"> the text message </textarea>



As I read your question, I assume you have this flow in mind:

Probably a button,

<button type="submit" name="go_to_next"></button>


<input type="submit" name="go_to_next" value="Submit" />

that will redirect to contacts.php, where<input> fields are present

<input type="text" name="name" />
<input type="text" name="email" />
<input type="text" name="company" />
<input type="text" name="subject" />
<textarea cols="30" rows="10" name="message></textarea>

Now (just an assumption) that you want to have this fields default values when loaded, if it is, then all you have to do is set<input value="" />

<input type="text" name="name" value="Pepe Smith" />
<input type="text" name="email" value="[email protected]" />
<input type="text" name="company" value="My Company" />
<input type="text" name="subject" value="My Subject" />
<textarea cols="30" rows="10" name="message>My Message</textarea>

You can set dynamic values for this, but I'm not sure where you want those values to come from.


I created a demo of populating the fields by an onClick event in button.

People are also looking for solutions to the problem: php - how to delete mysql table row from mail inbox


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.