如何使用php重定向按钮点击


How to redirect on button click using php

我在表单上使用了一个按钮。在成功提交用户信息后,我试图通过点击按钮重定向到另一个页面。除了重定向之外,其他一切都能正常工作。这是我试过的代码。

Html代码:

<form role="form" id="contact-form" method="get">
  <div class="form-group row">
      <input type="email" id="email" name="email" placeholder="Enter your email" required="required" class="form-control input-lg" />
      <input type="text" id="address" name="address" placeholder="Enter your address" required="required" class="form-control input-lg" />
      <button type="submit" class="btn btn-t-primary">Show Me Now!</button>
  </div>
</form>

Javascript代码:

function contact() {
  $("#contact-us-form").submit(function (e) {
  e.preventDefault();
  e.stopImmediatePropagation();
  form = $(this);
  data = $(this).serialize();
  $.post("contact.php", data, function(response){
      form.trigger('reset');
  });
}

Php代码:

<?php 
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
if (isset($_POST['email'])) {
    $email = $_POST['email'];
    $address= $_POST['address'];
    $subject = "Message from: ".$email;
    $content = "Email: " . $email."'n"
            . "Address: " . $address;
    $headers ='Reply-To: ' . $email . "'r'n";
    mail('example@gmail.com', $subject ,$content, $headers );
    header("Location:https://www.example.com");
    echo 1;
}else {
    echo 0;
}

?>

您不能重定向来自PHP的ajax请求,而是使用以下内容:

$(function() {
    $("#contact-form").submit(function(e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        form = $(this);
        data = $(this).serialize();
        $.post("contact.php", data, function(response) {
            form.trigger('reset');
            //Redirection
            window.location.href = "https://www.google.com";
        });
    });
});

只需从PHP中删除此header("Location:https://www.example.com");即可。

我希望这对你有帮助。

在设置标题时,可能需要编写整个url:

header("Location: http://www.google.com");

这实际上并没有必要。

在HTML form标记的内部,添加要将数据发布到action属性内部的位置。

<form role="form"  id="contact-form" method="get" action="postpage.php"> <!-- notice the action attribute !-->

试试这个!

echo '<meta http-equiv=REFRESH CONTENT=0;url=./yourDestination.php>';

正如我们在代码中看到的,页面将刷新到您声明的url。

"Content=0"表示它将在0秒后刷新如果你愿意,你可以修改号码!

我想知道为什么设置form method="get",但在php代码下使用$_POST变量。下面是使用POST方法的另一个版本,如果有帮助,请尝试。

<?php
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
if (isset($_POST['email'])) {
    $email = $_POST['email'];
    $address= $_POST['address'];
    $subject = "Message from: ".$email;
    $content = "Email: " . $email."'n"
            . "Address: " . $address;
    $headers ='Reply-To: ' . $email . "'r'n";
    mail('example@gmail.com', $subject ,$content, $headers );
    header("location: https://google.com");
    exit;
}
?>
<form role="form" id="contact-form" method="post">
  <div class="form-group row">
      <input type="email" id="email" name="email" placeholder="Enter your email" required="required" class="form-control input-lg" />
      <input type="text" id="address" name="address" placeholder="Enter your address" required="required" class="form-control input-lg" />
      <button type="submit" class="btn btn-t-primary">Show Me Now!</button>
  </div>
</form>

如果要重定向到新站点,则需要设置绝对地址,包括标头位置中的协议(https://)。

header("Location: https://www.google.com")

如果没有协议,则假定位置是相对的,并将附加到现有域中。

此外,您不应该在标头之后回显任何内容。

首先,您必须编写处理Ajax请求的PHP代码:

if (isset($_POST['ajax_request'])) {
    // write code;
    echo "http://www.example.com"; // redirect url
}

Javascript:

 jQuery(document).ready(function($) {
    $.post('/path/to/file.php', {"ajax_request": true}, function(data, textStatus, xhr) {
        window.location.href = data; // http://www.example.com
    });
});

希望这能有所帮助。