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
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:
To convert the lines back for the textarea field:
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.
the form
the target script
output looks something like: