$errMsgUsername="";
$errMsgPassword="";
if ($_POST['username']==""){
$errMsgUsername="Username is null";
}
if ($_POST['password']==""){
$errMsgPassword="Password is null";
}
echo json_encode(['errMsgUsername'=>$errMsgUsername,'errMsgPassword',=>$errMsgPassword]); // json response 1
$success = "";
if ($_POST['username']!="" && $_POST['password']!=""){
$success = "Form data available";
json_encode(['success'=>$success]); // json response 2
}
$.ajax({
url: $url,
type: $type,
data :formData,
cache: false,
dataType:"json",
contentType: false,
processData: false,
success: function(response){
//if fields null, the response has to display here.
$('.div_username').html(response.errMsgUsername);
$('.div_password').html(response.errMsgPassword);
//if fields are not null the response has to display here/
$('.div').html(response.success);
}
});
問題は、ユーザー名とパスワードがnullの間にフォームを送信すると、AjaxレスポンスがDivの期待どおりに表示されることです。 (ユーザー名とパスワードは、データとnullではない)しながら、私はフォームを送信する場合でも、AJAXの応答は、$(「DIV」)に表示されていない。HTML(response.success);. 私はそれは要求が成功したことを示しており、応答はまた戻って、このようなてきた開発ツールの下にAJAX要求をチェックしてください{「errMsgUsername」: 『『errMsgPassword」』:』 『}、{』成功 『:』フォームデータ利用可能 "}。Ajax経由で2つ以上のjson_encode phpエコーを取得するには?
ここでの問題は、ajaxレスポンスに2つの2つの json_encodeレスポンスがあることです。どのようにこの2つの応答をうまく管理するには? Jomyは、私はあなたのコードビットをクリーンアップとして、それを投稿し、あなたの答えの前に
? –
私は同じ反応を必要としません。どのようにそれを上記の状況に依存するようにするか? @NitinDhomse –
@JomyJosephあなたは私にそれを明確な視点で示すことができますか?答えとして? –