0

I have a form that has fields with the following name

<input type="text" name="value[]">
<input type="text" name="value[]">
<input type="text" name="value[]">

On form submit I need to run a query and store each submitted non-empty values into my db. How do I do that? I suppose I need to run loop but not sure how and especially to avoid blank field submissions.

5
  • 2
    i would first suggest do a print_r($_GET['value']) for get or print_r($_POST['value']) for post and see what prints out. Commented Apr 25, 2011 at 18:26
  • Oh, it submits fine, I can see all fields submitted and which ones have values or not. How do I integrate this in my code though? Commented Apr 25, 2011 at 18:27
  • well, what is your code? Commented Apr 25, 2011 at 18:31
  • I don't have any yet. I was thinking something along the lines: for($i = 0; $i < sizeof($value); ++$i) { //db query } but this does not weed out blank submissions. Do I just run a loop and validate before db query is run? Commented Apr 25, 2011 at 18:34
  • why not to add some condition inside of the loop? and make db query conditional (note that you don't have $value but $_POST['value']) Commented Apr 25, 2011 at 18:37

1 Answer 1

2
foreach($_POST['value'] as $val)
{
   if($val != "")
   {
       //process query, $val will have the tag's value
   }
}
Sign up to request clarification or add additional context in comments.

9 Comments

checking for isset after assigning a variable makes no sense.
I was afraid of that. Edited to restrict empty values.
Can't I add TRIM and then check if ($val != '') or (!empty($val) ?
You're @santa. you can do anything at your wish, don't you? sure you can add trim, nobody forbids you that. empty() is redundant too.
@Col. Shrapnel actually the one I use mainly looks for script inserts. Fairly benign and quite fast. I'd rather strip someone's HTML rather than run a risk of having someone's code stored in my db. But this is a subject for a whole different discussion.
|

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.