This topic is locked

Edit OnLoad

2/2/2007 3:41:11 PM
PHPRunner General questions
D
drh author

Hi forum member,
I have an app with a text box (Notes) where users add comments. When the record is added, the default value of the field is now() so the first characters are the date and time. The user can then add comments behind the date/time. This works fine.
Now when the user pulls up the record to edit it, I would like to have a date/time stamp appended to the field on the next line. I would think that I could do this via the Edit OnLoad event.
I do know the sql to append the current date/time to the field followed by a space so that when the record is edited the next time, the cursor is placed after the newly added date/time and a space. Like this:
UPDATE `pos_equip` SET `Notes` = CONCAT( `notes` , '\n', now( ), ' ' ) WHERE `seq` =22 LIMIT 1
I see some posts regarding OnLoad Events, just not making much sense to me today.
Any help, examples, links, etc. would be very much appreciated.
Thanks,

Dave

D
drh author 2/2/2007

Geez, sorry, that was just too easy.
Next is to get the VIEW page to display the Notes field with line breaks. Currently, all the lines run together (no new line like on the edit page). Guess I need to keep looking through this wonderfulforum.
This is the code I added into Edit Onload event.
function EditOnLoad()

{

global $where;

global $conn

// Parameters:

// $where - string with WHERE clause pointing to record to be edited

//** Custom code ****

// put your custom code here
db_exec("UPDATE `pos_equip` SET `Notes` = CONCAT( `notes` , '\n', now(), ' ' ) WHERE `seq` = $where",$conn);
}
Hope this helps someone else.
Any suggestions pertaining to the View page would be super.
Thanks again.
Dave

D
drh author 2/2/2007

oops, I got a little excited. Nope, its not working. Here is my revised code, although its not working, maybe someone can help.
Thanks

Alexey admin 2/5/2007

Dave,
here is the code that should work:

function EditOnLoad()

{

global $where;

global $conn

// Parameters:

// $where - string with WHERE clause pointing to record to be edited

//** Custom code ****

// put your custom code here
db_exec("UPDATE `pos_equip` SET `Notes` = CONCAT( `notes` , '\n', now(), ' ' ) WHERE ". $where",$conn);
}

D
drh author 2/9/2007

Hi forum,

Thanks for your help Alexey.
I am still confused as to what is happening in the EditOnLoad function. When I edit the record, I expect to get the previous contents of the 'Notes' field with the current date/time appended to the field.
What I am getting, is the date/time stamp appended after the comment I add while in the edit program.
Maybe there is a better way to do this, I am open to any suggestions.
What follows is a description of what happens while adding, editing and viewing records in this application.
ADD

field looks like:

2007-02-09 08:54:39 add comment

<SAVE>
VIEW

field looks like

2007-02-09 08:54:39 add comment GOOD
EDIT

field looks like

2007-02-09 08:54:39 add comment (cursor here)
expected

2007-02-09 08:54:39 add comment

new date/time (cursor here waiting for imput)
so I enter a comment into the field

2007-02-09 08:54:39 add comment

here i append a comment to field

<SAVE>
VIEW

field looks like

2007-02-09 08:54:39 add comment here i append a comment to field 2007-02-09 09:03:24
expected

2007-02-09 08:54:39 add comment here

i append a comment to field
what i want

2007-02-09 08:54:39 add comment here

2007-02-09 09:03:24 i append a comment to field
EDIT (2nd time)

field looks like

2007-02-09 08:54:39 add comment

here i append a comment to field

2007-02-09 09:03:24
I append to the field so it looks like this prior to saving

2007-02-09 08:54:39 add comment

here i append a comment to field

2007-02-09 09:03:24 this is the second edit of this record

<SAVE>
VIEW

field looks like

2007-02-09 08:54:39 add comment here i append a comment to field 2007-02-09 09:03:24 this is the second edit of this record 2007-02-09 09:12:36
EDIT (3rd time)

field looks like

2007-02-09 08:54:39 add comment

here i append a comment to field

2007-02-09 09:03:24 this is the second edit of this record

2007-02-09 09:12:36 (cursor here)
added comment prior to saving

2007-02-09 08:54:39 add comment

here i append a comment to field

2007-02-09 09:03:24 this is the second edit of this record

2007-02-09 09:12:36 this is the third edit of this record

<SAVE>
VIEW

2007-02-09 08:54:39 add comment here i append a comment to field 2007-02-09 09:03:24 this is the second edit of this record 2007-02-09 09:12:36 this is the third edit of this record 2007-02-09 09:16:54
I am not so concerned about the formatting of the VIEW, I am concerned that the edit doesn't put the date/time in the correct place.
It is not updating the record when it loads the EDIT page.
But it appends the date/time stamp to the field when I <SAVE> the record after editting.
Any thoughts? This is rather confusing to me, obviously, the EditOnLoad function does not work the way I think it does.

Here is my EditOnLoad function (per Alexey. Thanks for your assistance.)
function EditOnLoad()

{

global $where;

global $conn;

// Parameters:

// $where - string with WHERE clause pointing to record to be edited^M

//** Custom code ****^M

// put your custom code here

db_exec("UPDATE `pos_equip` SET `Notes` = CONCAT( `notes` , '\n', now(), ' ' ) WHERE "."$where",$conn);

}
Thanks for any insite into this issue.
Dave

J
Jane 2/12/2007

Hi,
you can do the following:

  1. add EditOnLoad and AfterEdit events on the Events tab. Here is a sample code:
    function EditOnLoad()

    {

    $_SESSION["added"]=0;

    return true;

    }

    function AfterEdit()

    {

    $_SESSION["added"]=1;

    }


2. builf your project.

3. open generated ..._edit.php file, find following line:

$smarty = new Smarty();

and add following code just after:

if ($_SESSION["added"]==0)

$data["notes"]=$data["notes"]."\n".now()." ";

D
drh author 2/12/2007

Thanks Jane!
It works like a charm. I was a little worried at first because it was not doing anything, then I realized that the notes field is actually `Notes` and not `notes`. Made that little change from your version and now I am on to bigger and better things.
What a wonderful group we belong to.
Thanks,

Dave