| 2:16 am on Apr 22, 2003 (gmt 0)|
<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.
how could i check if the said field is empty then cancels form processing if the said field is really is empty. thanks!
| 7:37 am on Apr 22, 2003 (gmt 0)|
|"...i have several submit buttons on the form and this code causes a problem everytime i click on the other submit buttons..." |
Yes, by saying onsubmit="... you are requesting it fire whenever the form is submitted, regardless of which button is used to submit it. If you only want it to fire when a particular button is pressed, use the onclick attribute of the particular button instead of the onsubmit attribute of the whole form.
|"...after checking on the 'title' field, it still adds the record even if the 'title' field is empty..." |
| 11:01 am on Apr 22, 2003 (gmt 0)|
how could i check if the said field is empty then cancels form processing if the said field is really is empty.
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?
| 4:44 am on Apr 23, 2003 (gmt 0)|
this is just a part of my code....i did not include other functions.
function validate(check) // function that checks if the Title field is empty
alert("You Must Enter a Title");
<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
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
/*********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!
| 8:09 am on Apr 23, 2003 (gmt 0)|
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 ..." |
- All your buttons have the same name ("action"). The purpose of the name attribute is to uniquely identify the button for scripting languages, so each should be named differently.
- Your form doesn't include an action. Where does it go... How do you call your php functions?
- For each button you specify a src="..." It looks a bit strange to me. The src is for a graphic, so you can specify the location of a jpeg, gif or png file instead of the standard grey box buttons; but instead of a graphic you are refering to php files.
- The onblur statement checks the field, but doesn't return focus to it. Once you get the whole thing working, that might be an improvement you could add.