2

If you know of any open source project that does something similar I'd be happy to look at the source code. Example of what I am looking for. You custom build a form that looks similar to

Name:
Address:
Zip Code:
Gender: Male/Female
(Now in this field I would like for the admin to be able to define a logic criteria, If Gender was female then some questions would should up, If male other questions would show up). This would be done dynamically since I would not know what the customer built.

Female Question One: (Logic Syntax that customer would put would be [gender] = 'female')

So How would I go implementing something like that. I would prefer if it is in PHP but other languages would be fine.

Here is a Picture, this would be entered by the user and I would have to parse it. enter image description here

Community
  • 1
  • 1
Mike Diaz
  • 2,045
  • 4
  • 32
  • 55
  • I don't think anybody understands your wishes. What application is the screenshot from? (its name could clear of a lot of misunderstandings!) I'm assuming you want to build a similar app, but one that builds HTML forms instead of the forms(?) this app makes. – Kerstomaat Jul 08 '12 at 19:08
  • @SimonPlus I read that as "I'm building something like surveymonkey, and I want to have conditional questions with complex conditions." I assume that the image is a mockup from the graphic design department. – Stobor Jul 12 '12 at 02:35
  • @SimonPlus a poke around the Google machine suggests that the screenshot came from [REDCap](http://project-redcap.org/) survey tool. See https://www.icts.uiowa.edu/confluence/display/ICTSit/Conditional+multiple+textbox+entry for example. – Stobor Jul 12 '12 at 02:45

4 Answers4

2
$string = "[var_1] = '1' or [var_2] = '2' or [var_3] = '3'";

$pattern = "([\[]+[a-z0-9_]+[\]]+[ ]?+[=]+[ ]?+[\']+[0-9]+[\'])";
preg_match_all($pattern, $string, $string);
var_dump($string);

var_dump() will print the following:

array (size=1)
  0 => 
     array (size=3)
       0 => string '[var_1] = '1'' (length=13)
       1 => string '[var_2] = '2'' (length=13)
       2 => string '[var_3] = '3'' (length=13)

The pattern will match [var_1] = '1' and [var_1]='1'.

Nikola K.
  • 7,093
  • 13
  • 31
  • 39
1

It sounds like you want to build a survey type tool, where people can create custom forms. If I understand it, you need to build a few pieces to get what you want:

  1. A way for people to specify under what conditions a field appears (which you've provided a screenshot of - I assume that's a sample/mock up).

  2. A standardised language which people can use to specify their conditions.

  3. a parser which will interpret what the user types in, and convert it to some standard representation

  4. a complier which will convert that standard representation into the code which displays/hides questions.

You will also have to make some design decisions, and they will determine things like which languages you can use. For example, if it is sufficient to only show/hide questions when the "next" button is clicked, then you can do it in PHP alone. If you need to dynamically hide questions on the same webpage before clicking next, you will need to use javascript or similar.

For parts 3 and 4, there are some helpful answers in Lexer written in Javascript? (javascript) and What is a good parser generator for php? (php) which might point you in the right direction.

Community
  • 1
  • 1
Stobor
  • 44,246
  • 6
  • 66
  • 69
0
<form>
    <!-- some input fields here -->
    <select id="gender" name="gender">
        <option selected="selected" disabled="disabled">Gender?</option>
        <option>Male</option>
        <option>Female</option>
    </select>
</form>

<script language="javascript">
$("#gender").change(function() {
    if($("#gender").val() == "Male")
    {
        // display MALE questions
    }
    else
    {
        // display FEMALE questions
    }
});
</script>

Now you can use AJAX to call a PHP file, select questions from database and then display it using JavaScript (element.innerHTML) or display it manually, without calling another file and using a database.

Here's how it looks like: jsFiddle.

Nikola K.
  • 7,093
  • 13
  • 31
  • 39
-1

Here's a jQuery plugin I've used in the past. Works pretty well from my experience! The technique is called "form chaining", I'm sure you could find more information about it on Google if this solution doesn't work.

cereallarceny
  • 4,913
  • 4
  • 39
  • 74