I am new to javascript language,and i couldnt find a javascript approach for this php statement:
if( isset($_POST['submit']) ){
//more if statements
} else { echo 'Error'; }
In other words,i want to make a form validation in javascript to check if a submit button has been pressed.
Use .onclick
:
<input type="submit" />
var submit = document.getElementsByTagName('input')[0];
submit.onclick = function() {
alert("Button pressed!");
}
In JavaScript you don't necessarily "check if the submit button has been pressed", you respond to the submit button's events. For example, the form
has a submit
event:
function someHandlerFunction() {
// respond to the form submit event here
}
var form = document.getElementById('theFormId');
form.addEventListener("submit", someHandlerFunction);
Or you can respond to the click
event of the button itself:
var button = document.getElementById('theButtonId');
button.addEventListener("click", someHandlerFunction);
You have to add an onclick event to the submit button. So when the button in the form is pressed, you run a function.
Basically it can look like this:
function save(){ alert(document.getElementById("myInput").value); }
<input id="myInput" type="text" value="test"> <button onclick="save()">Save!</button>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.