Jump to content

Putting For loops into dropdowns and Arrays into radio buttons in a HTML table?


doomy

Recommended Posts

for starters, i am a complete beginner at PHP, so i know almost nothing.

 

so, the basic outline is that i am required to create a table with 2 columns, and use PHP where i can

so the first set of code lays down the table and the ability to type in text of "name" and "surname". ----i think this "input" is HTML, yes?? is there a way to enter it as PHP?

<table border=\"2\">
<tr><th><b>Requirements</b></th><th><b>Selections</b></th></tr>
<tr><td>Name</td><td><input type="text" name="name"/></td></tr>
<tr><td>Surname</td><td><input type="text" name="surname"/></td></tr>

so, for this next section, adding on is the part for "age". i want this to appear as a dropdown selection. i've written PHP where possible, however this does not work when opening the file in a browser. it simply leaves a blank dropdown menu with no options. ????

<tr><td>Age</td><td><select>
<?php
for ($num=11; $num<=22; $num++){
    echo '<option>' .$num. '</option>';
}
?>
</select> 

lastly, i have the part for "activity choice". this again i believe i wrote the radio buttons in HTML??? am i able to write this as PHP????

 

<tr><td>Activity Choice</td><td><input type="radio" name="activityChoice" value "music"/> Music ($30.00)<br>
<input type="radio" name="activityChoice" value "swimming"/> Swimming ($25.50)<br>
<input type="radio" name="activityChoice" value "tennis"/> Tennis ($20.00)<br>
<input type="radio" name="activityChoice" value "basketball"/> Basketball ($15.50)<br>
<input type="radio" name="activityChoice" value "netball"/> Netball ($15.50)<br>
<input type="radio" name="activityChoice" value "dance"/> Dance ($10.50)<br>
<input type="radio" name="activityChoice" value "communityService"/> Community Service (No Charge)</td></tr>
 
please please please help??
Edited by doomy
Link to comment
Share on other sites

 

 

i think this "input" is HTML, yes?? is there a way to enter it as PHP?

No, HTML is used for creating the inputs. PHP is used for retrieving the data entered into the form fields when the form has been submitted. You can also use PHP to generate the HTML for the input fields, example

$fields = array('firstname', 'surname');
foreach($fields as $field)
{
   echo ucwords($field) . ': <input type="text" name="'.$field.'" /><br />'; // generate a text input field for current item
}

 

 

i want this to appear as a dropdown selection. i've written PHP where possible, however this does not work when opening the file in a browser. it simply leaves a blank dropdown menu with no options. ????

The code you posted is correct. You are most likely entered the PHP code in a .html file. Most servers are not configured to interpret .html files as PHP code. You should save your file with a .php file extension.

 

Or you are directly opening your .php file directly in the browser. PHP is a server side language, HTML is a client side language. Web browsers only know how to parse client side languages. PHP on the other hand needs be parsed by the http server, which is why it requires a server environment. 

 

If you are new to PHP you can install a package such as wamp or xampp which will allow you to run your PHP code locally on your computer. Otherwise you will need to upload all your sites files to a webhost and you test your code that way. 

 

 

 

lastly, i have the part for "activity choice". this again i believe i wrote the radio buttons in HTML??? am i able to write this as PHP????

You could generate the radio buttons using PHP, similar to my example code I posted above.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.