We help IT Professionals succeed at work.

Image Button, Form Action and onClick

OldHatt45
OldHatt45 asked
on
I have a form that gets submitted by an image button using the form "action".
I cannot mess with that part of the process.  And, It works the way it should.
However, some addiional processing needs have been requested.  
What needs to happen is when the image button is pressed, a database (mySQL) update needs to happen, before the "form action" submits the form and sends things off.  I have been  searching EE for possible solutions and can't find anything I can use to solve the problem.

I was thinking about using the "onClick" of the image button, but i have seen a couple of questions answered where it looks like this won't work.

I am in desparate need of a quick solution.
Would really appreciate some Major Help

Thanks
OldHatt45
Comment
Watch Question

Multitechnician
Distinguished Expert 2019
Commented:
Use the onsubmit event :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript">
	function ajaxMySQL() {
		alert("doing things on the mysql database");
		// Happy End submit the form now
		return true;
	}
</script>
</head>
<body>
<form action="#" method="post" onsubmit="return ajaxMySQL();">
<input type="submit" value="submit form" />
</form>
</body>
</html>

Open in new window

if the form is submitted you only need to save the form data before you do anyother thing in php

Commented:
Hi OldHatt45,
This can be solved, if you wold like to use javascripting. I would like to suggest you to use jquery framework in order to be able to solve this. An event can be attached to the button to process your DB stuff before the form is actually processed. Not only that, but your complete form can also be processed after your initial DB process is carried out, thus taking care of your DB processing & form submission at the same time.

So my question would be, are you willing to accept jquery into your application to handle the request that you made?

Author

Commented:
leakim971
The "onSubmit" and passing the function to AJAX worked out perfect.  Didn't know if it would, but it does.  Thanks,  It appears as though the onSubmit event fires before the "action" of the form.  Which is essentially what I needed to know.  Thanks Again.

p-nuts,
DIdn't understand what you were getting at.  Sorry

mepro,
I would be interested in jquery, however, not for this particular situation.
But Thanks for the response.

I am awarding the Points to leakim971.  You provided what I needed.

Thanks to ALL
OldHatt45

Author

Commented:
I wish I would have asked the question a little better.
I learned a couple of things without too much pain or delay.  leakim971 provided an excellent answer.
leakim971Multitechnician
Distinguished Expert 2019

Commented:
You need to be carefull with the type of your ajax call, mean synchronous or asynchronous. It must be synchronous (wait the end of the call before continue)
http://javascript.about.com/od/ajax/a/ajaxasyn.htm

Author

Commented:
leakim971,
YUP!
I am familar with what happens in these situations when you don't do it synchronously.
But THANK YOU FOR THE REMINDER!!!!  :-)
OldHatt45