开发者

How to Inform user about ajax submission?

add.php - html markup.

dbadd.php - serverside script,

addpg.js - clientside including AJAX

RSV- form validator

I'm trying to do following: First validate the form (with RSV), if all things right, Then ajax submit (That's why i'm using myOnComplete). Inform user about submission. If user pressed for the first time save button then insert into db. Else update db.

The problems are:

  1. It inserts data into db table but doesn't inform about succes or error
  2. I can't figure out how to insert data into db If user pressed for the first time save button or update data.

Tried all possible ways. There is no error. Please anyone help me to fix that.

addpg.js

 function myOnComplete() {
    return true;
}


$(document).ready(function () {

    $("#add_form").RSV({
        onCompleteHandler: myOnComplete,
        rules: [
            "required,name,Name field required.",
            "required,title,Title field required.",
            "required,menu, Menu field required",
            "required,parentcheck,Parentcheck required",
            "if:parentcheck=1,required,parent,Parent required",
            "required,content,Page content field required"
            ]
    });

});


$("#submit_btn").click(function () {
    CKEDITOR.instances.content.updateElement();
    $("#add_form").submit(function (e) {
        e.preventDefault();
        dataString = $("#add_form").serialize();
        $.ajax({
            type: "POST",
            url: "processor/dbadd.php",
            data: dataString,
            dataType: "json",
            success: function (result, status, xResponse) {
                //do something if ajax call is success
                var message = xResponse.getResponseHeader("msg");
                var err = xResponse.getResponseHeader("err");
                if (message != null) {
                    //do what you like with the message
                }
                if (err != null) {
                    //do what you like with the erro
                }
            },
            error: function (e) {
                //ajax call failed
                alert(e);
            }
        });
    });
});

dbadd.php

<?php
require '../../core/includes/common.php';

$name=filter($_POST['name'], $db);
$title=filter($_POST['title'], $db);
$parentcheck=filter($_POST['parentcheck'],$db);
if(isset ($_POST['parent'])) $parent=filter($_POST['parent'],$db);
else $parent=$parentcheck;  
$menu=filter($_POST['menu'],开发者_如何学编程 $db);
$content = $db->escape_string($_POST['content']);

$result=$db->query("INSERT INTO menu (parent, name, showinmenu) VALUES ('$parent', '$name', '$menu')") or die($db->error);
$new_id = $db->insert_id;
$result2=$db->query("INSERT INTO pages (id, title, content) VALUES ('$new_id', '$title', '$content')") or die($db->error);  

if ($new_id>0){  
echo "{";  
echo '"msg": "All right" ';
echo "}";  
}else{ 
echo "{"; 
echo
'"err": "error"';  
echo "}";  
}

?>  

add.php

<div id="add">
<form id="add_form" method="" action=""> 
<input type="text" name="name" id="name" size="40"  value="" class="text-input" />  
<input type="text" name="title" id="title" size="40"  value="" class="text-input" />  
<select name="menu" id="menu">
<option value="" selected="selected">sample</option>
<option value="1">sample 1</option>
<option value="2">sample 2</option>
<option value="0">sample 3</option>
</select>
<input type="radio" class="parentcheck" name="parentcheck" value="0"/>
<input type="radio" class="parentcheck" name="parentcheck" value="1"/>
<select name="parent" id="parent"></select>

<textarea id="content" style="width:100%" name="content"></textarea>
<input type="submit" name="submit" class="button" id="submit_btn" value="Save" />
</form>
</div>
<script type="text/javascript" src="../../core/includes/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="../../core/scripts/admin/addpg.js"></script>
<script type="text/javascript" src="../../core/scripts/admin/rsv.js"></script>


For the first problem:

Without actually running the code or seeing a live example, I can't say for sure, but it looks like you have the right idea and it's just a syntax/usage error. For example:

var message = xResponse.getResponseHeader("msg");
var err = xResponse.getResponseHeader("err");

Someone please scold me if I'm wrong, but aren't "msg" and "err" found in the JSON (result) rather than in xResponse? result.msg (or result["msg"]) and result.err (or result["err"])

If so, also be aware that I -believe- you will get an 'undefined' error when trying to declare both of those variables since only one of them will be present. You might want to wrap them in try/catch blocks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