Need help to develop a survey site

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
photonatic
Forum Newbie
Posts: 8
Joined: Thu Jun 08, 2006 4:44 pm

Need help to develop a survey site

Post by photonatic »

Hello, I’m working on developing a survey site (on hobby-basis), where users can create a set or multiple sets of surveys. I have many features to add in mind, but I better stay to the minimum for a start.

Anyway, here’s my problem: When the user decides to create a survey, she can click on the button “Create survey”. Once clicked, a textfield named “Survey title” with an OK-button associated to it, and another button “Add survey topic” will appear*. If the user clicks “Add survey topic”, another textfield named “survey topic” with an “add survey topic”- and OK-button associated to it, plus a button named “Add question to topic” will appear. Thus, as the user clicks, the page should display more and more options in the same page.

My failed solution: I made each “level of options” as PHP-functions that could be invoked as soon as some HTML-variable was detected (using isset) everytime the user clicked on a button. I made all buttons of type ‘submit’ so that the page would be refreshed itself (using $_SERVER[‘PHP_SELF’]). But it doesn’t look like a viable solution to me cause the page will look messed up or have undesired behaviour.

This is why I have come to you guys. I am wondering how to add more and more options (more and more html-code?) in the same page as the user clicks on the buttons. The JavaScript below is an attempt of solving it using JavaScript instead of PHP, but I don't think it's much better than using pHP.


*: The idea of “survey topic” is that the user can then have multiple questions to one particular topic in the entire survey. Example: The survey title might be named “Best soft drink”, and one of the survey topic could be “Preferred flavour type” in which there may be questions such as “Which flavour do you like” and “Do you have any suggestions for other flavours?”

Code: Select all

<html>

<head>

<script language="JavaScript" type="text/javascript">
function create_survey()
{
	window.document.write("<p><input type='text' name='text_survey'></p>");
	window.document.write("<p><input type='button' name='button_topic' value='Add topic' onclick='add_topic()'></p>");
}

function add_topic()
{
	document.write("<form name='anotherForm'>");
	document.write("<p><input type='text' name='text_topic'></p>");
	document.write("</form>");
	//alert("Hello World");
}
</script>
</head>

<body>

<form name='myForm'>
	<input type='button' name='button_survey' value='Create survey' onclick='create_survey()'>
</form>


</body>
</html>
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Anyway, here’s my problem: When the user decides to create a survey, she can click on the button “Create survey”. Once clicked, a textfield named “Survey title” with an OK-button associated to it, and another button “Add survey topic” will appear*. If the user clicks “Add survey topic”, another textfield named “survey topic” with an “add survey topic”- and OK-button associated to it, plus a button named “Add question to topic” will appear. Thus, as the user clicks, the page should display more and more options in the same page.
Sounds to me like you want to use JavaScript, as per your code example.
My failed solution: I made each “level of options” as PHP-functions that could be invoked as soon as some HTML-variable was detected (using isset) everytime the user clicked on a button. I made all buttons of type ‘submit’ so that the page would be refreshed itself (using $_SERVER[‘PHP_SELF’]). But it doesn’t look like a viable solution to me cause the page will look messed up or have undesired behaviour.
The PHP script would have to be submitted every time for this to work, so the script could read in the specific variable you've set. PHP is server side, it will only react to something submitted to it. JavaScript is client side, it can react immediately and pop up different menus and alter different elements on the page.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

bdlang wrote:
The PHP script would have to be submitted every time for this to work, so the script could read in the specific variable you've set. PHP is server side, it will only react to something submitted to it. JavaScript is client side, it can react immediately and pop up different menus and alter different elements on the page.
You could combine javascript and php (ajax) to do this as well.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Jcart wrote: You could combine javascript and php (ajax) to do this as well.
Absolutely, but you're still using JavaScript. ;)
photonatic
Forum Newbie
Posts: 8
Joined: Thu Jun 08, 2006 4:44 pm

Post by photonatic »

Hey jcart, could you elaborate what you mean by combining JavaScript and PHP? Just so that I can grasp the overall idea / concept. Thanks!
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Not to speak for Jcart, but the reference was to using AJAX, essentially a buzzword that means accessing a server side script like PHP from JavaScript utilizing the XMLHttpRequest Object. This allows you to use JS to grab data from a PHP script and include it within your page without having to post/reload the page.

My point was, you're still using JS, whether you use it to create dynamic drop down form elements or otherwise. The same rules apply.
photonatic
Forum Newbie
Posts: 8
Joined: Thu Jun 08, 2006 4:44 pm

Post by photonatic »

bdlang, thanks! I guess I'll have to do some further readings hehe. I had actually wondered if such mechanism was possible, and you've answered that. :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

bdlang wrote:Not to speak for Jcart, but the reference was to using AJAX, essentially a buzzword that means accessing a server side script like PHP from JavaScript utilizing the XMLHttpRequest Object. This allows you to use JS to grab data from a PHP script and include it within your page without having to post/reload the page.

My point was, you're still using JS, whether you use it to create dynamic drop down form elements or otherwise. The same rules apply.
well put, thanks ;)
Post Reply