Reputation: 243
I want to create dynamic names based on a variable value. I use a variable $count
which holds 0 initially and is incremented giving each input type a different value.
Sadly after I submit the form and I try to acess those variables they don't exist. The variables should be $0,$1,$2 etc depending on the number of the input types. Before acessing them I have extracted the POST array.
while ($nrpyetjeve >0)
{
$raport="SELECT * FROM $emrikategorise WHERE Id=$nrpyetjarandom";
$result1=mysql_query($raport);
$rrjeshti=mysql_fetch_assoc($result1);
echo "<h3>- {$rrjeshti['Pyetja']}</h3><br/><br/>";
echo "<ul><li>{$rrjeshti['Pgj1']}</li>";
echo "<li>{$rrjeshti['Pgj2']}</li>";
echo "<li>{$rrjeshti['Pgj3']}</li>";
echo "<li>{$rrjeshti['Pgj4']}</li></ul>";
echo "<br/><br/>";
This is the problem
echo "<input type=\"text\" size=\"15\" name='$count'/>";
echo "<br/><br/>";
array_push($zgjidhjet,$rrjeshti['Pgjsakt']);
$nrpyetjeve=$nrpyetjeve-1;
$nrpyetjarandom=rand(1,$nrrjeshta);
$count=$count+1;
}
Upvotes: 0
Views: 354
Reputation: 79024
If you use register_globals
or try and extract()
these variables, they are invalid, as PHP variables cannot start with a number (or be a number). You need to access the $_POST
superglobal. A better approach would be to use an array:
echo "<input type=\"text\" size=\"15\" name='text[$count]'/>";
Then access as $_POST['text'][0]
etc. Now all of your text inputs are grouped under the $_POST['text']
and you can group checkboxes etc. as well. You could then extract()
these (not recommended).
Upvotes: 1
Reputation: 324820
"Register globals" will only work if the array keys of $_POST
et al. are valid variable names.
For example, if you have <input name="foo-bar" />
, you can't expect $foo-bar
to come out as a variable. Instead, use $_POST['foo-bar']
.
In your case, it would be $_POST['0']
, $_POST['1']
and so on.
In general, relying on "Register globals" is a bad idea.
Upvotes: 2