0

I have created many textboxes and I want to put all of the values into array only if they have themselves filled. I need them to work as how checkbox works in HTML (only the checked ones will then put into array). I use PHP language here. How to do that?

This is my simple HTML textbox:

<input name="array[]">

Would really appreciate for any help you give to me. Much thanks!

2
  • Iterate over array and check with empty Commented Jan 26, 2017 at 15:37
  • That's what I'm thinking of but I didn't know which function should I use. Now I have my problem solved, thank you for the answer. Commented Jan 27, 2017 at 9:11

1 Answer 1

1

Firstly, I'd recommend you change your name to something more readable:

<input name="name[]">   

Next, you want to get your data, I am assuming your form is using POST. We're going to store the form data into a $names array variable.

$names = $_POST['name'];

Next, we're going to create a new array variable which will store input values that have data.

$namesWithData = [];

We're now going to loop through the $names array. This loop will add any fields with data to the $namesWithData array.

foreach($names as $name) {
   if(!empty($name) {
      array_push($namesWithData, $name);
   }
}

The $namesWithData array has the data ready to use.

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

2 Comments

This really helps me a lot. Didn't know if there is such function like array_push(). Frankly, I am still dumb in PHP. Thanks!
Not a problem, glad to help! :)

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.