1

I am working on html form present in a php file as shown below in which I want to count the entered html input values.

Php/HTML Code:

<form method="post" style="display: inline-block; margin-left: auto; margin-right: auto; text-align: left;">

   <div style ="display:flex; align-items: baseline;">
      Articles (EN) &nbsp;
      <input type="text" name="articles_id_en" style="width: 30%;height: 22px;" value="<?php if($data->{"articles_id_en"}<>''){echo $data->{"articles_id_en"};} ?>">
      &nbsp; Articles (FR) &nbsp;
      <input type="text" name="articles_id_fr" style="width: 30%;height: 22px;" value="<?php if($data->{"articles_id_fr"}<>''){echo $data->{"articles_id_fr"};} ?>"><br>
   </div>

   <div style ="display:flex; align-items: baseline;"> 
      Articles Entered (EN) &nbsp;
      <input type="text"style="width: 30%;height: 22px;" value="<?php $values = $_REQUEST['articles_id_en'];  $delimiter = ','; $valuesCount = count(explode($delimiter, $values)); echo "Values Count: " . $valuesCount . "<br>" . PHP_EOL; ?>">
      &nbsp; Articles (FR) &nbsp;
      <input type="text" name="articles_id_fr" style="width: 30%;height: 22px;" value="<?php if($data->{"articles_id_fr"}<>''){echo $data->{"articles_id_fr"};} ?>"><br>
   </div>

   <div>
      <button type="submit">Save</button>
   </div>

</form>

On hitting save, its get saved in a JSON as shown below with their list of values entered. At this moment, I have entered 149968, 149939, 149883, 149877, 149876, 149847, 154303 values so in JSON its showing like this:

{"articles_id_en":"149968, 149939, 149883, 149877, 149876, 149847, 154303"}

Below is the section of the screenshot of the form belonging to the above html/php code:

enter image description here

After entering the values, it display like this:

enter image description here

Following is the html code on inspect:

<input type="text" name="articles_id_en" style="width: 30%;height: 22px;" value=" 14996, 14993, 14988, 14987, 14987, 14984, 15430">

Problem Statement:

I am wondering what php code I need to add so that I can get the count of input values entered.

flash
  • 1,455
  • 11
  • 61
  • 132
  • 1
    You haven't shown enough code for us to be able to help you – Andreas May 25 '19 at 22:13
  • @Andreas I have modified my question. Before, I didn't provide much information – flash May 26 '19 at 05:45
  • Just to be clear, when you submit the `$_POST['articles_id_en']` is converted to json? If you `echo $_POST['articles_id_en'];` you get `{"articles_id_en":"149968, 149939, 149883, 149877, 149876, 149847, 154303"}`? – Andreas May 26 '19 at 07:27
  • Hi, it’s get saved in a json file. – flash May 26 '19 at 07:39
  • This is getting nowhere. Too much unknown for us to be able to help you. You should go read the [how to ask](https://stackoverflow.com/help/how-to-ask) and [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Voting close now. – Andreas May 26 '19 at 08:32

3 Answers3

1
<form method="post">
<input type="text" name="articles_id_en" style="width: 30%;height: 22px;" value=" 14996, 14993, 14988, 14987, 14987, 14984, 15430">
<input type="submit">
</form>

<?php

if(isset($_REQUEST['articles_id_en'])) {
    $values = $_REQUEST['articles_id_en'];
    $delimiter = ',';
    $valuesAsArray = explode($delimiter, $values);
    $valuesCount = count($valuesAsArray);
} else {
    $valuesCount = 0;
}

echo "Values Count: " . $valuesCount . "<br>" . PHP_EOL;
  1. Have a look at this post about getting values from form
  2. PHP function explode creates an array from the value="value1, value2" string that you get with $_GET['articles_id_en'];. The explode function creates an array with one entry per value that is separated with $delimiter character. Note that if values entered into form's input are separated with different character than , the solution will not work because explode looks for a specific character to divide a single string into multiple ones that it places inside an array.
  3. count() just gives an integer number of items that it has between its () so if the array that is returned by explode() has 5 elements it will give an integer = 5.

result:

enter image description here

and after hitting submit:

enter image description here

Jimmix
  • 5,644
  • 6
  • 44
  • 71
0

First, convert your value data in an array using explode() and then count it using count().

   if(isset($_POST['articles_id_en'])){
        $value = $_POST['articles_id_en'];
        $myArray = explode(',', $value);
        echo $count_numbers = count($myArray);
    }else{
        echo "Form not submitted yet";
    }
0

Here, we can also try and use preg_match_all that might work:

Notes:

  1. We need to make sure, that each entry consists of valid digits, using i.e. this regex /([0-9]+)/m
  2. However, it might be also the case where an entry contains a non-digit value in between, such as 149a93, in this case, we have to adjust our regex to overcome this problem by using word boundry, like so /(\b[0-9]+\b)/m

Then, our code might look like:

if(isset($_REQUEST['articles_id_en'])) {
    $values = $_REQUEST['articles_id_en'];
    $re = '/(\b[0-9]+\b)/m';
    preg_match_all($re, $values, $matches, PREG_SET_ORDER, 0);
    $count = sizeof($matches);
    echo $count;
}

Demos

Emma
  • 27,428
  • 11
  • 44
  • 69