mOna
mOna

Reputation: 2459

how to create a drop down menu for the answers of a survey app in php

I just started to build a survey application which contains different types of answers (e.g. checkboxes, radio buttons, yes/no, etc..

My problem is that I don't know how to show the answer for some questions such as "who are your favourite actors?". I thought about auto-completion or a drop-down box (e.g. in alphabetical order) from which users can select their favourite actors but I don't know how to do it.

I searched for similar questions like How to create drop down menu [closed], Creating A Drop-down Menu,, PHP Dynamic Drop Down Menu, and I read this tutorial however still I am confused how to make this type of answer (also because I don't know how could I design them in my database).

For designing my database, I thought of creating 4 tables:

Questions (questionId, questionText, questionType)

Answers (answerId, questionId, answer)

UsersAnswer (userAnswerId, AnswerId, UserId, userAnswer)

Users (userId, userName, .. )

I'd appreciate any ideas and suggestions.

Upvotes: 1

Views: 377

Answers (1)

Josef E.
Josef E.

Reputation: 2219

You'll need jquery and it's dimensions:

Put at top of page:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dimensions.js"></script>
<script type="text/javascript" src="autocomplete.js"></script>

Now you need function to call...

<script type="text/javascript">
$(function(){      setAutoComplete("searchField", "results", "autocomplete.php?part=");  });
</script>

Now for input (Client side):

<label>Actors: </label><input type="text" id="searchField" name="searchField">

PHP (Server-side):

$link = mysql_connect('localhost', 'dbUsername', 'dbPassword');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db("database")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$result = mysql_query("SELECT name FROM  sks_color");
while ($row = mysql_fetch_assoc($result)) {
$colors[]=$row['name'];
}
mysql_free_result($result);
mysql_close($link);        // check the parameter
if(isset($_GET['part']) and $_GET['part'] != '')
{
// initialize the results array
$results = array();        // search colors
foreach($colors as $color)
{
// if it starts with 'part' add to results
if( strpos($color, $_GET['part']) === 0 ){
$results[] = $color;
}
}         // return the array as json with PHP 5.2
echo json_encode($results);
}

Keep in mind that this is returning a JSON object...

Links:

Upvotes: 1

Related Questions