1

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?

andresr
  • 93
  • 3
  • 11
  • How is your page title set in the first place? Also, this is a weird question. – Buttle Butkus Oct 02 '13 at 22:45
  • Yeah I didn't know how to explain this question fully. My page title is set in the html. I wanted to know if I could get the page title automatically so I could know from what page the form was sent? Hope you understand my question. – andresr Oct 02 '13 at 22:48
  • 2
    If your page is generated by PHP, can't you put the title in a variable and echo it in the hidden input's value? Or is it a JavaScript question? – Sébastien Oct 02 '13 at 22:50
  • Yeah I could put the title in a variable but then if I use the form in multiple pages I have to change the title on each page right? – andresr Oct 02 '13 at 22:52
  • I agree with @Sébastien and also, other than that, you know that the requeseting page is always available. Look at the `$_SERVER` variable. Specifically I think `$_SERVER['REQUEST_URI']`. That will give you the page filename. – Buttle Butkus Oct 02 '13 at 22:52
  • andresr, that's the thing about a variable. It can be whatever you want it to be. Wouldn't you want to change the page title on each page??? – Buttle Butkus Oct 02 '13 at 22:53
  • @ButtleButkus Yes that's true I'll go with that. I'm going to have to change my html page into php for that right? – andresr Oct 02 '13 at 22:56

3 Answers3

2

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>
rfoo
  • 1,160
  • 1
  • 12
  • 27
  • Yes this worked quick without having to change my html to php. Would this work if a form is created in a php file? – andresr Oct 02 '13 at 23:03
  • 2
    Yeah it'll work anywhere since php outputs html. The browser doesn't care where it came from so long as the information is correct when the browser sees it. I also fixed up your html and gave your form elements the proper id to use in conjunction with the `label for` attribute – rfoo Oct 02 '13 at 23:06
  • Remember the `for="someid"` needs to match an `id="someid"` on some form element. – rfoo Oct 02 '13 at 23:07
  • yes correct forgot to set my `id`. Do I have to change the `button` to an `input` ? – andresr Oct 02 '13 at 23:14
  • @andresr I had made changes the same time you submitted the edit so I couldn't accept it but made the change manually. Sorry about that! – rfoo Oct 02 '13 at 23:19
  • Also I'd recommend using read [here](http://stackoverflow.com/questions/3543615/difference-between-input-type-submit-and-button-type-submittext-button) for more information. – rfoo Oct 02 '13 at 23:23
1

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>
r-sal
  • 1,169
  • 8
  • 9
0

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));
});
jasonslyvia
  • 2,529
  • 1
  • 24
  • 33