gpt4 book ai didi

php - 如果数据库中已存在某个值,如何显示错误消息?

转载 作者:行者123 更新时间:2023-11-29 02:14:12 25 4
gpt4 key购买 nike

我有一个页面,用户可以在其中通过 ajax 创建主题。在创建主题之前,我会检查它是否已存在于数据库中。如果该主题存在于数据库中,那么我想在继续提交新主题之前通过 div 框显示一条错误消息。一切正常。但是我不知道如何让错误消息仅在主题已存在于数据库中时出现。

这是包含我的表单和 ajax 代码的页面:

<script>
function cancelTopicCreation() {
$(".create-topic-cancel-button, .close_blue_modal").click(function () {
$('#id-create-topic-picture-preview').attr('src','<?php echo BASE_URL; ?>content/pictures/layout/default_group_icon.png');
});
}

function close_blue_modal() {
$('#blue_modal_mask').fadeOut(144);
$('.blue_modal_window').fadeOut(144);
$('.blue_modal_window_two').fadeOut(144);
$('.blue_modal_window_three').fadeOut(144);
$('.blue_modal_window_five').fadeOut(144);
}
function createTopic() {
/*----------------submit title of new topic----------------*/
$(document).ready(function() {
var topicTitle = $(".create-topic-input-box").val();/* get the value of the topic title input and put it in a variable */
var dataString = 'topicTitle='+ topicTitle;/* returns successful data submission message when the entered information is stored in database. */
if(topicTitle == '') {/* if there is no title entered then */
alert("Please Fill All Fields");
} else {
$.ajax({
type: "POST",
url: site_url+'resources/create-topic.php',
data: dataString,
cache: false,
success: function(result) {/* if the ajax submit is successful then */
$('#createTopicForm').each(function() {
this.reset();
});
close_blue_modal();
$('#id-create-topic-picture-preview').attr('src','<?php echo BASE_URL; ?>content/pictures/layout/default_group_icon.png');
$("#results").html(''); //append data into #results element
load_contents(track_page); //initial content load
}
});
}
return false;
});
/*----------------upload topic picture----------------*/
$(document).ready(function() {
var input = document.getElementById("id-create-topic-picture-input");
file = input.files[0];
if (file != undefined){
formData= new FormData();
if(!!file.type.match(/image.*/)){
formData.append("nameTopicPicture", file);
$.ajax({
url: site_url+'resources/upload-topic-icon.php',
type: "POST",
data: formData,
processData: false,
contentType: false,
success: function(data) {/* you can do something if the picture upload is successful here */}
});
} else {
alert('Not a valid image!');
}
} else{/* if there is no image selected then */}
});
}
</script>
<div id="blue_modal_mask" class="close_blue_modal"></div><!--#blue_modal_mask .close_blue_modal-->
<div id="blue_modal_four" class="blue_modal_window_two">
<div onclick="return cancelTopicCreation();" class="close_blue_modal icon-x-mark"></div><!--.close_white_modal-->
<div class="create-topic-label">Create topic</div><!--.create-topic-label-->
<div class="create-topic-error-message">That topic already exists. Please choose a different topic name.</div><!--.create-topic-label-->
<div class="create-topic-description"><div class="icon-book create-topic-description-icon"></div>Topics are used to organize content on the website.</div><!--.create-topic-label-->
<form id="createTopicForm" action="<?php echo BASE_URL?>resources/upload_topic_icon.php" method="post" enctype="multipart/form-data">
<div class="create-topic-input-wrap">
<label for='id-create-topic-input-box'><div class='create-topic-name-label'>Topic name</div></label>
<input class="create-topic-input-box" id="id-create-topic-input-box" type="text" name="topicTitle" placeholder="Enter a topic name" autocomplete="off"><!--input.index_sign_up_input-->
<div class="create-topic-autocomplete-result-box">
<div class="create-topic-autocomplete-result-list nice-scroll">
<div class="create-topic-autocomplete-result-item">
<img class="create-topic-autocomplete-result-item-picture" src="content/pictures/layout/default_group_icon.png" alt="">
<div class="create-topic-autocomplete-result-item-content">
<div class="create-topic-autocomplete-result-item-title">The title of the result goes here</div>
<div class="create-topic-autocomplete-result-item-details">The details of the result goes here</div>
</div><!--create-topic-autocomplete-result-item-content-->
</div><!--create-topic-autocomplete-result-item-->
<div class="create-topic-autocomplete-result-item">
<img class="create-topic-autocomplete-result-item-picture" src="content/pictures/layout/default_group_icon.png" alt="">
<div class="create-topic-autocomplete-result-item-content">
<div class="create-topic-autocomplete-result-item-title">The name of the result goes here</div>
<div class="create-topic-autocomplete-result-item-details">The details of the result goes here</div>
</div><!--create-topic-autocomplete-result-item-content-->
</div><!--create-topic-autocomplete-result-item-->
<div class="create-topic-autocomplete-result-item">
<img class="create-topic-autocomplete-result-item-picture" src="content/pictures/layout/default_group_icon.png" alt="">
<div class="create-topic-autocomplete-result-item-content">
<div class="create-topic-autocomplete-result-item-title">The name of the result goes here</div>
<div class="create-topic-autocomplete-result-item-details">The details of the result goes here</div>
</div><!--create-topic-autocomplete-result-item-content-->
</div><!--create-topic-autocomplete-result-item-->
<div class="create-topic-autocomplete-result-item-empty">Nothing found</div>
</div><!--create-topic-autocomplete-result-list-->
</div><!--create-topic-autocomplete-result-box-->
</div>
<div class="create-topic-picture-preview-wrap">
<img id="id-create-topic-picture-preview" src="<?php echo BASE_URL; ?>content/pictures/layout/default_group_icon.png" class="create-topic-picture-preview">
</div>
<div id="create-topic-footer">
<label for="id-create-topic-picture-input" >
<div class="icon-camera create-topic-camera-icon"></div>
</label>
<input name="nameTopicPicture" accept="image/*" type="file" class="create-topic-picture-input" id="id-create-topic-picture-input">
<a onclick="return createTopic();" class="create-topic-button">Create</a>
<a onclick="return cancelTopicCreation();" class="create-topic-cancel-button cancel_blue_modal">Cancel</a>
</div><!--#new_post_options-->
</form>
</div><!--.blue_modal_one .blue_modal_window-->

