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 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 多ip服务器站群如何搭建l2tp服务器
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗
  • ¥15 Sharepoint JS开发 付费技术指导
  • ¥15 输入程序运行仿真后,烟雾值不实时检测,变成固定值
  • ¥20 数据排序,可选择排序方向
  • ¥15 修改一下代码,考虑进程到达时间不同的情况
  • ¥15 帮我看看这是个啥题,带解题过程和结果,条件如下FCF = 290471.33 g1 = 15% r = 8% g2 = 4% n = 5