Get Data From a Textarea in PHP

Introduction

In this article I explain how to get text area data in your PHP application. If we talk about the text area, then we can simply say that a text area in a web page is essentially a multiple text box that premises the user to enter a large amount of data. A text box allows getting the data from a text area, you use the "value" property of a text area object. Using XHTML code, I created a text area that has a name and id of the comment.

Property of a Text area object

The property of a text area is:

Property Description
value It specifies the text in the textarea.

XHTML code for a text area and a Paragraph

The XHTML code given below creates a text area that has a name and id comment and it has the area of 50 columns and 5 lines tall.


<
p><b>Please Enter Your comment in the TextBox below:<b></p>

<
p><textarea name="comment" id="Textarea1" rows="5" cols="50"></textarea></p>

 It seems like:

get-textarea-data1-in-php.jpg
JavaScript code to get the text area data


var
$ = function (id) {
           
return document.getElementById(id);
        }
       
var comment = $("comment").value;
       
var charcount = comment.length;
       
if (comment == " ") {
            alett(
"Please Enter some thing");
        }
       
else {
            alert(
"Your Comment is " + charcount + " characters:\n\n" + comment);
        }

When the JavaScript code runs it copies the contents of the text area into a variable named comment. Then it uses the length property of a string object to store the

number of characters in the text area in a variable named "charcount". In the JavaScript code above the if statement checks the text area for a blank then displays

the message "Please Enter some thing" otherwise display a message that shows the number of characters in the comment and the comment itself.


The button element given below creates a button that calls the JavaScript function to get comment data and the characters using the JavaScript code above.

<
button onclick="myFunction()">Get TextBox Data</button>


Now, I provide the full script.

Example

An example that gets the data from a text area, when the user types some data into the text area, and after clicking the button, the text area data and characters will display in an alert box.

<html>

<head>

<script>

    function myFunction() {

        var $ = function (id) {

            return document.getElementById(id);

        }

        var comment = $("comment").value;

        var charcount = comment.length;

        if (comment == "") {

            alert("Please Enter some thing");

        }

        else {

            alert("Your Comment is " + charcount + " characters:\n\n" + comment);

        }

    }

</script>

</head>

<body>

<?php

?>

<p><b>Please Enter Your comment in the TextBox below:<b></p>

<p><textarea name="comment" id="Textarea1" rows="5" cols="50"></textarea></p>

<button onclick="myFunction()">Get TextBox Data</button>

</body>

</html>

Output

When this code runs, the output looks like:

get-textarea-data2-in-php.jpg

After entering something into the text area the output looks like:

get-textarea-data3-in-php.jpg

Fiinally, after the "Get TextBox Data" button is clicked:

get-textarea-data4-in-php.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all