add onclick function to a submit button
Asked Answered
H

6

77

I'm just learning javascript and php. I created a contact form and I'd like the submit button to accomplish two things when I press it:

  1. submit the data to me (this part is working)
  2. read my onclick function (this part is not working)
<input id="submit" name="submit" type="submit" value="Submit" onclick="eatFood()">

<?php
if ($_POST['submit']) {
 ////????? 
}
?>

I'm sending the data to my email, and so I get that. But the onclick function doesn't seem to work. I tried reviewing add onclick function for submit button but it didn't help.

Hulky answered 17/10, 2012 at 22:22 Comment(3)
show us your code, to evaluate whats wrongBreastwork
You don't have an eatFood function in that code.Maitund
document.forms['formID'].reportValidity() returns true if all the form elements validity is true.Sihun
M
94

I need to see your submit button html tag for better help. I am not familiar with php and how it handles the postback, but I guess depending on what you want to do, you have three options:

  1. Getting the handling onclick button on the client-side: In this case you only need to call a javascript function.

function foo() {
   alert("Submit button clicked!");
   return true;
}
<input type="submit" value="submit" onclick="return foo();" />
  1. If you want to handle the click on the server-side, you should first make sure that the form tag method attribute is set to post:

    <form method="post">
    
  2. You can use onsubmit event from form itself to bind your function to it.

<form name="frm1" method="post" onsubmit="return greeting()">
    <input type="text" name="fname">
    <input type="submit" value="Submit">
</form>
Monsour answered 17/10, 2012 at 23:27 Comment(4)
onsubmit of the form is the better handler. If you do not have a form, change to type="button"Almund
@Almund tanx for the tip, I added that to the answerMonsour
No +1 yet due to poor choice in reference material ;)Almund
@Almund I didn't realize the link wasn't in anchor. Thanks for mentioning it.Monsour
H
29

html:

<form method="post" name="form1" id="form1">    
    <input id="submit" name="submit" type="submit" value="Submit" onclick="eatFood();" />
</form>

Javascript: to submit the form using javascript

function eatFood() {
document.getElementById('form1').submit();
}

to show onclick message

function eatFood() {
alert('Form has been submitted');
}
Heriberto answered 23/10, 2013 at 13:0 Comment(0)
M
20

if you need to do something before submitting data, you could use form's onsubmit.

<form method=post onsubmit="return doSomething()">
  <input type=text name=text1>
  <input type=submit>
</form>
Microcosm answered 17/10, 2012 at 22:51 Comment(0)
K
4

I have this code:

<html>
<head>
<SCRIPT type=text/javascript>
function deshabilitarBoton() {     
    document.getElementById("boton").style.display = 'none';
    document.getElementById("envio").innerHTML ="<br><img src='img/loading.gif' width='16' height='16' border='0'>Generando...";     
    return true;
} 
</SCRIPT>
<title>untitled</title>
</head>
<body>
<form name="form" action="ok.do" method="post" >
<table>
<tr>
<td>Fecha inicio:</td>
<td><input type="TEXT" name="fecha_inicio" id="fecha_inicio"  /></td>
</tr>
</table>
<div id="boton">
   <input type="submit" name="event" value="Enviar" class="button" onclick="return deshabilitarBoton()" />
</div>
<div id="envio">
</div>
</form>
</body>
</html>
Kendre answered 31/3, 2015 at 21:45 Comment(0)
A
3
  1. Create a hidden button with id="hiddenBtn" and type="submit" that do the submit
  2. Change current button to type="button"
  3. set onclick of the current button call a function look like below:
function foo() {
    // do something before submit
    ...
    // trigger click event of the hidden button
    $('#hinddenBtn').trigger("click");
}
Adjoin answered 13/7, 2017 at 8:47 Comment(0)
B
0
<button type="submit" name="uname" value="uname" onclick="browserlink(ex.google.com,home.html etc)or myfunction();"> submit</button>

if you want to open a page on the click of a button in HTML without any scripting language then you can use above code.

Basie answered 31/7, 2015 at 21:51 Comment(1)
How does onclick returns myfunction(), but force the browser to stay on the same page. The function will just say "Thank you for your feedback." My form is connected to a local php file(formsubmit.php): <form action="formsubmit.php" id="contact-form" target="_blank" method="post" enctype="application/x-www-form-urlencoded">Sphene

© 2022 - 2024 — McMap. All rights reserved.