Ajax数据发布

时间:2012-12-10 20:18:14

标签: php javascript jquery html ajax

你好我试图实现一个ajax邀请脚本,让用户邀请他/她的朋友参加那个活动。我在网站的其他部分使用大致相同的javascript并且它们工作完美,但在这种情况下,它不起作用,我确信问题仍然存在因为javascript部分,因为正如我所说,我使用几乎完全的脚本,它工作完美,当我发布数据,它不发送电子邮件,我的邮件功能工作良好(在其他页面我使用相同没有ajax,它的工作原理)但我认为javascript部分可以'在这种情况下发布数据。

顺便说一下,在隐藏部分中获取值没有任何问题。

希望你能提供帮助。

javascript部分:

 <script type=\"text/javascript\">
 $(document).ready(function() {

$('.error').hide(); //Hide error messages 
$('#MainResult').hide(); //we will hide this right now

$(\"#button\").click(function() { //User clicks on Submit button


 var js_name = $(\"#name\").val();
 var js_message = $(\"#message\").val();
 var js_username = $(\"#username\").val();
 var js_useremail = $(\"#useremail\").val();
 var js_eventname = $(\"#eventname\").val();

 if(js_name==\"\"){
     $(\"#nameLb .error\").show(); // If Field is empty, we'll just show error text inside <span> tag.
     return false;}
if( js_message==\"\"){
     $(\"#messageLb .error\").show(); // If Field is empty, we'll just show error text inside <span> tag.
     return false;}


  var myData = 'postName='+ js_name + '&postMessage=' + js_message + '&username=' + js_username  + '&useremail=' + js_useremail  + '&eventname=' + js_eventname;

        jQuery.ajax({
            type: \"POST\",
            url: \"invite.php\",
            dataType:\"html\",
            data:myData,
            success:function(response){
                $(\"#MainResult\").html('<fieldset class=\"response\">'+response+'</fieldset>');
                $(\"#MainResult\").slideDown(\"slow\"); //show Result 
                $(\"#MainContent\").hide(); //hide form div slowly
            },
            error:function (xhr, ajaxOptions, thrownError){
                $(\"#ErrResults\").html(thrownError);
            }    
        });
    return false;
});


$(\"#gobacknow\").live(\"click\", function() { 
            $(\"#MainResult\").hide(); //show Result 
            $(\"#MainContent\").slideDown(\"slow\"); //hide form div slowly

            //clear all fields to empty state
            $(\"#name\").val('');$(\"#message\").val('');
});

$(\"#OpenContact\").live(\"click\", function() { 
            $(\"#form-wapper\").toggle(\"slow\");

});
 });
 </script>

html部分:

  <div id="form-wapper">
    <div id="form-inner">
    <div id="ErrResults"><!-- retrive Error Here --></div>
    <div id="MainResult"><!-- retrive response Here --></div>
    <div id="MainContent">
    <fieldset>
        <form id="MyContactForm" name="MyContactForm" method="post" action="">
        <label for="name" id="nameLb">Email : <span class="error" style="font-size:10px; color:red;">Error.</span></label>
        <input type="text" name="name" id="name" />
        <label for="message" name="messageLb" id="messageLb">Message : <span class="error" style="font-size:10px; color:red;">Error.</span></label><textarea style="resize:vertical;" name="message" id="message" ></textarea>
        <input type="hidden" name="username" id="username" value="<?php echo get_username($userid); ?>">
        <input type="hidden" name="useremail" id="useremail" value="<?php echo get_email($userid); ?>">
        <input type="hidden" name="eventname" id="eventname" value="<?php echo $eventname; ?>">
      <br><button id="button">Send</button>

        </form>
    </fieldset>
    </div>
    <div style="clear:both;"></div>
</div>

邀请php文件:

$postName       =  filter_var($_POST["postName"], FILTER_SANITIZE_STRING); 
$postMessage    = filter_var($_POST["postMessage"], FILTER_SANITIZE_STRING);
$username = filter_var($_POST["username"], FILTER_SANITIZE_STRING);
$useremail = filter_var($_POST["useremail"], FILTER_SANITIZE_STRING); 
$eventname= filter_var($_POST["eventname"], FILTER_SANITIZE_STRING);
invite($useremail, $postMessage , $username, $eventname, $postName); // this is a functipon that i use, it works in other cases, but not working in here

1 个答案:

答案 0 :(得分:0)

不是试图调试那个javascript,而是为javascript AJAX帖子做这个更容易/更简洁的方法:

$.post('invite.php',$('#MyContactForm').serialize(),function(data){
    if(data.success){
      // all your on success stuff here
        alert('success!');
    }else{
      // show error messages
        alert(data.e);
    }
},'json');

对于PHP部分,回显一个JSON响应数组,例如:

$data['success']=false;
$data['e']='Some error';
echo json_encode($data);