我正在创建一个基本网站来创建艺术家并将歌曲上传给艺术家以学习mysql,php,js。
我在使用JavaScript时遇到问题,检查您是否为艺术家输入了名称并选择了图片。或者,如果用户正在更新现有艺术家。 (我有PHP代码,当JS失败时可以工作)。
这里是JS代码:
function checkFormDetails(form, formEdit) {
//set filename to lowercase
var fileName = form.filePictureFileName.value.toLowerCase();
//Check if user is updating a song
if (form.txtArtist.value == formEdit.hidArtist.value && fileName == "") {
return true;
}
//Checks if Artist-field is empty
else if (form.txtArtist.value == "") {
window.alert("Please enter a Artistname!");
form.txtArtist.focus();
return false;
}
//Checks if a picture is choosen
else if (fileName == "") {
window.alert("Please choose a Picture!");
form.filePictureFileName.focus();
return false;
}
//Checks if the file extension is correct
else if (fileName.lastIndexOf(".jpg") == -1) {
window.alert("Only '.jpg' files are allowed.");
return false;
} else
return true;
}
//Copys the artist data from the hidden fields
function copyEditArtistFormData(formDel, formUpd) {
formUpd.txtArtist.value = formDel.hidArtist.value;
formUpd.hidId.value = formDel.hidId.value;
formUpd.hidPictureFileName.value = formDel.hidPictureFileName.value;
}
//Confirmation for deleting a Artist
function verifyDeleteOfArtist(formDel) {
var x = window.confirm('Do you want to delete the artist?');
if (x) {
return true;
} else {
return false;
}
}
//Resets the fiels to default
function resetNewUpdateArtistFormData(form) {
form.txtArtist.value = "";
form.filePictureFileName.value = null;
form.hidId.value = "";
form.hidPictureFileName.value = "";
}
在其当前状态下,编辑功能起作用。但是,如果我想创建一个新的艺术家并且不输入名称和/或选择要上传的图片,我将无法获得一个窗口。
如果我移动if语句,检查用户是否正在将艺术家更新到函数的末尾。如果未能输入艺术家姓名或图片,我会获得window.alerts。但是当我想编辑现有的艺术家时,艺术家名称会被移动到表单,但图片文件不会移动到表单,所以我收到一个错误,告诉我选择一个文件。
这是用于保存和编辑艺术家的两个php函数:
function printRecord() {
echo("<fieldset><legend>New/Edit Artist</legend>\n<form action=\"adminArtist.php\" method=\"post\"
name = \"frmNewUpdateArtist\" enctype=\"multipart/form-data\" onsubmit=\"return checkFormDetails(this, frmEditDeleteArtist);\">\n");
echo("<input type=\"hidden\" id=\"hidId\" name=\"hidId\" />\n");
echo("<input type=\"hidden\" id=\"hidPictureFileName\" name=\"hidPictureFileName\" />\n");
echo("<label>Artist<br /><input type=\"text\" id=\"txtArtist\" name=\"txtArtist\" autofocus=\"autofocus\"
title = \"Artist\"/></label><br />\n");
echo("<label>Picture<br /><input type=\"file\" id=\"filePictureFileName\" name=\"filePictureFileName\"
title = \"Picture\" /></label><br />\n");
echo("<input type=\"submit\" id=\"btnSave\" name=\"btnSave\" value=\"Save\" />\n");
echo("<input type=\"button\" id=\"btnReset\" name=\"btnReset\" value=\"Reset\"
onclick = \"resetNewUpdateArtistFormData(this.form);\"/>\n");
echo("</form>\n</fieldset>\n");
}
function listArtists($dbConnection) {
$strSQL = "SELECT * FROM tblartist";
$recordSet = myDBQuery($dbConnection, $strSQL);
echo("<fieldset>\n<legend>Stored Artists</legend>\n");
while ($record = mysqli_fetch_assoc($recordSet)) {
echo("<form action=\"adminArtist.php\" method=\"post\" name=\"frmEditDeleteArtist\" onsubmit=\"return verifyDeleteOfArtist(this);\">\n");
echo("id: ".$record["id"].
"<br />\n");
echo("name: ".$record["name"].
"<br />\n");
echo("picture: ".$record["picture"].
"<br />\n");
echo("changedate: ".$record["changedate"].
"<br />\n");
$filePathAndName = "upload_jpg/".$record["picture"];
echo("<img src=\"".$filePathAndName.
"\" alt=\"".$record["picture"].
"\" class=\"imgAnimation\" /><br />\n");
echo("<input type=\"hidden\" name=\"hidId\" value=\"".$record["id"].
"\" />\n");
echo("<input type=\"hidden\" name=\"hidArtist\" value=\"".$record["name"].
"\" />\n");
echo("<input type=\"hidden\" name=\"hidPictureFileName\" value=\"".$record["picture"].
"\" />\n");
echo("<input type=\"button\" name=\"btnEdit\" value=\"Edit\" onclick=\"copyEditArtistFormData(this.form, frmNewUpdateArtist);\"/>\n");
echo("<input type=\"submit\" name=\"btnDelete\" value=\"Delete\" />\n");
echo("</form>\n\n");
}
echo("</fieldset>\n");
myDBFreeResult($recordSet);
}
我想这是一个非常基本的问题,但我不能因为上帝之爱找出问题所在。如果我的代码看起来对你更有经验的程序员来说很糟糕,我也很抱歉。
我很感谢有关如何解决此问题的任何想法。
答案 0 :(得分:0)
&#39;其他&#39;声明不应该全部进入&#39;如果有的话。您不想只检查艺术家是否为空,或者文件扩展名是否错误,您想要检查所有这些,所以只需使用:
function checkFormDetails(form, formEdit){
var result = true;
//set filename to lowercase
var fileName = form.filePictureFileName.value.toLowerCase();
//Check if user is updating a song
if (form.txtArtist.value != formEdit.hidArtist.value || fileName != "")
{
result = false;
}
//Checks if Artist-field is empty
if(form.txtArtist.value == "")
{
window.alert("Please enter a Artistname!");
form.txtArtist.focus();
result = false;
}
//Checks if a picture is choosen
if(fileName == "")
{
window.alert("Please choose a Picture!");
form.filePictureFileName.focus();
result = false;
}
//Checks if the file extension is correct
if(fileName.lastIndexOf(".jpg") == -1)
{
window.alert("Only '.jpg' files are allowed.");
result = false;
}
return result;
}