名前、電子メール、ファイルのアップロードを含むHTMLマルチパートフォームがあります。JavaScript:フォーム送信の前にAjaxを呼び出すための制御ロジック?
私はまずユーザーをAjaxに登録したいと考えています。登録が成功すると、フォームを送信してください。失敗した場合は、フォームを提出しないようにします。
は、これは私のコードです:
--- HTML ---
<form id="account-form" method="post" enctype="multipart/form-data" data-ajax="false" action="upload.php" >
<input type="text" name="username" id="username" />
<input type="email" name="email" id="email" />
<input type="file" id="mediaupload" name="mediaupload" accept="image/*" />
</form>
--- JavaScript ---
accountform.submit(function(event) {
var registration_ok = true;
// See if we can register the user
// And if we can, submit the form.
$.ajax({
type: "POST",
url: "/usercreate.php",
data: postdata,
dataType: 'json',
success: function(data) {
// fine to submit the form.
},
error: function (data){
// If this fails, we don't want to submit the form!
registration_ok = false;
}
});
return registration_ok;
});
しかし、それは常にtrueを返します - 私は、主な機能はAjaxのエラーが呼び出される前を返すので、これがあると思います。
登録が失敗した場合、どのようにしてフォームが返されないようにするにはどうすればよいですか?
ありがとうございます!
ああ、私は参照してください - ありがとう。ニースの解決策:) – Richard