Katherine
Katherine

Reputation: 573

Dynamically, naming form elements in PHP

I wil create a simple program that is somewhat similar to xampp phpmyadmin wherein the creation of database, creation of table structure is dynamic using forms. I tried to make their names dynamic so that i could loop them.

<?php $b =1; ?>

Field:<input name="field$b" type="text" /></br>

When i submit the form, the value of the form is null. Here's the code after the sumbitting of form

    $a = 1;

    $tblField = $_POST['field$a'];

    echo "field name : ".$tblField;

The output is

field name : 

I'm not sure if what i did is right, in using a variable in the name of forms. but that is the only way i think i could do so that i could make the creation of table columns dynamic..

Upvotes: 1

Views: 1178

Answers (4)

user1646111
user1646111

Reputation:

Un-escaped variables inside single quota will be treated as text:

$tblField = $_POST['field$a'];

To:

$tblField = $_POST["field$a"];

See php NoOb's answer also

Upvotes: 3

samayo
samayo

Reputation: 16495

What is the $ doing in Field:<input name="field$b" type="text" /></br> ?? If you are going to include the dollar sign $ in your input name's field, then the whole code must be encapsulated in the php tags.

like this: <?= 'Field:<input name="field$b" type="text" /></br>'; ?> otherwise, it won't work

Upvotes: 3

Oleja
Oleja

Reputation: 76

It's seems like you forgot to use double quotes instead of single quotes.

$_POST["field$a"];

instead of

$_POST['field$a'];

Upvotes: 2

M. Suleiman
M. Suleiman

Reputation: 848

Something like:

<?php  $b =1;  
echo "Field:<input name=\"field$b\" type=\"text\" /></br>"; 
?>

?

Upvotes: 2

Related Questions