How to make Javascript works in a form!?

I have this script, it works really good in a normal PHP form but it doesn’t work in ScriptCase form. Can someone help me?

var st;
var stsplt;
var ftsplt;
var passflag;

function chkstart () {
var flag = 0;
st = document.myform.startTime.value;
st = st.replace(/[.-]/,":");
if (!/d{1,2}:d{2}/.test(st)) {
flag ++;
}

stsplt = st.split(":");
stsplt[0] = stsplt[0] * 1;
stsplt[1] = stsplt[1] * 1;

if ((stsplt[0] < 0) || (stsplt [0] > 23)) {
flag ++;
}
if ((stsplt[1] < 0) || (stsplt [1] > 59)) {
flag ++;
}

if (flag > 0) {
alert ("Invalid time - please re-enter! ");
flag = 0;
document.myform.startTime.value = “”;
document.myform.startTime.focus();
return false;
}
else {
passflag = 1;
}
}

function calcDiff () {
if (passflag != 1) {
alert (“You must enter a starting time!” );
document.myform.startTime.value = “”;
document.myform.startTime.focus();
return false;
}
var flag = 0;
var ft = document.myform.finishTime.value;
ft = ft.replace(/[.-]/,":");
if (!/d{1,2}:d{2}/.test(ft)) {
flag ++;
}
ftsplt = ft.split(":");
ftsplt[0] = ftsplt[0] * 1;
ftsplt[1] = ftsplt[1] * 1;
if ((ftsplt[0] < 0) || (ftsplt [0] > 23)) {
flag ++;
}
if ((ftsplt[1] < 0) || (ftsplt [1] > 59)) {
flag ++;
}
if (ftsplt[0] < stsplt[0]) {
flag ++;
}
if ((ftsplt[0] == stsplt[0]) && (ftsplt[1] > stsplt[1])) {
flag ++;
}

if (flag > 0) {
alert ("Invalid time - please re-enter! ");
flag = 0;
document.myform.finishTime.value = “”;
document.myform.finishTime.focus();
return false;
}

var hrsDiff = ftsplt[0] - stsplt[0];

if (ftsplt[1] < stsplt[1]) {
hrsDiff = hrsDiff -1;
alert (hrsDiff)
}

var minsDiff = ftsplt[1] - stsplt[1];
if (minsDiff < 0) {
minsDiff = 60 + minsDiff;
}
if (minsDiff < 10) {
minsDiff = “0” + minsDiff;
}

var diffTime = hrsDiff + “:” + minsDiff;
document.myform.timeDiff.value = diffTime;

}

Re: How to make Javascript works in a form!?

where do you put this code?

maybe SC does not accept inline function definitions? Then you should declare your functions as a javascript Method under Programming.

Re: How to make Javascript works in a form!?

Ok, I have added the script to the Method Javascript, and added this script under Ajax Events:

// Javascript function parameters
$javascript_function = ‘chkstart ()’; // Javascript function name

sc_ajax_javascript($javascript_function);

Still not working :frowning:

Re: How to make Javascript works in a form!?

I’m not that familiar with ajax and the sc ajax macro.

When I create a javascript (not ajax) event and call a javascript method, that contains javascript functions, works fine. Did you try calling your function from a javascript event?

Re: How to make Javascript works in a form!?

Yep, I have used the same script:

/ Javascript function parameters
$javascript_function = ‘chkstart ()’; // Javascript function name

sc_ajax_javascript($javascript_function);

If you can tell me about another why of calculating time (endTime - startTime = total) as HH:mm I will be very happy :slight_smile:

Re: How to make Javascript works in a form!?

Are you using the “Javascript” event option to call your javascript function?
Which event?
In javascript events you can use sc_ajax_javascript, because it is PHP.

Re: How to make Javascript works in a form!?

Hi vitorjamil

Can you write me a Step by Step? I don’t really understand what you mean!

Re: How to make Javascript works in a form!?

You should add the code in Javascript/Form/onInit then you can call your JS function from PHP submit event (onValidate)?

Regards,
Scott.

Re: How to make Javascript works in a form!?

Hi Scott

I can’t find the Javascript/Form/onInit! If I choose a Form (under Form Settings/Javascript) then I can only choose onLoad/onSubmit.

Re: How to make Javascript works in a form!?

err, my mistake … onLoad. It has been a while since I have used SC on a regular basis :wink:

You just need to have the JS loaded at start, then you can access it from PHP in code.

Regards,
Scott.

Re: How to make Javascript works in a form!?

Hi Scott

Ok, I have added the above code to the onLoad. Now, can you tell me how do I access it with PHP? I’m not so good to PHP (I’m just a Pilot ;-))!

Re: How to make Javascript works in a form!?

