I have more than 40 or so fields on a php page, the fields dont conatain lots of data but they are allot for users to complete.

Is there a way (I did this in VB.Net) to save once the user has edited a field or in VB case "Leave" call the save command to just update the MySql db

so when the update the form it saves every time you leave move to the next textbox or dropdown
Save on the fly type thing

OR save every two minutes ...

Currently I have to let my users click save every few minutes, they dont like this
(Complain way too much)

I basically want to call this every time the user leaves the textbox or dropdown OR every two minutes.

//UPDATE Information
    $sqlupdateinfo = "UPDATE companydetails SET 
    field='posted data from form',
    field='posted data from form',
    field='posted data from form',
    field='posted data from form',
    field='posted data from form',
    field='posted data from form',
    field='posted data from form',
    field='posted data from form'
    where email='$email'";
    $count = $conn->exec($sqlupdateinfo);

Recommended Answers

All 4 Replies

Member Avatar

You can do this via Ajax - where a 'change' prompts a save routine. What you want to avoid is refreshing the page everytime a field is changed - that would be very tiresome. An autosave is possible - which would be based on a javascript timeout/interval - running an Ajax call (in the background). CHoice is yours - do one or the other - obviously.

jQuery and other libraries have cool / easy ways of implementing Ajax.

I would use a small bit of jQuery

$( ".classnameofinputs" ).change(function() {
  $.post('ajax/realtimesave.php', {"field":$(this).id(), "newvalue":$(this).val()}, 
    if (data=='error')
        alert('there is a problem');

then realtimesave.php use the field name and newvalue to update only that part of the database. Realtimesave.php will only be called if a value of a field changes. If they click in then out of a field but do not change anything then the function will not be triggered.

sqlinfo = "update TABLE set $_post['field'] = $_post['newvalue']";

if there is a problem saving to your database then echo "error".

HTML of your form

  <input class="CLASSNAMEOFINPUTS" type="text" value="Field 1" id="COMPANYNAME">
  <select class="classnameofinputs" id="optionfield">
    <option value="option1" selected="selected">Option 1</option>
    <option value="option2">Option 2</option>

This is just a guide and there will be other people who can improve on this idea but this will do what you are looking for.

Member Avatar

One thing I'd change from the above is escaping input values - always escape post variables if using mysql_* functions, otherwise use paramter binding with mysqli or PDO.

Using PDO already :) got a very well known companies website hacked 9 months ago and had allot of trouble so learnt PDO. Being a VB,VB Script,ASP,SQL .. guy I seem to get the PHP code easy.

Back to the issue: I will look into the Jquery method, my problem is here in Africa people take more time reading and completing forms and my pages expire in 15 minutes so they are upset that cannot complete it and when it expires the data is lost that was not saved yet.

Get back to you on this Thank you !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.