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条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?