Log in

View Full Version : Holding values in textarea PHP



pkrishna42
10-25-2017, 10:06 AM
How to hold the values in text area using PHP validation

My code looks like this


<?php session_start(); ?>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php

require("connection.php");
if($_SERVER["REQUEST_METHOD"]=="POST") { $state = $_POST["state"]; $_SESSION['state']=$state; }
?>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" and !empty($_POST["phone"]) and preg_match("/^\d?[- ]?\d{3}[- ]?\d{3}[- ]?\d{4}$/",$_POST["phone"]) and !empty($_POST["email"]) and !empty($_POST["Message"])and !empty($_POST["name"]) and preg_match("/^[a-zA-Z ]*$/",$_POST["name"]) and isset($_POST['state']) and ($_POST['state'] !== 'NULL') and isset($_POST['city']) and $_POST['city'] !== 'NULL'){
header('location:sucess.php');
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
// define variables and set to empty values
$nameErr = $stateErr = $cityError = $phoneError = $emailError = "";
$name = $state = $city = $phone = $email = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$Err = "Name is required";
} else {
$name = $_POST["name"];
// check name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$Err = "Only letters and white space allowed";
}
}
if (empty($_POST["Message"])) {
$Err = "Message is required";
}
if (empty($_POST["phone"])) {
$Err = "Phone is required";
}
else {
$phone = $_POST["phone"];
if (!preg_match("/^\d?[- ]?\d{3}[- ]?\d{3}[- ]?\d{4}$/", $phone))
$Err = "enter xxx-xxx-xxxx allowed";
}

if (empty($_POST["email"])) {
$Err = "Email is required";
}
if(isset($_POST['state'])) {
if($_POST['state'] == 'NULL') {
$Err ='<div class="info">Please Select State !</div>';
} }

if(isset($_POST['city'])) {
if($_POST['city'] == 'NULL') {
$Err ='<div class="info">Please Select city !</div>';

}
}

}
?>
<?php if(isset($Err)) echo $Err; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend></legend>
<select name="state">
<option value="NULL">-- Please select a state --</option>
<option value="Member" <?php if(isset($_POST['state']) && $_POST['state']=="Member") { ?>selected<?php } ?>>Member</option>
<option <?php if(isset($_POST['state']) && $_POST['state']=="New") { ?>selected<?php } ?>>New</option>
<option value="one" <?php if(isset($_POST['state']) && $_POST['state']=="one") { ?>selected<?php } ?>>one</option>

</select>

<select name="city">

<option value="NULL">-- Please select a state --</option>

<option value="Member" <?php if(isset($_POST['city']) && $_POST['city']=="Member") { ?>selected<?php } ?>>Member</option>
<option value="New" <?php if(isset($_POST['city']) && $_POST['city']=="New") { ?>selected<?php } ?>>New</option>
<option value="one" <?php if(isset($_POST['city']) && $_POST['city']=="one") { ?>selected<?php } ?>>one</option>


</select>
<input type="tel" name="phone" id="phone" required placeholder="Enter Phone" value="<?php echo isset($_POST["phone"]) ? $_POST["phone"] : ''; ?>">

<input type="email" name="email" id="email" placeholder="Enter Email" value="<?php echo isset($_POST["email"]) ? $_POST["email"] : ''; ?>">
<input type="text" name="name" id="name" placeholder="Enter Name" autocomplete="on" value="<?php echo isset($_POST["name"]) ? $_POST["name"] : ''; ?>">

<textarea name="Message" placeholder="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Extra Details" value="<?php echo isset($_POST["Message"]) ? $_POST["Message"] : ''; ?>"></textarea>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
</body>
</html>

DyDr
10-26-2017, 12:04 PM
Textareas don't have value attributes. You would output the value in between the <textarea>output the value here...</textarea> tags.

You are still repeating a lot on unnecessary code, making it hard to even find where in your code the problem lies. Your current code is also only displaying the last validation error, making it hard for a real visitor to your site, since he/she will need to keep submitting the form to find all the errors. The code that I posted as an example cleaned up and corrected the logic for the page. Why did you use that code, then revert to the bad code?

