问题
I want to basically create a PHP form that stores data into a database - based on what you enter - this is of course basic.
However, there is one quirk:
This php form must be id based - given a link.
Let's say something like mywebsite.com/form.php=id446
will generate a much different form than something like mywebsite.com/form.php=id447
.
The difference in both the forms is about 3-4 form fields(whether they appear or not), rest is the same
Now, I could brute force this and just make form446.php
, form447.php
, etc. - but I'd rather not.
So is there any function that allows me to do this?
Also, is there any template for making aesthetic and eye-pleasing php forms - that make them easier to fill?
thanks guys, xoxo
回答1:
You can use a single form.php but use if structures in the code to include / exclude the individual parts - so something like:
<form action="doForm" method="post">
Input 1: <input type="text" id="input1"/>
Input 2: <input type="text" id="input1"/>
<?php
//This field only appears if you hit the url form.php?formid=123
if($_GET['formid'] == 123):
echo 'Input 3: <input type="text" id="input3"/>';
endif;
?>
<input type="submit" id="submit" value="submit" />
</form>
Being a server-side scripting language, PHP isn't what you need for making pretty, usable forms - it only cares about the functional processing of them. For pretty forms, you'll probably want some nice HTML / CSS and javascript instead. Have a google for "usable forms" and check out things like this list of jQuery plugins for ideas: http://www.queness.com/post/204/25-jquery-plugins-that-enhance-and-beautify-html-form-elements
--- Edit ---- Should I assume you also need about 10000 different forms with their own unique elements? I hope (for your own sanity!) that's not the case. If you can reduce that at all then you might want to try adding formType as a url parameter instead of just the ID, that way you can parse those out in groups instead.
To be honest this is the sort of job I'd use a database for anyway - I'd set up a table to hold each form's ID number and another field for its associated metadata (maybe in an array or even a serialised array). You could then extract that out in the application and you can still go by using the formId as the only url parameter. From your other comments I guess that the table that holds the saved data isn't formId specific, so this should probably work.
回答2:
Your question is a little vague for me. What is different for example between 446 and 447? Does it have to be totally random? Or do you have a set of fields/form available on first hand?
回答3:
Code may not run correctly, didn't check it, but it gives some idea.
$formTypes = array();
$formTypes["type1"] = array("var1" => "string", "var2" => "textarea");
$formTypes["type2"] = array("var1" => "string");
$forms = array();
$forms["446"] = "type1";
$forms["447"] = "type2";
foreach ($formTypes[$id] as $varname => $vartype) {
switch ($vartype){
case "string":
print "<input name='" . $varname . "'>";
break;
case "textarea":
print "<textarea name='" . $varname . "'></textarea>";
break;
}
}
回答4:
Hallo
To make this work you have to think things through propely. You should start in the database part.
As you conclude yourself you can not hard code each id in php.
You should use the database for this.
Some assumptions for the discussion: The forms that vary is named question1, question2, question3
You should first create a table with one row per id and maybe some additional information about that id.
Then, the easiest way of doing it as I see it right now is quite ugly and bad database design. Add one field to this id table per question, so the table will look something like:
id|question1|question2|question3
446|1|1|0
447|0|1|1
Then, in php you just make a "SELECT * from id_table where id="||id||";"
I don't know the syntax in php without searching the net, but you then just check for each one of the varying questions if the id should have that question, in pseudo code:
if(question1)
{
the question 1
}
if(question2)
{
the question 2
}
HTH Nicklas
来源:https://stackoverflow.com/questions/4538294/how-to-create-an-id-based-php-form