Forum Moderators: open
<form onsubmit="return check(this);" action="...">
it worked pretty good, however, i have several submit buttons on the form and this code causes a problem everytime i click on the other submit buttons.
i also tried to call a javascript function in the 'ADD' button. the function worked well and does check if the 'title' field is not empty. however, after checking on the 'title' field, it still adds the record even if the 'title' field is empty.
how could i check if the said field is empty then cancels form processing if the said field is really is empty. thanks!
"...i have several submit buttons on the form and this code causes a problem everytime i click on the other submit buttons..."
"...after checking on the 'title' field, it still adds the record even if the 'title' field is empty..."
Shawn
how could i check if the said field is empty then cancels form processing if the said field is really is empty.
I agree with ShawnR, a peek at your JavaScript may help, and as he says, no matter which submit button is pressed, the form will run the validation.
I have a question though, why do you have more than one submit button? If one submit button is above some form fields, when the user hits that button the form will submit without the user entering details below. Or am I missing the point?
function validate(check) // function that checks if the Title field is empty
{
if(check.Title.value=="")
{
alert("You Must Enter a Title");
return(false);
}
else {return(true);}
}
<form name="modify" onsubmit="return validate(this)" method="post">
<textarea name="Title" cols="50" onBlur="validate(this.form)">
<?echo $title?></textarea> //this is the required field
/***i did not include other fields***/
<input type="submit" name="action" value="Add" src="modifyvf.php?agent=$agent"> //add button which processes the form
<input type="submit" name="action" value="Modify" src="modifyvf.php?agent=$agent&ID=$ID"> //modifies the title
<input type="submit" name="action" value="Search" src="modifyvf.php?agent=$agent&actions=$actions">; //searches for a certain title
</form>
when clicking on the Add button, it does validate if the title field is empty and cancels the form processing. however, when i click on other submit buttons (s.a. modify and search) they also validate if the title field is empty (which should not be the case).
what i did, instead of calling the jsfxn in the onsubmit handler of the form element, i placed it in the ADD button only using the onclick event.
<form name="modify" method="post">
<input type="submit" name="action" value="Add" src="modifyvf.php?agent=$agent" onclick="validate(this.form)">; //add button which processes the form
</form>
/*********i did not include my php scripts for fxns ADD,SEARCH and MODIFY*******/
this also works but after clicking the "ok" button in the alert message, the form is still processed thus adding an empty record in my database.
in this form, the user can search for a certain title and at the same time can modify it. that is the reason why there are several buttons in this form.
i know something is definitely wrong/missing in my code, please help...thanks!
To stop the form being sent if the validation fails, use the following: onclick="return validate(this.form)"
To stop the form being sent if a different button is pressed, you could use onclick="return false", but the real question is why use a submit button if you don't want the form submitted when the button is pressed? (perhaps I just don't understand what you are trying to do, or how you call your php functions.)
A few other things I can spot:
"... ">; //searches for a certain title ..."Did you just comment it up like this for posting to this board? "//" can't be used for comments in html, only in javascript. Also the semicolon is not used to terminate html tags.
Shawn