0

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

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

希望你能帮忙。

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
4

1 回答 1

0

Rather than trying to debug that javascript, here is a much much easier / cleaner way to do this for the javascript AJAX post:

$.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');

For your PHP part, echo a JSON response array, eg:

$data['success']=false;
$data['e']='Some error';
echo json_encode($data);
于 2012-12-10T20:40:09.690 回答