andresr
andresr

Reputation: 93

How to get the page title into a input's value?

I want my hidden input field to get the page title as its value.

This is my testmail.php code.

    <p>From: <?php echo $_POST['name']; ?></p>
<p>Subject: <?php echo $_POST['subject']; ?></p>
<p>Email: <?php echo $_POST['email']; ?></p>
<p>Phone Number: <?php echo $_POST['phone']; ?></p>
<p style="width:300px;">Message: <?php echo $_POST['message']; ?></p>

and this is my form code

                <form action="testmail.php" method="post" class="cf">

                    <label for="name">* Name:</label>
                        <input type="text" name="name" placeholder="Your Name">

                        <input type="hidden" name="subject" value="<?php value $_POST['pagetitle']; ?>">

                    <label for="email">* Email:</label>
                        <input type="text" name="email" placeholder="Enter a valid Email Address">

                    <label for="phone"> Phone:</label>
                        <input type="text" name="phone" placeholder="Enter areacode and number">

                    <label for="message">* Message:</label>
                        <textarea name="message"></textarea>

                        <button type="input">Send</button>
            </form>

Is there a way to get the title automatically?

Upvotes: 1

Views: 3101

Answers (3)

rfoo
rfoo

Reputation: 1198

Here's a pure javascript way using the onsubmit event.

<form onsubmit="this.subject.value=document.title;">
    <input type="text" name="subject" value="" />
    <input type="submit" />
</form>

To give you a better understanding of the onsubmit event as the name suggests it executes the contained javascript when the user submits the form. The operation this.subject.value=document.title working from right to left basically says assign the value of document.title to the value attribute of the element with the name of subject in this specific form.

Using your existing form it should look like this (I added the onsubmit event and fixed up errors in your html as well as added the appropriate id's to form elements):

<form action="testmail.php" method="post" class="cf" onsubmit="this.subject.value=document.title;">

    <label for="name">* Name:</label>
    <input type="text" id="name" name="name" placeholder="Your Name" />

    <input type="hidden" name="subject" value="" />

    <label for="email">* Email:</label>
    <input type="text" id="email" name="email" placeholder="Enter a valid Email Address" />

    <label for="phone"> Phone:</label>
    <input type="text" id="phone" name="phone" placeholder="Enter areacode and number" />

    <label for="message">* Message:</label>
    <textarea id="message" name="message"></textarea>

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

</form>

Upvotes: 2

r-sal
r-sal

Reputation: 1169

This will set the value of your hidden input field once the page has finished loading.

<script>
$(function() {
    $('input[name="subject"]').val($('title').text());
});    
</script>

Upvotes: 1

jasonslyvia
jasonslyvia

Reputation: 2535

You can use JavaScript to do so, assuming you're using jQuery, bind submit event to your form

$('your_form_selector').on('submit', function(){
    $('<input />').attr('type', 'hidden')
            .attr('name', 'title')
            .attr('value', document.title)
            .appendTo($(this));
});

Upvotes: 0

Related Questions