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:
- submit the data to me (this part is working)
- 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.
if you need to do something before submitting data, you could use form's onsubmit.
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.
html:
Javascript: to submit the form using javascript
to show onclick message
id="hiddenBtn"
andtype="submit"
that do the submittype="button"
set
onclick
of the current button call afunction
look like below:function foo() { // do something before submit ... // trigger click event of the hidden button $('#hinddenBtn').trigger("click"); }
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:
onclick
button on the client-side: In this case you only need to call a javascript function.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
:You can use
onsubmit
event fromform
itself to bind your function to it.I have this code: