dou8mwz5079 2019-05-04 08:42
浏览 43
已采纳

如何同时发送数据和重定向?

I need to upload the inserted data of the form and go to the Thank You Page on submission. I've tried some methods and I managed to only get one working, but not both.

The site on Wordpress and XAMPP. The following code just redirects, but if I remove the onsubmit attribute it will submit but won't redirect.

<script>
   function redirect(){
       window.location.replace("localhost/concurs/thank-you-page");
       return false;
   }
</script>

<?php
      $x=new mysqli("localhost", "root", "");
      if ($x->connect_error) {
          die("Connection failed: " . $x->connect_error);
      }
      $y=mysqli_select_db($x,"concurs");
      $Prenume = "";
      $Nume = "";
      $email = "";
      $tel = "";
      $Prenume= isset($_POST['firstname']) ? $_POST['firstname'] : '';
      $Nume= isset($_POST['lastname']) ? $_POST['lastname'] : '';
      $email= isset($_POST['email']) ? $_POST['email'] : '';
      $tel= isset($_POST['tel']) ? $_POST['tel'] : '';
      $z = "";
      if($Prenume!=null && $Nume!=null && $email!=null)
             $z=mysqli_query($x,"INSERT INTO concurs.wp_formular (Prenume, Nume, email, tel) VALUES ('$Prenume', '$Nume','$email', '$tel')");
      mysqli_close($x);
      ?>

<form data-validate="parsley" data-parsley-validate="" id="myForm" method="post" action="" onsubmit="return redirect()">
    <button type="submit" class="form_submit LatoBlack" id="submit"> 
Submit </button>
</form>

  • 写回答

1条回答 默认 最新

  • drip5880 2019-05-04 08:56
    关注

    Try this

    $query=mysqli_query($con,"INSERT INTO `table` (name,mobile) VALUES('$name','$mobile')");
    
    if($query){
    ?>
     <script>
    window.location.href="thankyou.php";
    </script> 
    <?php
    }else{
       echo mysqli_error();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块