简体   繁体   English

我怎样才能弹出一个jQuery CSS容器,让用户知道数据已成功输入?

[英]How can I get a jquery css container to pop up letting the user know data was successfully entered?

When a user inserts data into a form and then submits it, it goes to my php script and inserts the data. 当用户将数据插入表单然后提交时,它将转到我的PHP脚本并插入数据。 The script will return a value letting the user know if the data was inserted or not. 该脚本将返回一个值,让用户知道是否插入了数据。 Since I am using JQuery, I have no idea how to do this. 由于我正在使用JQuery,因此我不知道如何执行此操作。 Can someone please point me in the right direction? 有人可以指出正确的方向吗? All I'm after here is a nice, neat little 200px by 25px container that will fade in and the user MUST click it to acknowledge it. 我只想看到一个200像素乘25像素的漂亮,整洁的容器,该容器将逐渐消失,用户必须单击它以确认它。

Now that I think about it, since users are not the brightest lights in the harbor, it would be really cool if I could not only show then this css box letting them know if the insert was successful but also show them the customer that was inserted in case they forgot where they left off. 现在,我考虑了一下,因为用户并不是港口中最亮的灯,所以如果我不仅可以显示此css框,让他们知道插入是否成功,还可以向他们显示插入的客户,那真的很酷万一他们忘记了他们离开的地方。

Thanks. 谢谢。

I'm not going to mark this as a duplicate, but you are essentially asking the best way to show notifications using jQuery. 我不会将其标记为重复项,但是您实质上是在询问使用jQuery显示通知的最佳方法。 And that has been asked before . 这是以前问过的 In a nutshell, in your basic setup all you need to do is show a div and fade it in, but there are a lot of plugins out there that handle more situations. 简而言之,在基本设置中,您所需要做的就是显示一个div并将其淡入,但是那里有很多可以处理更多情况的插件。

As far as showing them the customer that was inserted or whatever, all you have to do is return a JSON response from the server and handle it with the success callback of your AJAX request. 只要向他们显示插入的客户或其他对象,您要做的就是从服务器返回JSON响应,并使用AJAX请求的成功回调来处理它。 You will have access to the data passed back from the server then. 然后,您将有权访问从服务器传回的数据。

To explain a little further: What you seem to be asking to do is how to send a request to a server using Ajax, get a notification that everything went as expected, and then show a notification in the webpage depending on what happened. 进一步解释一下:您似乎想做的是如何使用Ajax向服务器发送请求,如何获得一切正常的通知,然后根据发生的情况在网页中显示通知。 If that is correct, I'm going to move on with these assumptions. 如果是正确的话,我将继续这些假设。

Let's put together a simple form to add a new customer to a fictional website. 让我们放一个简单的表单,将一个新客户添加到一个虚构的网站。 It might look like this: 它可能看起来像这样:

<form action='add_customer.php' method='POST' id='add_customer_form'>
    First Name: <input type='text' name='first_name'><br>
    Last Name: <input type='text' name='last_name'><br>
    Email: <input type='text' name='email'><br>
    <input type='submit' value='Add Customer'>
</form>

Now, our naive, insecure PHP code to handle this form submission (through AJAX) might look something like this. 现在,我们处理这些表单提交(通过AJAX)的幼稚,不安全的 PHP代码可能看起来像这样。 I say naive and insecure because you would do more data validation (as you never trust anything coming from the client) and would certainly need to clean the data to prevent SQL injections. 我之所以说天真和不安全,是因为您将进行更多的数据验证(因为您永远不信任来自客户端的任何信息),并且肯定需要清理数据以防止SQL注入。 This is an example, however, so it might look like this: 但是,这是一个示例,因此可能如下所示:

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
if(mysql_query("
    INSERT INTO customers
    (first_name, last_name, email)
    VALUES 
    ('$first_name','$last_name','$email')
   ")) {
    $success = 1;
    $message = 'Customer ' . $first_name . ' successfully added!';
} else {
    $success = 0;
    $message = 'Something went wrong while adding the customer!';
}
print json_encode(array('success' => $success, 'message' => $message));

We might then handle the sending of this form and the receiving of data from the server, using jQuery, with code that looks a little something like this: 然后,我们可能会使用jQuery处理这种形式的发送和从服务器接收数据,其代码看起来像这样:

$(function() {
    $('#add_customer_form').submit(function() { // handle form submit
        var data = $(this).serialize(); // get form's data
        var url = $(this).attr('action'); // get form's action
        var method = $(this).attr('method'); // get form's method
        $.ajax({
            url: url, // where is this being sent to?
            type: method, // we're performing a POST
            data: data, // we're sending the form data
            dataType: 'json', // what will the server send back?
            success: function(data) { // the request succeeded, now what?
                // here data is a JSON object we received from the server
                // data.success will be 0 if something went wrong
                // and 1 if everything went well. data.message will have
                // the message we want to display to the user
                // at this point you would use one of the techniques I linked
                // to in order to display a fancy notification

                // the line below will dynamically create a new div element,
                // give it an ID of "message" (presumably for CSS purposes)
                // and insert the message returned by the server inside of it
                var $div = $('<div>').attr('id', 'message').html(data.message);

                if(data.success == 0) {
                    $div.addClass('error'); // to personalize the type of error
                } else {
                    $div.addClass('success'); // to personalize the type of error
                }
                $('body').append($div); // add the div to the document
            }
        });
        return false; // prevent non-ajax form submission.
    });
});

I haven't tested any of this code but the idea is there. 我没有测试过任何这些代码,但是想法就在那里。 You return a format, JSON, from the server, with data, and handle the success callback jQuery fires when the request is complete, and add an element to your document you can style however you want (and show with things like fadeIn() if you want) with a message describing what happened. 您从服务器返回带有数据的JSON格式,并处理成功请求完成后jQuery触发的成功回调,并向您的文档中添加一个元素,您可以随意设置样式(并显示诸如fadeIn()这样的内容您想要),并附有描述发生了什么情况的消息。 You might also need to catch the error callback (which fires if your server doesn't respond for whatever reason) and firing off another alert in that situation. 您可能还需要捕获错误回调(如果服务器由于某种原因没有响应,则将触发该回调)并在这种情况下触发另一个警报。

Fading effects can be accomplished by doing one of the following: 淡出效果可以通过执行以下任一操作来实现:

jQuery(myDiv).fadeIn()
jQuery(myDiv).fadeOut()
jQuery(myDiv).fadeTo()

I'd recommend you first create this css box - create the html and the css. 我建议您首先创建此CSS框-创建HTML和CSS。 Then you can experiment with fading - jQuery lets you specify arguments like fade color, fade duration, etc. 然后您可以尝试淡入淡出-jQuery使您可以指定诸如淡入淡出颜色,淡入淡出持续时间等参数。

Here is something to work from... 这是一些工作...

Your PHP script outputs this depending on results 您的PHP脚本根据结果输出此内容

$json = array(
    'success' => $insertSuccess,
    'message' => $numRecords . ' records inserted in ' . $insertTime . 's'
);

echo json_encode($json);

Then, in the callback of your JSON, you determine from parsing the JSON object if the insert was successful or not. 然后,在JSON的回调中,通过解析JSON对象来确定插入是否成功。 Your callback might look something like this 您的回调可能看起来像这样

function(data) {

    success = data.insertedSuccess;

    if (success) {
         $('#success').fadeIn(1000);
    } else {
         $('#failure').fadeIn(1000);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM