[英]Prevent an empty form input from submitting an empty value to database after removing it using a jquery and ajax
美好的一天,我一直在這個網站上閱讀所有可能的問題和答案,我知道我幾乎得到了正確的答案,但似乎這里的一些建議對我不起作用。 我有一個動態表單,用戶可以添加和刪除文本字段,並通過ajax和php提交請求。 該表單包含兩個必需的文本字段和按鈕,用於添加和刪除另一個字段(除了兩個必填字段)。 即使不使用其他額外字段,用戶也可以提交表單。
我的問題是,如果我按下添加按鈕,稍后決定刪除它,即使按下刪除按鈕后,我在數據庫中的相應表中得到一個'0'值。
這是我的HTML:
<form method="POST">
<span class="text-label">Subject:</span>
<input type="text" name="subject" id="subject-field" placeholder="Subject name here" maxlength="10" class="record-input-forms" /> <span class="text-label">Section:</span>
<input type="text" name="section" id="section-field" placeholder="Subject section here" maxlength="3" class="record-input-forms" /> <a href="#" class="add-field" title="Add student field">+</a> <a class="remove-field" href="#" title="Remove student field">×</a> <a href="#" id="save-button" title="Save">→</a>
<div id="student-box-wrap"></div> <span id="status-message"></span> </form>
這是我的AJAX
$(document).ready(function() {
$("#save-button").click(function() {
var subject = $("input#subject-field").val();
if (subject == "") {
$('#status-message').css({
"color": "#ec3f8c"
});
$('#status-message').html('Please fill the subject fields');
return false;
}
var section = $("input#section-field").val();
if (section == "") {
$('#status-message').css({
"color": "#ec3f8c"
});
$('#status-message').html('Please fill the section fields');
return false;
}
var studid = [];
$('input[name="studid[]"]').map(function() {
studid.push($(this).val());
});
var dataString = 'subject=' + subject + '§ion=' + section + '&studid=' + studid;
$.ajax({
type: "POST",
url: 'save.php',
data: dataString,
dataType: "html",
success: function(data) {
$("input#subject-field").val('');
$("input#section-field").val('');
$("input#field-wrap").remove();
$("#status-message").css({
"color": "#39b1c6"
});
$("#status-message").html('Save successfully');
$("#status-message").fadeOut(2000);
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError);
}
});
return false;
});
});
我的Jquery櫃台
$(document).ready(function() {
var counter = 0;
$(".add-field").click(function() {
counter += 1;
$("#student-box-wrap").append('<div class="field-wrap-' + counter + '"><span id="number-' + counter + '">' + counter + '.</span> Student ID: <input type="text" name="studid[]" class="record-input-forms" /></div>');
});
$(".remove-field").click(function() {
if (counter == 0) {
alert("Nothing to remove!");
} else {
$(".field-wrap-" + counter + "").remove();
counter--;
}
});
});
還有我的PHP
<?php
require 'connection.php';
session_start();
$studid = (explode(",", $_POST['studid']));
$subject = mysql_real_escape_string(strtoupper($_POST['subject']));
$section = mysql_real_escape_string(strtoupper($_POST['section']));
$adminid = $_SESSION['AdminID'];
mysqli_query($con, "INSERT INTO tbl_subjects(SubjectName, SubjectSection, AdminID) VALUES ('$subject', '$section', '$adminid')");
if (!empty($studid) && !empty($name)) {
foreach ($studid as $new) {
$sql_1 = "INSERT INTO tbl_student(StudentID, SubjectID) VALUES ('$new', LAST_INSERT_ID())";
mysqli_query($con, $sql_1);
}
}
if (!mysqli_query($con, $sql)) {
die('Error: ' . mysqli_error($con));
}
?>
我用過!在我的PHP中空,我得到相同的結果。 如果我根本不按添加按鈕,我沒有任何問題。 它只是在按下它時,甚至在刪除它之后,ajax中的變量似乎攜帶一個空數據到數據庫。
我認為你的問題是,在你的PHP中,你調用$studid = (explode(",", $_POST['studid']));
在檢查是否設置了值之前。
如果分隔符是空字符串(“”),則explode()將返回FALSE。 如果分隔符包含未包含在字符串中的值並且使用了負限制,則將返回空數組, 否則將返回包含字符串的數組 。
實際上,您在空字符串上調用explode()
並返回空字符串。
在PHP
,嘗試在if語句中移動explode()
,然后檢查它是否設置為:
<?php
require 'connection.php';
session_start();
$subject = mysql_real_escape_string(strtoupper($_POST['subject']));
$section = mysql_real_escape_string(strtoupper($_POST['section']));
$adminid = $_SESSION['AdminID'];
mysqli_query($con, "INSERT INTO tbl_subjects(SubjectName, SubjectSection, AdminID) VALUES ('$subject', '$section', '$adminid')");
if ( isset( $_POST['studid'] )) {
$studid = (explode(",", $_POST['studid']));
foreach ($studid as $new) {
$sql_1 = "INSERT INTO tbl_student(StudentID, SubjectID) VALUES ('$new', LAST_INSERT_ID())";
mysqli_query($con, $sql_1);
}
}
if (!mysqli_query($con, $sql)) {
die('Error: ' . mysqli_error($con));
}
?>
另外,在你的jquery中,更改:
var dataString = 'subject=' + subject + '§ion=' + section + '§ion=' + section;
至:
// only add `studid` if there is one or more present
var studidStr = studid != '' ? '&studid=' + studid : '';
var dataString = 'subject=' + subject + '§ion=' + section + studidStr;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.