Jump to content
kennr

Need a modified onsumit JS function

Recommended Posts

Hello. Here is some excerpted code which already works well.

-------------------------------------------------------------------------------------------------------

<script type="text/javascript">
function checkForm(form) {
if(form.username.value == "") {
... // many more lines

</script>

<form action="return_to_calling_page.html" onsubmit="return checkForm(this);" id="logonForm" >
<p>Username:  <input type="email" name="username" autofocus></p>
<p>Password:  <input type="password" name="pwd1"></p>
<p>Confirm Password:  <input type="password" name="pwd2"></p>
<p><br/>
<input type="submit" value="submit">  
<button type="button" onclick="javascript:location.href='about_page.html'">cancel</button>
</p>
</form>
... // many more lines

-------------------------------------------------------------------------------------------------------

 

Then the JavaScript question:

The checkForm(form) function only checks input data against invalid character types.
But we additionally need to check the logon input values are in the company db. So I tried to write

and call a modified function which included the checkForm(form) function. But the new modified

javascript function verifyLogon() does not seem to run at all?

 

Currently the following modified code is in use (which does not appear to call verifyLogon() successfully):
-------------------------------------------------------------------------------------------------------
<form action="return_to_calling_page.html" onsubmit="return verifyLogon();" id="logonForm" >

 

<script type="text/javascript">
function verifyLogon() {
alert("Entered verifyLogon()"); // print something, regardless, but never (yet) seen
checkForm(form) // checkForm(form) does not (yet) run from here
if(checkForm()) { // if checkForm() returns true, do un/pw lookup in db
<?PHP some_php_code1 ?> // run some php, sql find un in db
if($stmt1 = true) { // if un query returns true, then...
<?PHP some_php_code2 ?> // run some php, sql find pw in db
if($stmt2 = true) { // if pw query returns true, then...
<?PHP some_php_code3 ?> // run some php, sql view customer account db records
}else{
alert("Password not found in company db.");
}
}else{
alert("Username was not found in company db.");
}
}else{
document.getElementById("logonForm").reset(); // clears the form
form.username.focus(); // sets focus to username field
return false // return to form
}
}

</script>

-------------------------------------------------------------------------------------------------------

Currently, I am not very concerned about the (if) control logic inside the verifyLogon() function. The

plan is to fine tune the (if) logic later once the verifyLogon() is running. Mostly now just want to get the
verifyLogon() running at all. Thank you.

Edited by kennr

Share this post


Link to post
Share on other sites

PHP runs immediately as soon as the page loads, then it generates the HTML, CSS and Javascript which run independently of PHP.

 

If you want to run PHP after the page has finished loading you have to use AJAX http://www.w3schools.com/js/js_ajax_intro.asp

Share this post


Link to post
Share on other sites

Please accept my thanks for considering this post about a modified onsubmit JS function. In the one day since I made that post I came to realize I should just include include the additional functionality needed directly into the checkForm(form) function. When I did this all the HTML and JavaScript then ran smoothly and dependably. So my need for a modified onscript JS function was satisfied. Still there is at least one important follow up question. Right now I am trying to run PHP from JavaScript. As your replied said, AJAX may be needed. I am researching and testing on my end, need two days. The test plan is to open a new window for each PHP call, run the PHP, then close the window after each call. If it does not work we will use AJAX.

<script>
function newDoc() {
window.location.assign("php_code_file_1.php")
}
</script>

Thank you.

Edited by kennr

Share this post


Link to post
Share on other sites

Opening a window and then closing it would result in worse user experience. AJAX does exactly that but without having to open a window.

 

There's no good reason to avoid AJAX in this scenario.

Share this post


Link to post
Share on other sites

It would be a kindness if one of the Administrators would correct the misspelling in this post title.

I would have corrected it by now but the Forum software does not give me permission.

 

So it is to be AJAX on this project. Very well then, AJAX it shall be. To that goal, I found a copy of this ebook:

http://www.negingostar.com/temp_uploads/pdf/Learn_JavaScript_and_Ajax_with_w3Schools.pdf

 

Expect it will take two weeks (perhaps one week) to read it through. So I expect to be back in Forum then (after the read).

Speak with you then. Thank you

Edited by kennr

Share this post


Link to post
Share on other sites

There's a tutorial on the website, reading a book is not strictly necessary

Share this post


Link to post
Share on other sites

It would be a kindness if one of the Administrators would correct the misspelling in this post title.

I would have corrected it by now but the Forum software does not give me permission.

 

Not Forum Software, Moderator Justsomeguy thought it would a great wondrous decision after what is it about 10years now being enabled, to disable ANY editing after 6hours, you have to ask GOD or other moderator to correct it for you now.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...