How to split HTML input fields using multiple PHP variables, implemented through line breaks
P粉647449444
2023-08-16 13:06:13
<p>So, I'm making a tool for those who want to copy/paste lines of text from Excel into a text box.
The goal is to make each row a separate variable. If possible I would like to be able to count the number of rows and convert them into an array. </p>
<pre class="brush:php;toolbar:false;"><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"></pre>
<p>For example, a user might enter the following into the text box: </p>
<pre class="brush:php;toolbar:false;">X5044MLGSP4
7PM985DSA
PO94ASDS</pre>
<p>I have no way of knowing if they want to enter 1 line, 3 lines, or 300 lines of product code, but I want to make a text box that automatically splits. The goal is to have a textarea textbox where the user can enter information. </p>
<p>In the example above, I'm trying to find a way to programatically identify that there are 3 rows, and therefore require 3 variables. I'd like to be able to do it via carriage return or some other means. </p>
<p>Then for the example above: I want them to be converted into an array, for example: </p>
<pre class="brush:php;toolbar:false;">$CompetitorPartNumber[0] = $_POST['CompetitorPartNumber1'];
$CompetitorPartNumber[1] = $_POST['CompetitorPartNumber2'];
$CompetitorPartNumber[2] = $_POST['CompetitorPartNumber3'];</pre>
<p>I can then use this data to make a database query and return the data.
thank you for your help.</p>
<p>I tried the following: </p>
<pre class="brush:php;toolbar:false;"><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></pre>
<p>It sends the published data to the following script: </p>
<pre class="brush:php;toolbar:false;"><?php
$CompetitorPartNumber = $_POST['CompetitorPartNumber'];
echo "<h1>".$CompetitorPartNumber."</h1>";
?></pre>
<p>So the problem I'm having is that when I output the results, they appear on different lines with blanks instead of items. This is fine unless the user really accidentally entered a space.
I thought I could determine the number of fields in the array by counting the spaces. Then split the variable by a space character. However, the user may enter spaces at the end, so I don't think this is a good strategy as I may get empty results. I wish I could ask if anyone else has a code example of how to solve this problem. </p>
Form
Target script
The output result is similar to: