|
|
|||
Can you dynamically change the HTML code of a page without submitting a form with PHP?
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 6 Replies
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
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.
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. 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 |
|||
|