skip to Main Content

So, I am making a tool for users that want to copy / paste lines of text from excel into a text box.
The goal is for each line to be a seperate variable. If possible I’d like to count the lines and turn them into arrays.

<form method="post" action="/TestThisOneOut2.php">
  <label for="CompetitorPartNumber">Competitor Part Number:</label><br>
  <textarea rows="4" id="CompetitorPartNumber" name="CompetitorPartNumber1" cols="50" name="comment">
Test</textarea><br>
  <input type='submit' value='Download Now' class="SubmitButton">

For example a user might enter the following into a text box,

X5044MLGSP4
7PM985DSA
PO94ASDS

I would have no way of knowing if they want to enter 1 line, 3 lines or 300 lines of product codes, but I’d like to make one text box that will automatically split itself apart. The goal would be to have 1 text area text box that users would enter info into.

In the above case I am struggling with a way to programatically identify that there are 3 seperate lines and therefore 3 variables needed. I was hoping there was some way to do it off of a carriage return or something.

Then for the above example: I’d like them to go to an array for example:

$CompetitorPartNumber[0] = $_POST['CompetitorPartNumber1'];
$CompetitorPartNumber[1] = $_POST['CompetitorPartNumber2'];
$CompetitorPartNumber[2] = $_POST['CompetitorPartNumber3'];

Then I can use the data to do database lookups and return data.
Thanks for your help.

I have tried the following

<form method="post" action="/TestThisOneOut2.php">
  <label for="CompetitorPartNumber">Competitor Part Number:</label><br>
  <textarea rows="4" id="CompetitorPartNumber" name="CompetitorPartNumber" cols="50" name="comment">
Test</textarea><br>
  <input type='submit' value='Download Now' class="SubmitButton">
  </form>  

And it sends the posted data to the following script:

<?php 
$CompetitorPartNumber = $_POST['CompetitorPartNumber'];

echo "<h1>".$CompetitorPartNumber."</h1>";
?>

So the problem I have is that when I echo the results they appear as blank spaces in place of the items being on seperate lines. This is good unless a user actually enters a blank space on accident.
I was thinking I could could the blank spaces to make the number of fields in an array. Then split up the variable by blank space character. However, users may enter blank spaces at the end so I don’t think this will be a good strategy as I may get empty results. I was hoping I could ask around for code samples incase someone else knew how to solve this problem.

2

Answers


  1. For a HTML <textarea> element, you find the to its name attribute corresponding value in the $_POST array (<form> with the method=post attribute value).

    The value is a string and lines are delimited by CR LF which is "rn" as string in PHP.

    For example:

    <?php
    
    # ...
    
    # Split the POST textarea value into lines:
    
    /** @var string[] $CompetitorPartNumber */
    $CompetitorPartNumber = explode("rn", $_POST['CompetitorPartNumber']);
    
    # Trim & filter:
    
    $CompetitorPartNumber = array_map('trim', $CompetitorPartNumber);
    
    $CompetitorPartNumber = array_filter('strlen', $CompetitorPartNumber);
    

    To convert the lines back for the textarea field:

    <?php
    
    # ...
    
    $CompetitorPartNumberValue = implode("n", $CompetitorPartNumber);
    
    ?>
    <form ...>
    
      <textarea name="CompetitorPartNumber"
                cols="..." ...
                ><?= htmlspecialchars($CompetitorPartNumberValue); ?></textarea>
    
    </form>
    

    And if you’re looking for some small script to extend for testing, see Simple PHP editor of text files, you can directly code along with the PHP development webserver from the command-line.

    Login or Signup to reply.
  2. the form

    <?php
    $CompetitorPartNumbers = $_POST['CompetitorPartNumbers'] ?? 'Test';
        
    ?>
    <form method="post" action="">
      <label for="CompetitorPartNumber">Competitor Part Number:</label><br>
      <textarea rows="4" id="CompetitorPartNumber" name="CompetitorPartNumbers" cols="50" name="comment">
    <?= $CompetitorPartNumbers ?></textarea><br>
      <input type='submit' value='Download Now' class="SubmitButton">
    

    the target script

    <?php
    $CompetitorPartNumbers = $_POST['CompetitorPartNumbers'] ?? 'Test';
    echo "<h1>plain input</h1>";
    echo "<pre>".$CompetitorPartNumbers."</pre>";
        
    // split by new line
    $CompetitorPartNumbersEx = explode("n", $CompetitorPartNumbers);
    // split by carriage return
    $CompetitorPartNumbersEx = explode("nr", $CompetitorPartNumbers);
    // split by The correct 'End Of Line' symbol for this platform.
    $CompetitorPartNumbersEx = explode(PHP_EOL, $CompetitorPartNumbers);
    ?>
    <h1><code>explode()</code> the values into lines</h1>
    <pre><?= print_r($CompetitorPartNumbersEx, true) ?></pre>
    <h2>count the lines</h2>
    <samp><?='count = '.count($CompetitorPartNumbersEx) ?></samp>
    

    output looks something like:
    screenshot of the output

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search