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.

5
  • 1
    You haven't shown enough code for us to be able to help you Commented May 25, 2019 at 22:13
  • @Andreas I have modified my question. Before, I didn't provide much information Commented May 26, 2019 at 5: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"}? Commented May 26, 2019 at 7:27
  • Hi, it’s get saved in a json file. Commented May 26, 2019 at 7:39
  • This is getting nowhere. Too much unknown for us to be able to help you. You should go read the how to ask and minimal reproducible example. Voting close now. Commented May 26, 2019 at 8:32

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

9 Comments

Please explain how this works and why this works?
@Martin I have tried your code but I am getting the value 1 everytime. Its counting 1 everytime
add this line at the top of script var_dump($_REQUEST['articles_id_en']); and post it result after filling form with your values and sending.
@flash please try updated version of answer with $_REQUEST
Still getting 1
|
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";
    }

1 Comment

I have modified my question. Before, I didn't provide much information
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.