我遇到了AJAX响应和显示错误的问题。
例如,当我提交表单时,我会在控制台中看到1
,但是如果我在第一个输入中写了一些内容然后再次提交,则
我看到了:
1
2
下面的Ajax将1
和2
读作两个响应,所以我看到2个错误,但我应该只看到最新的错误,所以应该只有2
。
此外,当我尝试使用搜索(invite
)时我会有所收获,但是Ajax跳过了所有内容,并在提交后仅显示成功消息。
ajax.js
$(document).ready(function() {
$('#form_create_circle').submit(function(event){
event.preventDefault();
$.ajax({
url: 'form-create-circle.php',
type: 'POST',
data: $('#form_create_circle').serialize(),
dataType: 'json',
success: function(response) {
console.log(response);
if (response == 1) {
$('#title').addClass('is-invalid');
$('#invalid_title').append('<div class="invalid-feedback"><p>This field is required.</p></div>');
} else if (response == 2) {
$('#invite').addClass('is-invalid');
$('#invalid_invite').append('<div class="invalid-feedback"><p>This field is required.</p></div>');
} else if (response == 3) {
$('#color').addClass('is-invalid');
$('#invalid_color').append('<div class="invalid-feedback"><p>This field is required.</p></div>');
} else {
// success message
$('#_noti-container').append('<div class="noti noti-success noti-top-right noti-close-on-click noti-on-leave" style="z-index:100000"><div class="noti-content-wrapper"><div class="noti-content">Circle has been created!</div><div class="noti-close">×</div></div></div>');
}
}
});
return false;
});
});
form-create-circle.php
require_once($_SERVER['DOCUMENT_ROOT'].'/system/mysql/config.php');
$title = $db->EscapeString($_POST['title']);
$invite = $db->EscapeString($_POST['invite']);
$color = $db->EscapeString($_POST['color']);
$time = date('Y-m-d H:i:s');
$search = $db->QueryFetchArrayAll("SELECT * FROM user_about WHERE firstname LIKE '%".$invite."%' OR lastname LIKE '%".$invite."%'");
foreach ($search as $key) {
echo "
<div class='invite_search_cont'>
<div class='invite_search_img'><img src='{$key['profile_image']}'></img></div>
<div class='invite_search_name'>{$key['firstname']} {$key['lastname']}</div>
</div>
";
}
if ($title == '' || (!preg_match('/^[a-zA-Z0-9]+$/', $title))) {
echo 1;
} elseif ($search == '') {
echo 2;
} elseif ($color == '') {
echo 3;
} else {
$db->Query("INSERT INTO user_circle (user_id, user_added, title, color, time_added) VALUES ('{$user['id']}', '$invite', '$title', '$color', '$time')");
}
HTML
<form method='POST' id='form_create_circle'>
<div class='modal-body'>
<div>
<div class='form-group'>
<input type='text' name='title' id='title' placeholder='Family' class='form-control'>
<div id='invalid_title'></div>
</div>
<div class='form-group'>
<input type='text' name='invite' id='invite' placeholder='Search' class='form-control'>
<div id='invite_search_result'></div>
<div id='invalid_invite'></div>
</div>
<div class='form-group'>
<select name='color' id='color' class='form-control'>
<option value='0'>white</option>
<option value='1'>yellow</option>
<option value='2'>red</option>
</select>
<div id='invalid_color'></div>
</div>
</div>
</div>
<button type='submit' class='btn btn-primary' id='ajax_create_circle'>Submit</button>
</form>
<div id='_noti-container' class='noti-t-right'></div>
答案 0 :(得分:0)
对我来说,您只需要在脚本之前将using Foundation;
using System;
using UIKit;
using SQLite;
using System.Collections.Generic;
using System.IO;
namespace One
{
public partial class StudentDataBaViewController : UITableViewController
{
private string pathToDatabase;
private List<Student> students;
public StudentDataBaViewController (IntPtr handle) : base (handle)
{
students = new List<Student>();
}
//connect to student_da.db database file and create a table named Student
public override void ViewDidLoad()
{
base.ViewDidLoad();
//path of the database
var documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
pathToDatabase = Path.Combine(documentsFolder, "student_db.db");
//connect database and create table
using (var connection = new SQLite.SQLiteConnection(pathToDatabase))
{
connection.CreateTable<Student>();
}
}
//used to relaod or update new elements entered on the list
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
students = new List<Student>();
using (var connection = new SQLite.SQLiteConnection(pathToDatabase))
{
var query = connection.Table<Student>();
foreach (Student student in query)
{
students.Add(student);
TableView.ReloadData();
}
}
}
public override nint RowsInSection(UITableView tableView, nint section)
{
return students.Count;
}
//make elements to be display on the database list
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell("student");
var data = students[indexPath.Row];
cell.TextLabel.Text = data.StudentName;
cell.DetailTextLabel.Text = data.StudentId;
return cell;
}
public override bool CanEditRow(UITableView tableView, NSIndexPath indexPath)
{
return true;
}
}
public class Student
{
[PrimaryKey]
public string StudentId
{
get;
set;
}
public string StudentName
{
get;
set;
}
public string StudentPassword
{
get;
set;
}
public bool StudentAttendence
{
get;
set;
}
}
添加到脚本的开头或将$('#invalid_...').empty()
更改为.append
还可以在所有涉及的div上使用removeClass:
.html