我的问题是如何使用JSON发布多个输入并获取每个输入的响应。目前,我正在使用AJAX发布我的整个表单数据。但是正如预期的那样,我的PHP仅返回一个响应。我需要的是在客户端按输入ID获取响应,以便我可以正确地显示结果。
前侧
<form action="javascript:void(0);">
<div>
<label>
<span>Item 1</span>
<input type="text" name="TestItem1">
</label>
<!-- I WANT TO SHOW TEST RESULT OF TESTITEM1 HERE -->
</div>
<div>
<label>
<span>Item 2</span>
<input type="text" name="TestItem2">
</label>
<!-- I WANT TO SHOW TEST RESULT OF TESTITEM2 HERE -->
</div>
<div>
<button type="submit">Check</button>
</div>
</form>
<div id="result"></div><!-- SO I WON'T NEED THIS RESULT PART ANYMORE -->
AJAX
$(document).ready(function(){
$('form').submit( postgonder )
});
function postgonder()
{
$.post('result.php', $('form').serialize(),function(veri){$('#result').html(veri)});
}
服务器端
$testitem1=$_POST['TestItem1'];
$testitem2=$_POST['TestItem2'];
$test1 = (some tests of $testitem1);
if(empty($testitem1)) {
echo "-";
}
else if($test1) {
echo "successful";
}
else {
echo "failed";
}
$test2 = (some tests of $testitem2);
if(empty($testitem2)) {
echo "-";
}
else if($test2) {
echo "successful";
}
else {
echo "failed";
}