how do retain value of dropdown box in php - php

I will try to make it simple as possible. I know how to select a default value in dropdown. My problem is how to make the last option in the dropdown as default "interactively" for next time. so that means that I cannot hardcode the default value.
Lets say I want to multiply 2 numbers. I have two drop down boxes and contain number 1 to 10; at the very first time it will say 1 and 1 since if no default is mentioned, then the default is the number at 1st position. Lets say if somebody picks 2 in the 1st and 6 in the second. Once the user hits calculate, the page will refresh, the answer 12 will be displayed. I still want 2 and 6 stil to be there, and NOT go back to 1 and 1
Please note that if I have action set to "answer.php", and display the answer on a new page and have a back button to come back to initial page. There is no problem. but I don't want that. I want to stay on the same page.
I am not using mysql. All have is a simple for loop and I dynamically load values 1 to 10
I believe I need something like selected = ?????? please fill the blank and where would I put this in the for loop
I have spent whole day on this on google but no luck to what I want
Thanks
Amit

As I see it, there are several ways to do this:
On the page that receives the form data, check which option the user selected and store it in a cookie. When you generate your form, check if this cookie is present; if so, use its data as the default value. This is probably the simplest option.
Store the selected option in a PHP session variable. This will work the same way as #1, except default values will not be saved if the user closes his browser.
Use a login system and store the default value in a database.

There are many ways to do this, the differences come down to how 'deep' you want the storage to be. Without wasting too much time here is a quick list of storage options:
POST vars (this is the solution that best suits your scenario)
Cookies
Session
Database
Server Filesystem
etc etc
Basically the simplest technique is to take the data that is passed and redisplay it, so for your form you have 2 inputs, leftval & rightval, the user hits submit on the form and you multiply them to show the result.
When you build the page to show the result use the $_POST['leftval'] & $_POST['rigtval'] to populate them on the new page. Make sure to sanitize the values and escape them on the way out!

Related

How do I update a value on a page instantly (or ASAP) when it's updated in MySQL database? (without refresh)

Is there a way to pull a value from a MySQL database instantly (or within a second or less) when it is updated and display it on a page without refreshing the page?
The idea is that 2 people sitting at their computers are "racing" to update the value and whoever does it first "wins". If User A clicks a link first, the value will update and User B will see the number change from 0 to 1 and his ability to click the link will disappear.
I am good with clicking a link to change the value from 0 to 1, but I'm not sure how to make the value instantly change for the other guy once the value has already been changed.
I'm starting to think this is just a bad idea for a web app. Regardless, I'd like to know how to do it if it's possible.
This might not be possible with mysql. As mysql does not support realtime communication, it needs something like websockets.

Is it possible to create and run a thread in PHP?

