Jump to content

Can you dynamically change the HTML code of a page without submitting a form with PHP?

Alejandro Ramirez's Photo
Posted Jun 06 2011 05:48 PM
6966 Views

I am building an online survey using PHP and would like to show each question separately (similar to when you take tests online).

Can I use a single Page with PHP dynamically changing properties and storing answers, or should I create a separate HTML page with a form in each with a submit button on each page?

How do you retain the values across questions without savi to the database until the user reviews all the answers and submits it at the end? (global variables in a single php file -code behind-?

Thanks!
Alejandro Ramirez
Digital Photography Club
Administrator

Tags:
0 Subscribe


6 Replies

0
  bogomilshopov's Photo
Posted Jun 08 2011 05:16 AM

Hello Alejandro,
PHP is a dynamic language - you may create what you want with it. And the answer is yes.

For example if you create a file: poll.php, you can add parameters like this

poll.php?question=1


and to get questions and answers from mySQL database without changing HTML (you will use only one HTML file)

//Bogo
+ 1
  scrappedcola's Photo
Posted Jun 08 2011 07:09 AM

By combining some ajax on your client side with your php on the server side you can accomplish whatever you want. You can also persist your question data either in the server side or client side.

If you wish to persist on the server side set up a second db table to maintain your temporary question answers. Then when the user completes the questionnaire move those temporary answers to the final answers table.The pro's of saving on server side is that the user can leave the questionnaire and come back to finish it later.

If you wish to use the client side do not use form submits, but instead use a javascript framework for Ajax and just refresh the area of the page that has the questions. You maintain the question answers in a data structure (JSON object would be very well suited for this) on the client. Since the entire page does not refresh the data structure will persist. Then on the final answer you pass the JSON object back to the php server for final saving.
+ 1
  visualAsparagus's Photo
Posted Jun 08 2011 11:11 AM

As a follow on to scrappedcola's comment -

I thought I'd mention that you can also save the user's data before submission by using either a cookie or a session on the server.

This may be efficient if you're not storing and analysing the data yourself but sending it on via an API.
I like to call myself a creative realist.

I try to understand relationships, identify issues and create solutions. Bring me an idea and I'll help you make it better, then I'll help you figure out the best way to get it done.
0
  Alejandro Ramirez's Photo
Posted Jun 09 2011 03:31 PM

Thank for the pointers, I opted for creating multiple pages and using the PHP Session to store values in between. It's not exactly an ideal solution, but it works for now.

Thanks!

The reason for this decision was that the PHP way would have been rather... convoluted and the only alternatives would have been AJAX, jquery and Javascript. So I opted for a little less elegant solution, but one that gets the job done. At least for now.


Scratch that off...

I opted for switching to good ol' Javascript for dynamically changing form tag properties, while storing internal values in variables.

Getting there...

This post has been edited by Alejandro Ramirez: 24 June 2011 - 08:38 AM

Alejandro Ramirez
Digital Photography Club
Administrator
0
  Indianic's Photo
Posted Mar 09 2013 01:43 AM

I think you have to go to the best PHP web development company where you get all the answers of your question. I also have so many question but one company IndiaNIC solve it very easily. Their expert PHP developer solve it very easily. I think you have to contact them.
0
  morgan321's Photo
Posted Mar 22 2013 03:13 AM

Thanks for haring such a nice answer. I really like your hard work on this topic.