Results 1 to 1 of 1

Thread: preg match and values in text area

  1. #1
    Join Date
    Sep 2017
    Posts
    16
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default preg match and values in text area

    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)

    Code:
    <?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>
    Last edited by james438; 11-04-2017 at 10:52 PM. Reason: format

Similar Threads

  1. Replies: 14
    Last Post: 12-18-2013, 02:06 AM
  2. preg regex end parameter /Ui
    By djr33 in forum PHP
    Replies: 2
    Last Post: 06-19-2010, 07:43 PM
  3. Cannot display pulled text from database in my rich text area
    By zeetec in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 10-16-2008, 09:36 AM
  4. Adding text into a text area
    By hmsnacker123 in forum Looking for such a script or service
    Replies: 1
    Last Post: 06-21-2008, 03:01 AM
  5. Make pic append text to text area.
    By nordabeck in forum JavaScript
    Replies: 4
    Last Post: 10-15-2006, 04:21 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •