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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