How to split HTML input fields using multiple PHP variables, implemented through line breaks
P粉647449444
P粉647449444 2023-08-16 13:06:13
0
1
605
<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>
P粉647449444
P粉647449444

reply all(1)
P粉530519234

Form

<?php
$CompetitorPartNumbers = $_POST['CompetitorPartNumbers'] ?? 'Test';
    
?>
<form method="post" action="">
  <label for="CompetitorPartNumber">竞争对手零件编号:</label><br>
  <textarea rows="4" id="CompetitorPartNumber" name="CompetitorPartNumbers" cols="50" name="comment">
<?= $CompetitorPartNumbers ?></textarea><br>
  <input type='submit' value='立即下载' class="SubmitButton">

Target script

<?php
$CompetitorPartNumbers = $_POST['CompetitorPartNumbers'] ?? 'Test';
echo "<h1>纯文本输入</h1>";
echo "<pre>".$CompetitorPartNumbers."</pre>";
    
// 按换行符分割
$CompetitorPartNumbersEx = explode("\n", $CompetitorPartNumbers);
// 按回车换行符分割
$CompetitorPartNumbersEx = explode("\n\r", $CompetitorPartNumbers);
// 按当前平台的正确换行符分割
$CompetitorPartNumbersEx = explode(PHP_EOL, $CompetitorPartNumbers);
?>
<h1><code>explode()</code> 将值按行分割</h1>
<pre><?= print_r($CompetitorPartNumbersEx, true) ?></pre>
<h2>计算行数</h2>
<samp><?='count = '.count($CompetitorPartNumbersEx) ?></samp>

The output result is similar to:

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template