简体   繁体   English

OnClick重定向到我在PHP中的网站的特定部分

[英]OnClick redirect to a particular section of my website in Php

My HTML: 我的HTML:

<div class=" col-sm-7 slideanim text-center ">
  <div class="row text-center">
  <h4 class="text-center"><b>Fill the details and we'll get back to you within 6 business hours. <br>
  * - Required</b></h4><i>
    <form action="mail_handler.php" method="post" name="form" class="formfont">
    <div class="col-sm-6 form-group">   
        <input type="text" name="name" class="inp form-control" placeholder="Enter Your Name*" required="">
        </div>
        <div class="col-sm-6 form-group">

        <input type="email" name="email" class="inp form-control" placeholder="Enter Your Email*" required="">
        </div>
        <div class="col-sm-6 form-group">

        <input type="tel" name="phone" class="inp form-control" placeholder="Enter Your Phone*" required="">
        </div>

        <div class="col-sm-6 form-group">

        <input type="text" name="college" class="inp form-control" placeholder="College/University*" required="">

        </div>
        <div class="col-sm-12 form-group">
        <textarea name="msg" class="msg-box form-control" placeholder="Enter Your Message Here...(Optional)"></textarea>
        </div>
        <input type="submit" name="submit" value="Send" class="sub-btn">
        <br>
          <br>
    </form>
  </i></div>

  </div>

MailHandler php MailHandler PHP

<?php
if(isset($_POST['submit'])){
    $name=$_POST['name'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $college=$_POST['college'];
    $msg=$_POST['msg'];

    $to='mailtosemicolon@gmail.com'; // Receiver Email ID, Replace with your email ID
    $subject='Form Submission';
    $message="Name :".$name."\n"."Phone :".$phone."\n"."Wrote the following :"."\n\n".$msg;
    $headers="From: ".$email;

    if(mail($to, $subject, $message, $headers)){
        echo "<h1>Sent Successfully! Thank you"." ".$name.", We will contact you shortly!</h1>";

    }
    else{
        echo "Something went wrong! Please try back sometime later";
    }
}?>

I am designing a web page which has 3 sections in it, namely section 1, section 2, and section 3 respectively. 我正在设计一个包含3个部分的网页,分别是第1部分,第2部分和第3部分。 So, in section 3, i have a form and given its action as php file[MailHandler.php], which actually gets details from the form and sends a mail to my personal mail once the submit button(inside the form) is clicked. 因此,在第3节中,我有一个表单,并以php文件[MailHandler.php]的形式给出了操作,该文件实际上是从表单中获取详细信息,并在单击“提交”按钮(在表单内部)后将邮件发送到我的个人邮件中。

Is there any way to redirect to section 3, when the details are successfully sent to my mail id, and display a success message below my submit button in the form 成功将详细信息发送到我的邮件ID后,有什么方法可以重定向到第3节,并在表单中“提交”按钮下方显示成功消息。

Since you stay in section 3, there is no need for a redirect. 由于您停留在第3节中,因此无需重定向。 I would solve this in Javascript. 我会用Java语言解决。

What you can do is replace the submit button with a normal button. 您可以做的是将提交按钮替换为普通按钮。 When clicked, you use Javascript to generate an ajax request to the server, like your submit would do. 单击后,您可以使用Javascript向服务器生成ajax请求,就像您的提交一样。 With the return code or custom return data, you can insert the response using Javascript below your form. 使用返回码或自定义返回数据,您可以在表单下方使用Java脚本插入响应。 If you are using JQuery, you can quickly generate the form data like this: 如果使用的是JQuery,则可以像这样快速生成表单数据:

$('form').serialize()

An ajax request has a done or success method which you can use. 一个ajax请求具有可以使用的donesuccess方法。 See this example: https://www.w3schools.com/jquery/ajax_ajax.asp 参见以下示例: https : //www.w3schools.com/jquery/ajax_ajax.asp

There can be two approaches to your problem. 有两种方法可以解决您的问题。

The first one is to use, JavaScript. 第一个是使用JavaScript。 By this way, you can submit the contact form to your PHP script and then show the response message without even reloading the page, the tutorial can be found here . 这样,您可以将联系表单提交到您的PHP脚本,然后显示响应消息,而无需重新加载页面,该教程可在此处找到。

The second method is when you submit the contact form to PHP, then using PHP you can redirect back to your HTML page with a section id appended in your URL, something like http://www.example.com/index.html#section3 , where section3 is the ID of your section. 第二种方法是,当您将联系表单提交给PHP时,然后使用PHP您可以重定向回HTML页面,并在URL中附加节ID,例如http://www.example.com/index.html#section3 ,其中section3是您的部分的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM