weixin_33728708 2018-05-21 09:37 采纳率: 0%
浏览 60

如何提交一个表单而不重新加载页面?

我想提交一个表单而不重新加载页面,这可能是一个老话题了。但是我找到的解决方案根本不起作用,我也不知道哪里做错了。

我知道这是一个JavaScript问题,因为如果我使用简单版本,提交没有任何JavaScript的表单,则会收到错误提示。

这是我的HTML:

<form action="confirm.php" id="support" method="post">
    <input autocomplete="off" id="name" name="name" placeholder="Numele tău" required="" type="text" />
    <input autocomplete="off" id="email" name="email" placeholder="Email-ul tău" required="" type="email" />
    <input autocomplete="off" id="phone" name="phone" placeholder="Numărul tău de telefon (opțional)" type="tel" />
    <input class="button" name="submit" type="submit" value="TRIMITE" />
    <p id="success">Thanks!</p>
</form>

PHP:

<?php
    if(isset($_POST['submit'])){
        $to = "invest@es-sphere.ro";
        $name = $_POST['name'];
        $from = $_POST['email'];
        $phone = $_POST['phone'];

        $subject = "Sunt interesat de INVEST!";

        $message = $name . ", cu numărul de telefon " . $phone . " și email-ul " . $from . "este interesat de proiectul INVEST";

        $headers = "From:" . $from;
        mail($to,$subject,$message,$headers);
    }
?>

javascript:

$(document).ready(function () {
  $('#support').submit(function (e) {

    e.preventDefault();
    var formData = $(this).serialize();

    $.post("confirm.php", formData).done(function() {
        console.log("Success");
        $("#success").show();
    });
  });
});
  • 写回答

2条回答 默认 最新

  • weixin_33694620 2018-05-21 09:40
    关注

    $(this).serialize() will not gather the name/value of the submit button used to submit the form.

    if(isset($_POST['submit'])){ will therefore fail.

    Use some other test (e.g. another field) to see if form data is being submitted.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题