问题描述:

Hi there i have a form (with specific id) that has checkboxes in it and when the checkbox is clicked, more questions (basicly a text field) will pop up. However some checkboxes have the same questions inside. Then I am saving the data to a mysql database. And i am displaying it afterwards. And the Question is: How to create some sort of switch statement or some algorithm that it would post in the current id and only to desired textfield.

Fo illustration:

0-100kg (Checkbox1)

- Which material? (textfield with name=material)

- Circle radius? (textfield with name=radius)

100-500kg (Checkbox2)

- Which material? (textfield with name=material)

- Circle radius? (textfield with name=radius)

500-1000kg (Checkbox3)

- Which material? (textfield with name=material)

- Circle radius? (textfield with name=radius)

- Roots? (textfield with name=roots)

Code:

Form:

<label class="collapse" for="100to500"><b>100 - 500kg</b></label>

<input id="100to500" type="checkbox" name="100to500">

<div><br>

Which material? <input type="text" name="material"/><br>

Circle radius? <input type="text" name="radius"/><br></div>

<label class="collapse" for="500to1000"><b>500 - 1000kg</b></label>

<input id="500to1000" type="checkbox" name="500to1000">

<div><br>

Which material? <input type="text" name="material"/><br>

Circle radius? <input type="text" name="radius"/><br>

Roots? <input type="text" name="roots"/><br></div>

PHP:

storing variables:

$material=mysql_real_escape_string($_POST['material']);

$radius=mysql_real_escape_string($_POST['radius']);

$roots=mysql_real_escape_string($_POST['roots']);

$100to500 = isset($_POST['100to500']) ? $_POST['100to500'] : '0';

$500to1000= isset($_POST['500to1000']) ? $_POST['500to1000'] : '0';

I am trying to accopmlish that it would somehow save all the data into same id (database row) and save only the clicked on / filled data. I know it is a silly explanation but i am using PHP for 2 weeks only so im a complete noob :)

Thank you.

网友答案:

First, I assume you would not allow multiple section of weight categories? In that case use a select tag to limit user input. secondly set you layout so that it looks like this

[dropdown list/ select]
- Which material? (textfield with name=material)
- Circle radius? (textfield with name=radius)
- Roots? (textfield with name=roots)
- Q4 e.t.c.

filter all your possible question for every weight categories and make a list of all UNIQUE questions.

then lastly, use javascript to show/hide each of the question according to the currently selected weight. it is possible to use php to "dynamically" change the UI of a webpage, but it is kinda silly and is way less efficent than javascript.

The result form will be very easy to extract info from and won't give any possible duplicate textfield id/name

here's a mini sample: http://jsfiddle.net/Nn4zR/1/

相关阅读:
Top