Domanda

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?

È stato utile?

Soluzione

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 />";
   }
}

Altri suggerimenti

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
   }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top