开发者

perform works before connect to database

i am designing a register form for a site.in this form in html forms we have to password type and a submit button.

<form action="index1.php" method="post">
<b>pass:</b> <input type="password" name="pass" size="25"/>
<b>pass:</b> <input type="password" name="cpass" size="25"/>
<input type="submit" name ="submit" value="comfirm">

on of them are for pass and second for confirm password and a button for submit.

user must fill both of pass and cpass fields until program can store in database

i want that when user click on submit button (for speed up my program) my program check that if two fields are filled are no.if they have filled connect to database and store them into database or if no show a alert to user and doesn't connect to database. my database codes are in index开发者_开发百科1.php.

if it possible i check them with javascript functions with onclick in submit? (i could retrieve form values into a javascript functions and checked them but could not stop program and so it go to index1.php and connect to database that i don't need)

if yes how? if no how can i do that.

if it possible in javascript me go to a particular page in php for example 5.php?


Use the onsubmit event on the form element:

<form action="..." onsubmit="return validate_fields();">

The return keyword is important here. Inside your validate_fields, return false if the check fails or true if it succeeds:

function validate_fields() {
  ...
  if(fields_is_valid) {
    return true;
  }else{
    alert('Fill in all fields!');
    return false;
  }
  ...
}

This will stop the form from being sent if the validation fails.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