Ok, it looks like this now (I have 2 functions, which one shell I call?):

/**

  • Call a Javascript function after an AJAX call
    */

// Javascript function parameters
$javascript_function = ‘calcDiff ()’; // Javascript function name

// Call javascript function
sc_ajax_javascript($javascript_function);

But no change. Still no response :-/

Re: How to make Javascript works in a form!?

My initial tries did not work. It may be related to the order that SC add the JS to the form. As mentioned, it has been a while since I have used SC … I am a bit rusty on the caveats.

The other solution that should work is to create a new JS button and add your code there. It would replace the default add/edit button for the form.

js_button:


function say_hello(){
alert('hello');
}

say_hello();

The other alternative is to create ajax events for each control and check the values before you submit. I believe that is how I approach it. I like to alert the user at the time of entry, not submit.

Regards,
Scott.

Re: How to make Javascript works in a form!?

BTW Scott, this is the original code. You can try it yourself, it works like a charm:
That’s really weird, that SC made it so complicated!

<form name = "myform">
Enter the start time in HH:MM format <input type="text" id="startTime" name="startTime" size = "6" onblur = "chkstart()" ><br>
Enter the finish time in HH:MM format <input type="text" id="finishTime" name="finishTime" size = "6" onblur = "calcDiff()"><br>
Total time:- <input type = "text" id = "timeDiff" name = "timeDiff" size = "6">
</form>

<script type = "text/javascript">

var st;
var stsplt;
var ftsplt;
var passflag;

function chkstart () {
var flag = 0;
st = document.myform.startTime.value;
st = st.replace(/[.-]/,":");
if (!/d{1,2}:d{2}/.test(st)) {
flag ++;
}

stsplt = st.split(":");
stsplt[0] = stsplt[0] * 1;
stsplt[1] = stsplt[1] * 1;

if ((stsplt[0] < 0) || (stsplt [0] > 23)) {
flag ++;
}
if ((stsplt[1] < 0) || (stsplt [1] > 59)) {
flag ++;
}

if (flag > 0) {
alert ("Invalid time - please re-enter! ");
flag = 0;
document.myform.startTime.value = "";
document.myform.startTime.focus();
return false;
}
else {
passflag = 1;
}
}

function calcDiff () {
if (passflag != 1) {
alert ("You must enter a starting time!" );
document.myform.startTime.value = "";
document.myform.startTime.focus();
return false;
}
var flag = 0;
var ft = document.myform.finishTime.value;
ft = ft.replace(/[.-]/,":");
if (!/d{1,2}:d{2}/.test(ft)) {
flag ++;
}
ftsplt = ft.split(":");
ftsplt[0] = ftsplt[0] * 1;
ftsplt[1] = ftsplt[1] * 1;
if ((ftsplt[0] < 0) || (ftsplt [0] > 23)) {
flag ++;
}
if ((ftsplt[1] < 0) || (ftsplt [1] > 59)) {
flag ++;
}
if (ftsplt[0] < stsplt[0]) {
flag ++;
}
if ((ftsplt[0] == stsplt[0]) && (ftsplt[1] > stsplt[1])) {
flag ++;
}

if (flag > 0) {
alert ("Invalid time - please re-enter! ");
flag = 0;
document.myform.finishTime.value = "";
document.myform.finishTime.focus();
return false;
}

var hrsDiff = ftsplt[0] - stsplt[0];

if (ftsplt[1] < stsplt[1]) {
hrsDiff = hrsDiff -1;
alert (hrsDiff)
}

var minsDiff = ftsplt[1] - stsplt[1];
if (minsDiff < 0) {
minsDiff = 60 + minsDiff;
}
if (minsDiff < 10) {
minsDiff = "0" + minsDiff;
}

var diffTime = hrsDiff + ":" + minsDiff;
document.myform.timeDiff.value = diffTime;

}

</script>

Re: How to make Javascript works in a form!?

putting your code in a Javascript method and accessing it using a JS button or JS event also works (if your code is correct of course…)

Re: How to make Javascript works in a form!?

Hi Freezer

I’m trying to use an Event, but it doesn’t work. If you can make it work with SC form, I will be very happy to hear :slight_smile:

Re: How to make Javascript works in a form!?

I created a javascript method under Porgramming -> methods javascript.
(In this method you can embed other javascript functions, that can only be called from within this method).

Then I created a Javascript onchange event (Form -> Javascript) on one of my fields. In this event I just call the method and it works.

Re: How to make Javascript works in a form!?

How did you call it form Javascript onchange event (Form -> Javascript)? What code did you used?

Re: How to make Javascript works in a form!?

myfunctionname;

Re: How to make Javascript works in a form!?

Bad news, I can’t make it to work. maybe something is wrong with my :frowning:
Did you use your code or mine?