Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit the value of a <p> element when an html form is submitted

Tags:

post

forms

I have this code: <p class = "foo">Text</p>

And I also have a form: <form action = "XXX.php" method = post></form>

However, how can I get the value of the <p> when I submit it, as the <p> element can be changed.

So what I mean is to be able to post the value of the <p> when the user submits the form, and to be able to access it from that php file with: $_POST['foo'];

Thanks, I have tried to be as clear as possible.

like image 896
H Bellamy Avatar asked Nov 16 '11 18:11

H Bellamy


1 Answers

You have to use Javascript for that

A jQuery function that will work

$("form").submit(function(){

    var value = $("p").html();

    // If foo already exists
    if( $("[name=foo]").length > 0 )
    {
        $("[name=foo]").val(value);
    } 
    else
    {
         var input = $("<input />", { name : "foo", 
                                      value : value , 
                                      type : "hidden" });
         $(this).append(input);
    }

});
like image 112
Niels Avatar answered Sep 24 '22 17:09

Niels