When a PHP page is displayed then I want to start a thread searching for the last value entered in a MySQL table column so that the value of a text input field should be this last value incremented by 1 (it's a number). I want a thread because the page may be displayed for a long time and during that time the column may has been updated many times, so I want to get the value each period of a time interval. When clicking the submit button then the value should be last one in the column. Is that possible ?
Consider this scenario :
one user displays the page , so the field contains the incremented value. Then another user displays the same page, so the field contains also the same incremented value. When they submit the form then there is concurrency problem because they submit the same incremented data ! So how to resolve that situation ?
one user displays the page , so the field contains the incremented value. Then another user displays the same page, so the field contains also the same incremented value. When they submit the form then there is concurrency problem because they submit the same incremented data ! So how to resolve that situation ?
You will not be able to keep abreast of all the possible changes to the highest value in real time. An Ajax based solution which periodically fetches the value will not be fast and reliable enough. What happens if the highest value changes in exactly those 100ms that my request takes to make it to the server? What if a user's network connection is temporarily down, he clicks "save", and two values end up overwriting each other?
The usual solution is to determine the highest value not in the user interface, but at the time of storing the record in the database. A way to achieve this effect automatically is using an auto increment field in the database.
If you absolutely need to display the current value to the user, you will need to employ some sort of locking mechanism. Ie. determine the currently highest value, and reserve that value for the current user until they either save the record, or leave the page. But there is simply no way to display the highest current value to every user in a 100% reliable way. You will end up with collisions this way. Like #N.B. says in the comments, you probably should rethink your design.
You can achieve this through AJAX easily by calling the database or a controller asynchronously every 1 minutes for example - or on a particular mouse event perhaps - and returning JSON data or a view with displayed data. Many popular social network sites do this to get the latest feed / data displayed on their home page.
More info on: http://ajaxpatterns.org/Periodic_Refresh

Add to user input value before storing in the database

I have a form whereby a user fills it in and clicks submit. This then stores the information in the database. I have a field called height. And I want the user to type in something like 10, but I dont want them to type in the measurement aswel. I want this to be added to '10' before storing in the database
So when they click 10 and submit, i want it to show in the database as 10inches or 10".
I tried to google it but I get nothing because I have to be quite specific with keywords and I just dont know how to search it.
Does anybody know how I can achieve this?
Why do you want to store this in the database as text? That should not be a database concern, but instead be a UI concern. Store the 10, and just add the inches when you pull the data out?
Or you could store the 10 as a value alongside a mapping to what type it is (such as inches/cm/etc). This design will give you much more flexibility
You can use javascript to validate the textbox value on form submit (onSubmit)
In server php script, you could also check the value again.

Auto refresh in PHP

I have piece of code which refresh data periodically (after 5 sec.), and put it into a table.
This table has sorting option, and checkbox to select a particular row.
Now problem is when i want to sort or choose a row using checkbox, because of auto refresh it set whole table data in previous position. Means if any data i had sorted will not show sorted and/or checked row will be unchecked again.
Please provide me some suggestion how to handle this issue.
Thanks
Your alternative might be to do an AJAX call to pull the data periodically instead of refreshing the whole page via PHP. That way you'll be able to send the correct parameters to handle the sort towards the logic within PHP.
OR
You can push named parameters/actions within the url for sorting purposes. Then use URL's on the table header and the page reloads with a url of something like:
http://example.com/table/sort:asc
http://example.com/table?sort=asc
And then your logic could appropriately pick the previously selected areas up.
if (isset($_GET['sort'])) {
//Do sorting stuff
}

How to store the form data in a MULTIPAGE form?

I am trying to develop a registration page which involves three separate information.
First Page will get contact details
Second page - working details
Third page - study details.
How to keep the form data of the previous pages before posting the form?
You could do it with Ajax - multiple divs and hide/show the appropriate ones.
Or you could POST each page and save the data in the $_SESSION global variable until all pages are complete. Then save it all to the database.
While the other answers are certainly good ideas, you may also want to consider persisting the intermediate data to your database between each page. So, submitting the first page would create the new row, with the columns relating to contact details populated, and a status column set to a value indicating that the submission is not yet complete.
The second page would update that record in the database. The third page would also update the record, as well as the status flag to indicate the submission is complete.
The main benefit to this is that the user can walk away after the first (or second) page, and then return to it later, even if he had closed his browser and his session had expired. (As long as he has a unique URL to return).
This approach might not have a lot of benefit if you are only collecting three pages of data, but if you had many pages, the ability to leave and return later might be more important.
You should take a look at http://jqueryui.com/demos/tabs/, it should be able to do what you need.
While shifting to another page, you just put the values of first page variable in sessions, then you can access the value of previous page at any page, then post the value to the database query. In this way, you can use the use the value of first page at third page, up to when browser is open. As the browser close then variable lost their values.
Back in the day, I would've put hidden fields for all of the previous pages in each subsequent page, so the final submit would have everything... i.e.
Now, I would probably only have one actual page.. with multiple steps implemented by showing/hiding div's and collecting all of the data in one big form, broken up visually for the user... and if I was feeling especially frisky, with frequent validation and final submission through ajax.

Categories