我尝试了一些ajax脚本没有成功,当我提交表单时,我采取成功警报,但没有发送到数据库

时间:2014-01-19 13:41:15

标签: javascript php jquery ajax forms

我尝试了一些ajax脚本没有成功,当我提交表单时,我采取了成功警报,但没有发送到数据库。

我的HTML表单:

<form action="account_info.php" enctype="multipart/form-data" method="post">
<input id="email" name="email" type="text" value="Save"/>
<input id="username" name="username" type="text" value="Save"/>
<input type="submit" name="Submit" value="Save"/>
</form>

我的PHP代码:

$error='';
$info='';
if(isset($_POST['Submit'])) {

    require_once "classes/fields_process.php";
    require_once "classes/blocked_emails.php";

    if(!$usr->edit_info($crt_usr)) { 
        $usr_info=$usr->getTmp();
        $error=$usr->getError();
    } else { 
        $info=$usr->getInfo();
        $usr_info = $usr->getUser($crt_usr);
    }
} else $usr_info = $usr->getUser($crt_usr);

$smarty->assign("tmp",$usr_info);
$smarty->assign("error",$error);
$smarty->assign("info",$info);

2 个答案:

答案 0 :(得分:0)

在表单中添加ID:“accountForm”

<form action="account_info.php" enctype="multipart/form-data" method="post" **id="accountForm"**>
<input id="email" name="email" type="text" value="Save"/>
<input id="username" name="username" type="text" value="Save"/>
<input type="submit" name="Submit" value="Save"/>
</form>

javascript:

$.customPOST = function(data,callback){
    $.post('account_info.php',data,callback,'json');
}
$('#accountForm').submit(function(){
  $.customPOST($(this).serialize(),function(r){
   //the response from the server in JSON format
   //an array, example in the PHP script we return
   //an array that contains : 'success' => true
   if(r.sucess){
     alert('Operation OK');
   }
  });
  return false;
});

但是在你的PHP中,你需要以JSON格式回应响应:

echo json_encode(array('success' => true));

答案 1 :(得分:0)

好的,我解决了这个问题。

<form name="accountform" id="accountform" method="post" action="account.php">
<input id="email" name="email" type="text" value="Save"/>
<input id="username" name="username" type="text" value="Save"/>
<input type="submit" name="Submit" value="Save"/>
</form>



{literal}<script> 
    // wait for the DOM to be loaded 
    $(document).ready(function() { 
        // bind 'myForm' and provide a simple callback function 
        $('#accountform').ajaxForm(function() { 
            alert("Thank you for your comment!"); 
        }); 
    }); 
  </script>{/literal}