This topic is locked
[SOLVED]

  Another Confirmation box question

9/16/2010 11:09:09 PM
PHPRunner General questions
P
procheck author

This script has been mentioned in many posts and it works fine:
confirm on add / edit page
My problem is that I need this based only if 3 of 5 fields change. I can detect this in BeforeEdit but if I use the javascript based on when the SAVE button is pressed, this is all or nothing. I've tried calling separate php code outside of PHRRunner, but the data is saved regardless of my code. I've tried returning to the edit page but again the data is already saved. If I was able to return to the BeforeEdit section just after my call I could Return False but I don't know if this is possible. Any ideas?
Thanks
Al

Sergey Kornilov admin 9/17/2010

You cannot use BeforeEdit event for this purpose as this check needs to be done before submit.
The only way to do this is to track field changes in Javascript and popup confirmation box if required. This will require quite a bit of custom Javascript coding.
Here is the article that explains how to track form changes in Javascript:

http://www.codestore.net/store.nsf/unid/DOMM-4UTKE6
You'll need to modify it to take into account just a few fields.

P
procheck author 9/24/2010



You cannot use BeforeEdit event for this purpose as this check needs to be done before submit.
The only way to do this is to track field changes in Javascript and popup confirmation box if required. This will require quite a bit of custom Javascript coding.
Here is the article that explains how to track form changes in Javascript:

http://www.codestore.net/store.nsf/unid/DOMM-4UTKE6
You'll need to modify it to take into account just a few fields.


Hi Sergey,
I spent the week bringing myself up to speed in Javascript and I have the code working

(which I'll publish when completed). When I click on the SAVE button I call an external javascript

to check for the changed fields and then call another function with a confirmation box.
Is there a way to save the data from javascript?
Thanks
Al

A
ann 9/28/2010

Al,
the only way to save the data is to fill some fields with them.

P
procheck author 9/28/2010



Al,
the only way to save the data is to fill some fields with them.


Hi Ann,
I don't understand what you mean. I've tried changing the document.forms.editform1.value_Goaltenders_1.value

with no luck.
Today instead of using {editform.submit()} I tried creating a separate php file(savedata.php) to use MYSQL UPDATE.

It abends because of undefined variables in the SQL. If I use session_start(); instead of include("include/dbcommon.php"); then I just end up with a blank page.
{BEGIN save_button}<SPAN class=buttonborder> <INPUT class=button onclick="if (isFormChanged()) { window.location.href='savedata.php' } else { window.location.href='hockeypool_info_list.php?a=return' }; void('')" value=Save type=button ?> </SPAN>{END save_button}
savedata.php
<?php

include("include/dbcommon.php");
$sqludate = "UPDATE hockeypool_info SET Goaltenders ='".$values["Goaltenders"]."' WHERE PoolID='".$_SESSION["PoolID"]. "'";
//$result = db_exec($sqludate,$conn);

CustomQuery($sqludate);
//if (!$result)

// {

// die ("Could not INSERT any records: <br />". mysqli_error($mysqli));

// die ("Could not INSERT any records:");

// }
header( 'Location: hockeypool_info_list.php' ) ;

exit();
?>

Sergey Kornilov admin 9/29/2010

Al,
you should pass data from Javascript via form fields. You can either use existing form fields or add a new, hidden one.
i.e.

<input type=hidden name=myfield>
in Javascript use:

document.forms.editform1.myfield.value= 'something';
in PHP use $_REQUEST['myfield'] to access this value

P
procheck author 10/3/2010

I've never used $_REQUEST. My understanding is that $_REQUEST['myfield'] would be populated when the form is submitted (method=post). I've tried displaying it in PHPRunner but there is nothing there. Same with $_POST. Also used a debugger to display it but there was nothing populated. I tried populating (document.forms.editform1.myfield.value= 'something') but this field does not exist. This one does - document.forms.editform1.value_Goaltenders_1.value but it did not work. Any suggestions would be appreciated.

P
procheck author 10/4/2010

I was just thinking that since I intercept the submit statement, is it possible that the super global variables are not being populated because I am not actually submitting the form?

Sergey Kornilov admin 10/4/2010

I'm not really sure what's the purpose of implementing your own data saving routine if PHPRunner does this for you. Don't try to break what works already.
Try something like that:

<INPUT class=button onclick="if (!isFormChanged()) window.location.href='hockeypool_info_list.php?a=return';return false;" value=Save type=submit>
P
procheck author 10/4/2010

It's not that I want to implement a new save routine. I just can't get the data to save within PHPRunner so I was just trying an alternative. The data change is detected correctly in the javascript, but when I return the data is not saved. Your suggestion was something which I've already tried and it returns to the edit screen with the new data still showing but if I click on "back to list", the old data is displayed. I also verify that this is in the table. If this is not possible in PHPRunner then I will just change what I am trying to do. Sounds like it might be possible using Ajax but I will have to learn that at a later date.
Thanks
Al

P
procheck author 10/4/2010

Well sometimes it's good to be stubborn. I now have it working! I looked at one of your other posts and combined it with the other methods.
<SPAN class=buttonborder><INPUT class=button value=Save type=submit onclick="if (isFormChanged()) {editform.submit()} else { window.location.href='hockeypool_info_list.php?a=return' }; return false"></SPAN>
I noticed on your last reply that you had used type=submit and return false I changed my code to this and added {editform.submit()}. Should this be editform1? It works both ways.
Before I post the whole solution, can you please have a quick review to see if you agree with my solution.
Thanks again.

P
procheck author 10/5/2010
I decided to just go ahead and post the solution.
In the HTML HEADER add the line below:

*====================================*
<script type=text/javascript src="include/frmCheck.js"></SCRIPT>
FIND the section marked {BEGIN save_button}. Replace this line with the line below.

*=================================================================================*
{BEGIN save_button}<SPAN class=buttonborder><INPUT class=button onclick="if (isFormChanged()) {editform1.submit()}

else { window.location.href='FieldName_list.php?a=return' }; return false" value=Save type=submit></SPAN>{END save_button}
START of frmCheck.js

*===================*
var rtnCheck = false;

window.onload = isFormChanged;
function isFormChanged() {
var rtnVal = false;

var frm = document.forms[0];

var ele = frm.elements;
// Scan the form fields
for ( i=0; i < ele.length; i++ ) {

if ( ele[i].type.length > 0 ) {

if ( isElementChanged( ele, i ) ) {

rtnVal = true;

break;

}

}

}
if (rtnVal == true)

{

confirmation();

if (rtnCheck == false)

{

rtnVal = false;

}

}

return rtnVal;

}
// check for changed fields against the default field

function isElementChanged( ele, i ) {

var isEleChanged = false;
switch ( ele[i].type ) {
case "text" :

if (ele[i].id == "value_ FieldName1_1")

{

if (document.forms.editform1.value_ FieldName1_1.value == ele[i].defaultValue)

{

return false;

}

else

{

return true;

}

}
if (ele[i].id == "value_ FieldName2_1")

{

if (document.forms.editform1.value_ FieldName2_1.value == ele[i].defaultValue)

{

return false;

}

else

{

return true;

}

}
if (ele[i].id == "value_ FieldName3_1")

{

if (document.forms.editform1.value_ FieldName3_1.value == ele[i].defaultValue)

{

return false;

}

else

{

return true;

}

}

default:

return false;

break;

}

}
function confirmation() {

var answer = confirm("One of your fields has changed (CLICK OK to SAVE your changes)")

if (answer){

rtnCheck = true;

}

else{

rtnCheck = false;

}

}