这是 ajax 引用的我的 php 页面:

session_start();/* start new or resume existing session */
require_once('Wall.php');
$Wall = new Wall;/* create a new object for the class */
$languageFile = $Wall->getTheLanguageFileForFilesInTheResourcesFolder();/* get the language file and put it in a variable */
include_once ($languageFile);
$idOfLoggedInUser = isset($_SESSION['mem_id']) ? intval($_SESSION['mem_id']) : "";/* get the id of the logged in user and put it in a variable */
$topicTitle = $_POST['topicTitle'];/* get the entered topic title and put it in a variable */
$query = mysqli_query($databaseConnection, "SELECT columnTopicTitle FROM tableTopics WHERE columnTopicTitle='".$topicTitle."'");
if (mysqli_num_rows($query) != 0) {
echo "Username already exists";
} else {
$idOfLastTopicCreated = $Wall->createTopic($idOfLoggedInUser, $topicTitle);/* get the language file and put it in a variable */
$_SESSION['idOfLastTopicCreated'] = $idOfLastTopicCreated;
}

$advert = array(
'ajax' => 'Hello world!',
'advert' => $row['adverts'],
);
echo json_encode($advert);
?>

我如何告诉 javascript 该名称的主题已经存在。如果该主题已存在于数据库中,那么我希望 ajax 停止并显示错误弹出 div。请帮忙。我曾尝试使用 json 传递一个字符串,表明该主题已存在于数据库中。但是没有成功。我曾尝试将字符串从 php 传递到查询,但问题是该字符串在多次尝试时不会更改。所以它每次都显示错误。

最佳答案

您可以让 PHP 发送脚本而不是简单的字符串...

这样的东西也可以:

$query = mysqli_query($databaseConnection, "SELECT columnTopicTitle FROM tableTopics WHERE columnTopicTitle='".$topicTitle."'");
if (mysqli_num_rows($query) != 0) {
//echo "Username already exists";
?>
<script>
alert("Username already exists");
</script>
<?php
} else { ....

所以你可以在你的主页中有一个隐藏的 div,用 CSS 设置样式......只是为了以比警报更可爱的方式显示任何消息:

$query = mysqli_query($databaseConnection, "SELECT columnTopicTitle FROM tableTopics WHERE columnTopicTitle='".$topicTitle."'");
if (mysqli_num_rows($query) != 0) {
//echo "Username already exists";
?>
<script>
$("#myHiddenDiv").html("Username already exists").fadeIn(600);
</script>
<?php
} else { ....

在您的 Ajax success 回调中:

success: function(result) {
// Rest of your code...
// Add this for the script to be sent.
$("#someEmptyDiv").html(result);




------编辑

好吧...你必须得到可能的结果:
1- 关于用户名的错误信息已经存在
2- 一个 JSON

这里有很多发挥创意的方法,具体取决于您想要做什么:

$query = mysqli_query($databaseConnection, "SELECT columnTopicTitle FROM tableTopics WHERE columnTopicTitle='".$topicTitle."'");
if (mysqli_num_rows($query) != 0) {
//echo "Username already exists";
?>
<script>
$("#myHiddenDiv").html("Username already exists").fadeIn(600);
</script>
<?php
} else {
$advert = array( // I assume this works.
'ajax' => 'Hello world!',
'advert' => $row['adverts'],
);
?>
<script>
close_blue_modal();
$("#myHiddenDiv").html("Username created").fadeIn(600);
console.log( " <?php echo json_encode($advert); ?> " );
</script>
<?php
//echo json_encode($advert);
}

那么 Ajax 成功将是:

success: function(result) {
$("#someEmptyDiv").html(result);
}

关于php - 如果数据库中已存在某个值,如何显示错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43024472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com