Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果数据库中已经存在值,如何显示错误消息?_Php_Jquery_Mysql_Ajax - Fatal编程技术网

Php 如果数据库中已经存在值,如何显示错误消息?

Php 如果数据库中已经存在值,如何显示错误消息?,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我有一个页面,用户可以通过ajax创建主题。在创建主题之前,我会检查它是否已经存在于数据库中。如果数据库中存在该主题,则在继续提交新主题之前,我希望通过div框显示错误消息。一切正常。但我不知道如何使错误消息仅在主题已存在于数据库中时显示 这是包含我的表单和ajax代码的页面: <script> function cancelTopicCreation() { $(".create-topic-cancel-button, .close_blue_modal"

我有一个页面,用户可以通过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-->

我怎样才能告诉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 { ....
在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);
}

这里有更多的评论,您的php在错误时回显了一个文本字符串
用户名已经存在
,但在底部也有
回显
a
json_encode()
。您的错误消息将错误地形成json。它会写
Username已经存在{“ajax”:“Hello world”,“advert”:“whatever”}
,如果出现错误则无效。好的,如果Username已经存在,我需要用js显示一个div。我该怎么做?我应该使用ajax返回变量还是json?我应该怎么做?@RomeoHennessy使用json只是因为你在jquery代码中循环了一个json。问题是我的json返回的是未定义的。我不知道如何正确使用JSON。@RomeoHennessy您将数据库数据存储在
$row
的哪里??你错过了。为什么我的javascript不能从php页面工作?它没有任何作用。我已经试过了…它不起作用“PHP。。。它是由它发送的。。。等一下,我将添加一些内容您必须将ajax结果放入
div
。如果是脚本,它将执行。把它藏起来。。。因此,如果它不是脚本,它将不会显示。好的。另一件事是,在ajax调用成功后,我关闭了允许人们提交新主题的框。但是,如果主题已经存在,那么我想保持该框打开,直到输入有效的主题。嗯。。。可以因此,明确地说,您需要询问Ajax用户名是否已经存在。看起来您只有2个可能的答案1-错误消息2-另一条消息。。。(你的Json东西我没看)。因此,在
成功
中,您可以检查结果的内容以做出决策
success: function(result) {
    // Rest of your code...
    // Add this for the script to be sent.
    $("#someEmptyDiv").html(result);
$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);
}
success: function(result) {
    $("#someEmptyDiv").html(result);
}