This question might sound a little amatuer but just curious to find out.
So i've got a form field that consists of mixed text-boxes, selectboxes and textareas. It stores its user-inputted values in the database and have the values re-inserted everytime user wants to re-edit the form.
My question is for the re-insertion process of the form fields via stored values in my database, is it better to use 1) php mysql queries or 2) doing a .post that calls another script, json-encode the data and using jquery to insert the values into the form on .ready?
I would appreciate if someone would be able advise me the better way to do it and explain the complications of each method. Like say, which method would you choose and why would you choose it?
Would it incur more bandwidth? disk space on my host computer?
Any help would be greatly appreciated.
Either way is valid, but I personally would go with the PHP approach. The reason for this is if you do it via AJAX then you're form will load and a second later your fields will populate. That doesn't feel very professional and damages the customer experience. Whereas with PHP prebuilding the data, the form is already built and ready to go =)
As far as optimization, it's going to be un-noticable on the PHP side. But they will certainly notice un-populated fields suddenly being populated.
Related
How can I remember checkboxes in Smarty when I create them dynamically?
If a user fills in a webform and he submits the page and there are errors they are always gone. (The ones that are checked)
I should have a way to store them.
Currently I set them the following way (in a {foreach}:
{html_checkboxes values=$event#key output=$event#key selected=$event id=$event#value name=$event checked="1"}
Thanks a lot for helping me out.
That question isn't Smarty-specific in my eyes. It can be done in more than one way. Two examples to demonstrate how to cope with this:
Use a JScript libraries form validator to validate the data before submitting. An example if you're using JQuery can be found here: http://jqueryvalidation.org/ (there are many more out there for nearly every JScript library, I just chose JQuery as an example because its current widely acceptance).
Don't transfer the form data to your server using the form submit, use some AJAX to submit them and wait for a response from your server. If the response is "oops, error", show the user the field which contains the error.
Again, there's more than those ways of doing it, but - depending on what the form does - those two are usually the fastest and easiest to implement.
This is very hard to explain but I'm going to try.
We run a motor shop that has a QC program. The program was coded in access97 and it's time for an upgrade, we have elected to try a PHP/MySQL approach to do this.
Right now the access software has several pages to the form and each box sends to the database live so when you type something in you don't have to hit a save button or next or anything and when you come back it's there.
Also the forms are driven by an auto-incremented job number that you can punch into a field at the top of the page and it query's the server and displays all the data in the form boxes so you can edit it.
I don't know how to even start this project. I got a working form and an insert.php page but I don't know how to go about the rest.
If I could get a pointer in the right direction that would be appreciated. Thanks!
You just want it to save automatically? You'll have to look into JavaScript, and more specifically AJAX. I recommend using the jQuery library. Basically, you're going to want to make an AJAX call every time your form field is modified, and that AJAX call will simply update one field in particular.
I understand you are likely very new to website design, so this might be complicated for you.
I would read through this W3Schools tutorial. After reading through that, I'd pay close attention to this tutorial.
Again, this is difficult for beginners. I'd recommend you continue to work at your script, and ask more specific questions here on StackOverflow as time goes on. Good luck!
I have created a simple example here:
HTML/JS:
shaquin.tk/experiments/ajax.html,
PHP: shaquin.tk/experiments/qc.txt.
Have a look at the source to see how it works (I also have some comments in my code), feel free to copy it and modify for your own needs.
To sum up how it works:
When text is typed into a text box, a list of changed elements is updated.
Every updateInterval milliseconds (default 1000), the list is checked. (This helps reduce traffic and lag.) If anything has changed, the PHP file is called to update the database, and the list is cleared.
If an element loses focus and it has changed (e.g. copy/paste), the PHP file is called.
The PHP file sanitizes the query, checks for a valid job number, and updates the database.
References:
AJAX XMLHttpRequest
setInterval
addEventListener
encodeURI
mysqli_connect
mysqli_query
mysqli_real_escape_string
You'll need to submit the data as an ajax request. That way the data can be sent and returned without the page needing to be reloaded to update the information.
Thanks everybody. (For some reason I couldn't up vote answers, so everyone wins today :] thanks again!)
Complete php and javascript novice here, apologize up front for anything half-witted.
I have a portion of a user's profile in which I'd like the user to be able to add additional items of the same thing, with slightly different conditions.
For example, let's say it's favorite books. A fieldset contains some checkboxes for genre and an input text box for the title of a book. After the user checks a genre and fills in a title, they can add the book to their set of favorite books and then have the option to add another. When done adding books, they move on to the next fieldset, complete the form and submit.
How is this done? And more-importantly, is it possible without javascript?
Without JS, I understand this probably entails a lot of reloading of the page to add the items, regardless I'm more confused about how the $_POST data is handled, both before and after the submit.
Sorry for such an open ended question, really just looking for someone to point me in the right direction, as searching for this topic proved to be a bit difficult.
Thanks.
Some Clarification
I'm trying to develop an application that is as independent of javascript as possible. In that sense, I don't know if it's possible to add the new items with PHP alone. My sense is the fieldset in question could have it's own submit button, the action of which POSTS to the page itself (no DB interaction, etc), and variables like $book_genre1, $book_title1 are populated in the page. Then, the "official" submit is sent later, which actually adds the POST data, which contains the books array, to the DB. But I don't know if that is a safe procedure or good logic to begin with.
In reply to the above answer, if that is exactly what you need since I seem to have a different idea.
You simply store each addition in an array stored inside a session variable, and in each page load, parse the data into readable html.
$_SESSION['form'][] = serialized_form_data;
On each load,
foreach ($_SESSION['form'] as $form) {
unserialize_data_and_create_html();
}
add_new_form_element();
I'm assuming you want to show the user already filled forms so he can deal with them as he wishes.
This is a better implementation than what I thought of earlier. I wanted to implement a db version.
Sorry for the delay. I can't comment since I'm mobile (js issues) so I decided to edit instead.
You can simply use the $_SESSION['form'] for your inserts.
A simple foreach will work as well. However, remember to sanitize each value properly before inserting it. That's the key.
If you use prepared statements with binding, you have the advantage of clean input as well as better database performance.
What you're really asking is how to persist information between POSTs of the same form. The most common and effective way of doing so is to just use the inputs on the form. If you have information that shouldn't be displayed then use hidden input elements for them.
Note that storing information in the form like this is not considered secure since it can be manipulated and/or forged, so the next option is to store it in the session; since it is server-side only the cookie/session ID needs to be protected. The values can then be retrieved from the session in PHP after the POST has occurred.
Before you submit a form...there is no data to handle. Once a form is submitted data then is sent to the server in the form of an array.
(From php.net/manual/en/reserved.variables.post.php):
$_POST = "An associative array of variables passed to the current script via the HTTP POST method."
So a user submits some data to the server. Now that data is available to your scripts for use: Populate a database, validate the values before doing "x", write HTML to the client, etc.
I like where #frosty is going with his approach. It does potentially send multiple requests to the server, but it's also a very straight forward approach. A completely server-side solution.
JavaScript or the jQuery Library would allow you to accomplish a similar result, and post to the server just once - by simply hiding/showing populated fields, writing additional fields to the form dynamically (eg: After completing "book #1" the user can click "Add another" and JS writes an identical set of form fields all referencing book #2). Obviously, this approach will get more involved than a straight PHP approach. And you'll need a backup plan if you want to allow users with JS disabled to participate.
I would suggest you use javascript to handle the user adding multiple items. Check out jQuery (http://www.jquery.com) which is a fantastic javascript library that will help you do lots of great dynamic things.
Then once you have all of the information from the user, just once it all at once to your php and save it. For information about to access and use $_POST or $_REQUEST, you'll need to check out some PHP tutorials or pick up a book. That's fairly basic PHP stuff and too large a topic for this thread.
Apologies I can't post any code at this time cos I'm away from my desk, but I will try and explain my problem as clearly as possible in the hope someone can help!
I have a long form that submits to a method in a controller. To make things more user friendly I have split the form into sections. Each section has a hidden field that acts as a flag. The method that the form submits to is made up of conditionals that test for these flags. If a flag is found then the next part of the form can be displayed. I can also gather the data in sections rather than processing the whole lot in one go.
My problem arises when I try to use the set_select method to set a default value for select menus. The method is part of the CI system form_validation library. If an optional third parameter is passed to the method as true it should set that value as the default. However, this only works if the form_validation hasn't previously been run. This makes perfect sense because it is assumed that a default would only be needed the first time a form is seen ie. before any submission and therefore before any post data.
Ofcourse this isn't how it is in my situation. If i simply empty the post array then that defeats the point of the set_select function because any select menus higher up the form would no longer be able to be set to their last value.
Can anyone think of a good solution? I know this would be much easier with AJAX, but I want to make sure that everyone gets the best possible experience whether or not they decide to use JavaScript.
Personally, I would use AJAX to break the form up into your 3 sections, submitting by AJAX each time.
THEN for non-javascript users (the few there are), I would just display the form in its entirety, and process normally.
breaking the form up sounds like a "feature" not a function. Meaning that javascript users will be able to take advantage of the "feature" of breaking the form up, while non-javascript users will still be able to effortlessly accomplish the "function" of submitting the information.
what about doing it like this:
split it into 3 different form in 3 different method
on each method, set switch using session to check what form currently is
add form validation on each of these form and check accordingly
if those 3 form is OK then you can proceed to whatever process you want
if some form is failed, you load back the previously failed form
I've decided that using set_select is probably not the best option for me, so I've added some code into my view files which will do this for each individual case rather than trying to use a generic function.
Not ideal, but is working!
I have 3 arrays of data that are used to populate some fields on screen. When a user posts the form (unrelated to the data contained in the arrays) I want to display the array data on screen again, but without retrieving it from the database when the page reloads. What's the best way to temporarily store these values?
I was thinking of storing them in the session, is that bad practice? Is there a better way, perhaps passing them as hidden form values?
Another option could be to serialize the array and save it into a temporary file.
About the question session vs. hidden form fields: The disadvantage of the form fields is that hackers could see it in the HTML source code and misuse it. So you would have to do some extra checks to see if the form fields are in any way valid or not.
The problem with session and serialize is, that the information would be laying around on the server if the user is moving away from the website before he finished the whole process.
And the last thing: You are not writing how large those arrays are. If each of those 3 arrays have several thousand entries then serialize could be a better option than the session and form fields.
Probably sessions is what you need. But the other things should be taken into account too.
Generate a set of hidden inputs in the form. Then you can just read them from the post.
Sounds like a perfect use of session. I'd just caution to make sure that you have proper error handling in the database function, because you definitely don't want to mislead a user into thinking the data was saved, when there was actually an error.