pkrishna42
10-26-2017, 04:22 PM
Hi
thanks for your reply
I posted this code earlier but i did not get any response so i posted other code

In this below code i need pregmatch code for Phone Email and Website Fields and need help in holding values in textarea field
Please find the code below
Your help will be appreciated (I am beginer in PHP)

<?php

// some 'helper' functions -
// apply html htmlentities to a value
function _ent($val)
{
return htmlentities($val);
}

// return an element from an array
function _element($arr,$index)
{
return isset($arr[$index]) ? $arr[$index] : '';
}


$errors = []; // define an array to hold errors
$data = []; // define an array to hold a working copy of the data being operated on (if editing existing data, this is needed. for commonality, use if just submitting new data too.)

// define a list of option choices - both city/state are using the same list. if they were different lists, create two separate defining arrays.
$options = ['1','2','3'];
$options1 = ['4','5','6'];


// form processing
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$data = array_map('trim',$_POST); // get a trimmed copy of all 1st level (non-array) data

// validate all the inputs here
// state and city - select/option, consisting of Member, New, one.
// name - text field, consisting of letters and spaces only.
// phone - unknown if used, but using this recommend code layout, adding code for a field is straight forward.

if($data['state'] == '')
{
$errors['state'] = "State is required.";
}
else
{
if(!in_array($data['state'],$options))
{
$errors['state'] = "Invalid choice for State."; // if you see this error, either there is a mistake in the coding or someone is submitting their own values.
}
}

if($data['city'] == '')
{
$errors['city'] = "City is required.";
}
else
{
if(!in_array($data['city'],$options1))
{
$errors['city'] = "Invalid choice for City."; // if you see this error, either there is a mistake in the coding or someone is submitting their own values.
}
}

if($data['name'] == '')
{
$errors['name'] = "Name is required.";
}
else
{
if (!preg_match("/^[a-zA-Z ]*$/",$data['name']))
{
$errors['name'] = "Only letters and spaces are allowed in Name."; // allowing actual white space, which the original message stated uses a different value in the regex pattern
}
}
if($data['msg'] == '')
{
$errors['msg'] = "Message is required.";
}
// if no errors, use the submitted data
if(empty($errors))
{
header('location:sucess.php'); // note: success was misspelled in the original code
die;
}
}
?>
<?php
require("connection.php");
if($_SERVER["REQUEST_METHOD"]=="POST") { $name = $_POST["name"]; $_SESSION['name']=$name; }
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
// if you want to display all the errors at once, before the start of the form, do that here...
if(!empty($errors))
{
echo implode('<br>',$errors); // add any html markup and css to display them the way you want
}
?>
<form method="post">
<fieldset>
<legend>Please select a state</legend>
<select name="state">
<option value="">-- Please select a state --</option>
<?php
foreach($options as $choice)
{
$sel = isset($data['state']) && $data['state'] == $choice ? ' selected' : '';
echo "<option value='$choice'$sel>$choice</option>\n";
}
?>
</select>

<select name="city">
<option value="">-- Please select a city --</option>
<?php
foreach($options1 as $choice)
{
$sel = isset($data['city']) && $data['city'] == $choice ? ' selected' : '';
echo "<option value='$choice'$sel>$choice</option>\n";
}
?>
</select>

<?= _element($errors,'name'); ?> <input type="text" name="name" value="<?= _ent(_element($data,'name')); ?>">
<textarea name="msg" placeholder="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Leave Your Message" style="height:45px; width:275px; margin-right: 20px; margin-bottom:20px; " value="<?= _ent(_element($data,'msg')); ?>"></textarea>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
</body>
</html>

jymony
11-17-2017, 03:44 AM
You must add code as below,


<textarea class="textarea" id="textarea1" name="textarea1" type="text">
<?php if(isset($_POST['textarea1'])) {
echo htmlentities ($_POST['textarea1']); }?>
</textarea>

Textarea does not have value attribute.

So add PHP code between opening and closing textarea tag.

pkrishna42
11-24-2017, 10:33 AM
thanks for your reply its working