Prevent action attribute from running php script with javascript -
this question has answer here:
is there way can use javascript prevent form runing php script. example this:
<form action="somepage.php" method="post> <input type= "text" class= "field" name = "blah"> <input type="submit" value="send" > </form>
i know how validate what's in text box using javascript, want prevent somepage.php run if text box empty. haven't tried cause don't know how it.
hope guys understand problem.
thanks
you can attach function submit
event of form
:
document.getelementbyid('form-id').addeventlistener("submit", function(e){ var field1 = getelementbyid('field1').value; if(field1=='' || field1 == null){ e.preventdefault(); alert('pls fill required fields.'); return; } return true; });
or
below solution uses inline js:
if want run js function before submitting form php script, can use onsubmit
attribute of form,
<form id="form-id" action="somepage.php" method="post" onsubmit="return formsubmit();"> <input type= "text" class= "field" id="field1" name = "blah"> <input type="submit" value="send" > </form>
in formsubmit
function can check value of input, if empty or not, , if empty, can return false;
var formsubmit = function(){ var field1 = getelementbyid('field1').value; if(field1=='' || field1 == null) return false; else return true; }
Comments
Post a Comment