admin管理员组

文章数量:1355748

I'm fairly new to html/php/js and I'm running into an issue when conditionally submitting my form. Basically, what Im trying to do is have it where the confirm('Do you want to submit this form?') function only shows up if every field has a value entered (the checkform() function). If both are true, then the form will submit. Any help would be greatly appreciated, thanks!

<script type="text/javascript">
function checkform()
{
	var myForm=document.frmhot;
	
	if(myForm.status.value==""){
		alert("Please select a timeframe status.");	
		return false;
		myForm.status.focus();
	}
    if (myForm.line.value==""){
		alert("Please select a line.");
		return false;
	}
	if(myForm.reason.value==""){
		alert("Please select a reason code.");
		return false;
	}
	if(myForm.partnum.value==""){
		alert("Please enter a part number.");
		return false;
	}
	if(myForm.badgescan.value==""){
		alert("Please enter a badge number.");
		return false;
	}
	return true;
		
}	
</script>
<form method="post" action="newhotpartgenerate.php" name="frmhot" 
onclick="if(checkform();){
	confirm('Do you want to submit the form?');
}
>
         
<input class="button_text" type="submit"  value="Submit" name="submit" onclick= "checkform();"  />
</form>

I'm fairly new to html/php/js and I'm running into an issue when conditionally submitting my form. Basically, what Im trying to do is have it where the confirm('Do you want to submit this form?') function only shows up if every field has a value entered (the checkform() function). If both are true, then the form will submit. Any help would be greatly appreciated, thanks!

<script type="text/javascript">
function checkform()
{
	var myForm=document.frmhot;
	
	if(myForm.status.value==""){
		alert("Please select a timeframe status.");	
		return false;
		myForm.status.focus();
	}
    if (myForm.line.value==""){
		alert("Please select a line.");
		return false;
	}
	if(myForm.reason.value==""){
		alert("Please select a reason code.");
		return false;
	}
	if(myForm.partnum.value==""){
		alert("Please enter a part number.");
		return false;
	}
	if(myForm.badgescan.value==""){
		alert("Please enter a badge number.");
		return false;
	}
	return true;
		
}	
</script>
<form method="post" action="newhotpartgenerate.php" name="frmhot" 
onclick="if(checkform();){
	confirm('Do you want to submit the form?');
}
>
         
<input class="button_text" type="submit"  value="Submit" name="submit" onclick= "checkform();"  />
</form>

Share Improve this question edited Feb 24, 2015 at 20:51 William J. 1,58415 silver badges27 bronze badges asked Feb 24, 2015 at 20:48 AggieIEAggieIE 1251 gold badge1 silver badge12 bronze badges 1
  • Just a note, this statement myForm.status.focus(); will never be executed because it's after a return statement. – William J. Commented Feb 24, 2015 at 20:51
Add a ment  | 

2 Answers 2

Reset to default 2

Complete working solution with corrections and tweaks for IE patibility to a certain extend.

<script>
    function checkform(evt)    {        
        var myForm = document.frmhot;
        var condition = true;
        if(myForm.status.value==""){
            alert("Please select a timeframe status."); 
            myForm.status.focus();
            condition = false;
        }
        if (myForm.line.value==""){
            alert("Please select a line.");
            condition = false;
        }
        if(myForm.reason.value==""){
            alert("Please select a reason code.");
            condition = false;
        }
        if(myForm.partnum.value==""){
            alert("Please enter a part number.");
            condition = false;
        }
        if(myForm.badgescan.value==""){
            alert("Please enter a badge number.");
            condition = false;
        }
        if(condition){ condition =  confirm('Do you want to submit the form?'); }

        if(!condition) {
            if(evt.preventDefault) { event.preventDefault(); }    
            else if(evt.returnValue) { evt.returnValue = false; }    
            else { return false; }
        }
    }
</script>
<form method="post" action="newhotpartgenerate.php" name="frmhot" onsubmit="checkform(event)">
    <input type="text" name="status"/>         
    <input type="text" name="line"/>
    <input type="text" name="reason"/>
    <input type="text" name="partnum"/>
    <input type="text" name="badgescan"/>
    <input class="button_text" type="submit" value="Submit"/>
 </form>

You have the right idea, just extract your code into its own function and then call that in the onclick.

Add this function:

function checkAndConfirm() {
     if(checkform()) {
        if (confirm('Do you want to submit the form?')) {
            // submit the form
     }
}

And then call it from the onclick attribute:

<form method="post" action="newhotpartgenerate.php" name="frmhot" onclick="checkAndConfirm()">

本文标签: javascriptHtml Form If statement in OnSubmit FieldStack Overflow