PDA

View Full Version : preg match and values in text area



pkrishna42
11-03-2017, 12:22 PM
Hi
thanks for your reply
I posted this code earlier but i did not get any response so i posted other code due to this i am not able to use this code pls help me

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>