PHP - Form and Loops. Watch

Dobba
Badges: 0
Rep:
?
#1
Report Thread starter 10 years ago
#1
Right, I'm looking at making a simple form that accepts the input of up to 10 string values and places them into an array, which I can manage. However the next step is to allow the user to select the number of values they wish to enter (from a drop down menu) and the form to change as a result. (e.g. if they select 7 from the menu, the form will change to having 7 input areas)

Any ideas where the hell I start?
0
quote
reply
cascadingstylez
Badges: 16
Rep:
?
#2
Report 10 years ago
#2
The HTML would be something like:
Code:
<select name="component-select">
<?php foreach($array as $list_element): ?>
      <option value="somevalue"><?php print $list_element; ?></option>
<?php endforeach; ?>
</select>
At the top of the HTML page you would include the php document using
Code:
require_once('loop_doc.php');
0
quote
reply
cascadingstylez
Badges: 16
Rep:
?
#3
Report 10 years ago
#3
Oh, I think I totally read your post wrong.

If you want content that updates based on a users events then you will need to use Javascript. I am sure JQuery, Script.aculo.us or Prototype can help you out. They are JavaScript libraries that have functions for manipulating forums. Your problem is a common one.
0
quote
reply
BurningSnowman
Badges: 1
Rep:
?
#4
Report 10 years ago
#4
If you want it to change instantly without a page load then that's not PHP's job. It would need to be JavaScript and this of course limits compatibility.

If you don't mind another page load then unsurprisingly you need two forms. You can use get variables to have one PHP file with two 'modes' of operation, or you can have two files, one for each job.

So say first.php without any get data outputs a drop down numFields and a next button, submits the result back to first.php.

Then first.php with the get data does a loop with $ii=intval($_GET['numFields']); (for instance), outputs a field for each iteration, and submits to second.php to do whatever else.

If you don't want first.php to need a 'next' button you could start off this way and then have JavaScript hide the button and auto-submit to the next page when the drop-down's changed. This provides the quickest navigation for JS users and graceful degradation for everyone else.
0
quote
reply
cascadingstylez
Badges: 16
Rep:
?
#5
Report 10 years ago
#5
Design the transaction for users without JavaScript. Once implemented and working, progressively enhance it by using JavaScript. At least that way if the user has no JavaScript they are not limited.
0
quote
reply
BurningSnowman
Badges: 1
Rep:
?
#6
Report 10 years ago
#6
(Original post by cascadingstylez)
Design the transaction for users without JavaScript. Once implemented and working, progressively enhance it by using JavaScript. At least that way if the user has no JavaScript they are not limited.
That's what I said.
0
quote
reply
cascadingstylez
Badges: 16
Rep:
?
#7
Report 10 years ago
#7
(Original post by BurningSnowman)
That's what I said.
Ah but graceful degradation is when you start with a JavaScript based form, and then provide an alternative for browsers without JavaScript. Starting at the top and working down.

Progressive enhancement is where you first design for users without JavaScript and then enhance the experience for those that do support it. Starting at the bottom and working up.

0
quote
reply
Dobba
Badges: 0
Rep:
?
#8
Report Thread starter 10 years ago
#8
If I ditch the drop-down idea and instead just have them input and then submit a number between 1-10 is that then do-able in PHP using a loop?
0
quote
reply
BurningSnowman
Badges: 1
Rep:
?
#9
Report 10 years ago
#9
(Original post by cascadingstylez)
Progressive enhancement is where you first design for users without JavaScript and then enhance the experience for those that do support it. Starting at the bottom and working up.
It's the same result, I don't care what order you code it. I would always plan graceful degradation using plain HTML and modifying with JS, but it doesn't mean I didn't think of adding the JS interface until I was done.
(Original post by Dobba)
If I ditch the drop-down idea and instead just have them input and then submit a number between 1-10 is that then do-able in PHP using a loop?
Yes. To PHP it's exactly the same as a select box option's value attribute.
0
quote
reply
cascadingstylez
Badges: 16
Rep:
?
#10
Report 10 years ago
#10
Mate, I am just helping you out by correcting your use of terms. You said it was the same thing ... when it isn't.

I wasn't stating what order I code it, I was just stating the 2 terms are totally different and should not be confused as you did. One thing I have learnt is to respect those who know something you don't because you learn from them.

0
quote
reply
BurningSnowman
Badges: 1
Rep:
?
#11
Report 10 years ago
#11
The two as you describe them are only different methodologies, i.e. the order you make it. By your description, the end result is the same, so the distinction is totally irrelevant here when it's a single page all developed at once. I've always seen graceful degradation used to mean the behaviour of a finished product, not a methodology. But then I just checked and "progressive enhancement" has been coined since I started doing this stuff, so that we can have more pointless jargon. :p:
0
quote
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Were you ever put in isolation at school?

Yes (98)
25.65%
No (284)
74.35%

Watched Threads

View All