dpge74512 2013-09-10 14:44
浏览 34
已采纳

Ajax PHP脚本无法正常工作 - 使用php ajax联系方框

Hey guys I'm trying to make a contact form where people can contact me.

What I want want is something that people could fill, and if they click "Send", they get a message like "Mail has been sent", so that they don't get redirected to the PHP site.

Unfortunately, it's not working for me. If I click on "Send", it shows me the script. So I'm not sure if the AJAX code is correct. Could anyone help me?

$(document).ready(function()    {
        $("#kontakt").submit(function() {
                if($("von").val() == "" || $("mail").val() == "" || $("nachricht").val() ==""){
                    $("#response").html("bitte fülle alle felder aus!");
                    } else  {
                        $("#response").("Lade...");
                        $.ajax({
                            type:"POST",
                            url:"senden.php",
                            data:"von=" + $("von").val() + "&mail=" + $("mail").val() + ="&nachricht=" + $("nachricht").val(),
                            success: function (msg)
                            {
                            $("#response").html(msg);
                            }
                        );
                    });
                }
            return false;
            });

        });

<form method="post" action="senden.php" id="kontakt">
            <h3 class="title">Nur nicht sch&uuml;chtern !</h3>
            <input type="text" name="von" placeholder="Name" id="von">
            <input type="text" name="mail" placeholder="E-mail" id="mail">
            <textarea name="message" placeholder="Nachricht" id="nachricht"></textarea>
            <input id="submit" type="submit" value="senden"></submit>
            </form>
            <div id="response"></div>

I put it also on jsFiddle http://jsfiddle.net/PqKFb/

  • 写回答

2条回答 默认 最新

  • doutan5724 2013-09-10 15:08
    关注

    Working DEMO

    Since you are using ajax , do it without refreshing the page

    Here is the edited code

    HTML

    <form method="post" id="kontakt">
         <h3 class="title">Nur nicht sch&uuml;chtern !</h3>
    
        <input type="text" name="von" placeholder="Name" id="von">
        <input type="text" name="mail" placeholder="E-mail" id="mail">
        <textarea name="message" placeholder="Nachricht" id="nachricht"></textarea>
        <input id="submit" type="button" value="senden">
        </submit>
    </form>
    <div id="response"></div>
    

    corrected the mistakes in your code, and it would be better to use $('#kontakt').serialize(),it will take the data automatically

    code

        $(document).ready(function () {
        $("#submit").click(function () {
            if ($("#von").val() == "" || $("#mail").val() == "" || $("#nachricht").val() == "") {
                $("#response").html("bitte fülle alle felder aus!");
            } else {
                $("#response").html("Lade...");
                $.ajax({
                    type: "POST",
                    data: $('#kontakt').serialize(),
                    url: "senden.php",
                    success: function (msg) {
                        alert(msg);
                        $("#response").html(msg);
                    }
    
                });
            }
    
        });
    
    });
    

    senden.php

    <?php
    
    $data=$_POST['serialize'];
    $von=$data['von'];  //access data like this
    
    ?>
    

    Hope this helps,Thank you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