简体   繁体   English

将数据从PHP发送到jQuery

[英]Sending data from PHP to jQuery

I have build a simple but working AJAX form using jQuery. 我使用jQuery构建了一个简单但有效的AJAX表单。 The form is validated with jQuery before submit and then after successful submit again with PHP. 表单在提交之前使用jQuery进行验证,然后在使用PHP再次成功提交之后进行验证。

The only problem is the error handling. 唯一的问题是错误处理。 When the PHP-check returned that all fields are ok, I display a success message. 当PHP检查返回所有字段都没问题时,我显示一条成功消息。 If there was an error, I simply display "there was an error". 如果出现错误,我只是显示“有错误”。
How can I send back data from PHP to jQuery so invalid fields can be marked as invalid? 如何将数据从PHP发送回jQuery,以便将无效字段标记为无效?

Or is such form of error handling unnecessary as the dynamic highlighting needs JavaScript turned on and if JavaScript is turned on, the form can't be submitted wrong? 或者这种形式的错误处理是不必要的,因为动态突出显示需要启用JavaScript,如果启用JavaScript,表单不能提交错误?

Basically, my valid checks look like this (simplified of course): 基本上,我的有效检查看起来像这样(当然简化):

function checkSomething( $something ) {

        $valid = true;

        if ( strlen( $something ) === 0 ) {
            $valid = false;
        }


        return $valid;
} 

At the end of the script, I do it like this: 在脚本的最后,我这样做:

if ( checkSomething( $something ) && checkThis( $this ) && checkThat( $that ) ) {
    echo "success"; 
} else {
    echo "error";
}

And in my JavaScript/jQuery code it looks like this: 在我的JavaScript / jQuery代码中,它看起来像这样:

$.ajax({
    url: "form.php",
    type: "POST",
    data: string,
        success: function ( result ) {
            if ( result == "success" ) {
                alert( "success" );                     
            } else {
                alert( "error" );
            }
        }
});

I think you should have only one service in PHP. 我认为你应该只有一个PHP服务。 You send the data from the form, if there is any invalid data, you might want to return a JSON indicating which fields are wrong, so you can parse and show it. 您从表单发送数据,如果有任何无效数据,您可能希望返回一个JSON,指示哪些字段是错误的,因此您可以解析并显示它。 If it went ok, you just simply display that success message. 如果一切正常,您只需显示该成功消息即可。

I'm not sure I get your problem, though. 不过,我不确定我是否会遇到你的问题。

Your form submission has 3 steps: JavaScript Validation & Submission, PHP validation/Processing, and JavaScript on complete ( 'so invalid fields can be marked as invalid') 您的表单提交有3个步骤:JavaScript验证和提交,PHP验证/处理和JavaScript完成('因此无效字段可以标记为无效')

It sounds like you have step 1 completed and most of step 2. 听起来你已完成第1步,第2步的大部分时间。

In your PHP code you should have some data structure, preferably an array, which holds which values are invalid. 在PHP代码中,您应该有一些数据结构,最好是一个数组,它保存哪些值无效。 You can return this data to the browser by echo`ng it to the browser 您可以通过回显到浏览器将此数据返回给浏览器

<?php
...
...
if( count( $invalidFieldArray ) ){
    echo json_encode( array( 'status' => 'Fail',
                             'data' => $invalidFieldArray ) );
}else{
    echo json_encode( 'status' => 'Success' );
}

The code above will return the success/fail to your JavaScript form. 上面的代码会将成功/失败返回到您的JavaScript表单。

I personal use a jQuery form plugin to handle form submissions, and I recommend you give it a try. 我个人使用jQuery表单插件来处理表单提交,我建议你试一试。 It has many nice out of the box features. 它有许多开箱即用的功能。

$.ajax({
    url: "form.php",
    type: "POST",
    data: string,
    dataType: json,
        success: function ( result ) {
            if ( result.status === "Success" ) {
                alert( "success" );                     
            } elseif (result.status === 'Fail'){
                for (field in result.data) {
                         /* Invalid field handling */
                         ...
                } 
            }
        }
});

Tip : Slightly off-topic, but when using comparisons in JavaScript always use triple equal ( === ), double equal ( == ) is not the same in JavaScript as in other languages; 提示 :稍微偏离主题,但在JavaScript中使用比较时总是使用三等(===),双等(==)在JavaScript中与在其他语言中不一样; JavaScript does type coercion with double equals, which provides for inconsistent results. JavaScript使用double equals键入强制,这会产生不一致的结果。

可以从PHP发送数据并将其存储在URL中,例如url.com/#value=1

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

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