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:
<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.
In javascript onclick event , you can use form. submit() method to submit form. You can perform submit action by, submit button, by clicking on hyperlink, button and image tag etc. You can also perform javascript form submission by form attributes like id, name, class, tag name as well.
The form will be submitted to the server and the browser will redirect away to the current address of the browser and append as query string parameters the values of the input fields.
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.function foo() { alert("Submit button clicked!"); return true; }
<input type="submit" value="submit" onclick="return foo();" />
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">
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>
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'); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With