0

I know how to process something like: <input type="text" name="Textbox_T[]" id="txBox1" />

but I have an unknown number of boxes (they are generated via javascript and are only known to me after they are submitted) that are named like this:

<input type="text" name="Textbox_T1" id="txBox1" />

Textbox_T1
Textbox_T2
Textbox_T3
Textbox_T4
etc

since I cannot do: $_GET['Textbox_T'.$i]

how do I do it?

0

2 Answers 2

2

You could set the textbox name to an array:

<input type="text" name="textboxes[]" />
<input type="text" name="textboxes[]" />
<input type="text" name="textboxes[]" />
<input type="text" name="textboxes[]" />

and then in the code

if (is_array($_GET["textboxes"])){
   foreach ($_GET["textboxes"] AS $value){
      echo $value." entered in a textbox.<br />";    
   }
}

edit: If you can not change the name, you could iterate over ALL GEt-values:

foreach ($_GET AS $key=>$value){
   if (strpos($key, "Textbox") === 0){
      echo $value." has been entered in textbox ".$key."<br />";
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Better to check if it's an array beforehand.
I can't change how I get the textboxes... I can only process them as they come in... As I said in the beginning "I know how to process..", its the way they come in now that has me puzzled
Worked perfectly for my needs, thanks, will choose it as the answer as my time runs out :)
1

Ideally you'd have the javascript add the textareas to submit as an array:

<textarea name="Textbox_T[]" ></textarea>

(I'm assuming you're talking about textareas) because then you just need to loop through that item in PHP once it's been submitted:

foreach($_GET['Textbox_T'] as $text){
   //... do something
}

However, if you're stuck with it, you can just loop through your submitted _GET array and attempt to match based on a substring:

$prefix = "Textbox_T";
foreach($_GET as $key=>$value){
   if (substr($key,0,strlen($prefix))==$prefix){
     //this is one of them! do something
   }
}

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.