【问题标题】:Ajax keep redirecting to the php pageAjax 不断重定向到 php 页面
【发布时间】:2019-02-12 02:58:26
【问题描述】:

我有一个 shopify 商店,我想在产品页面上添加一个表单,问题是当我想使用带有外部链接的 ajax 提交一个表单时,但我想留在它转到外部链接的同一页面上

<form method="post" id="fastform"action="http://website.fun/bianca/doc.php" class="form" enctype="multipart/form-data">
<label  class="label" style="color: rgb(0, 0, 0);">Nom :  </label>
<input placeholder="Nom"    class="input" width="100%" type="text" name="nom" id="nom">
<label  class="label" style="color: rgb(0, 0, 0);">Télephone* :  </label>
<input placeholder="Telephone"    class="input" width="100%" type="text" name="phone" id="phone">
<input placeholder="Adresse"    class="input" width="100%" type="text" name="adresse" id="adresse">
<div id="form-messages"></div>
<button type="submit" class="button-input btn btn-default" name="btn"style="margin-top: 20px;" id="btn">Commander</button>

这是 js en ajax 部分

<script>

$(function () {
    // Get the form.
    var form = $('#fastform');

    // Get the messages div.
    var formMessages = $('#form-messages');

    // Set up an event listener for the contact form.
    $(form).submit(function (event) {
        // Stop the browser from submitting the form.
        event.preventDefault();
        // Serialize the form data.
        var formData = $(form).serialize();
        $.ajax({
            type: 'POST',
            url: $(form).attr('action'),
            data: formData
            }.done(function(response) {
// Make sure that the formMessages div has the 'success' class.
            $(formMessages).removeClass('error');
            $(formMessages).addClass('success');

            // Set the message text.
            $(formMessages).text("votre commande et bien traiter");

            // Clear the form.
            $('#name').val('');
            $('#email').val('');
            $('#message').val('');
        })).fail(function(data) {
// Make sure that the formMessages div has the 'error' class.
            $(formMessages).removeClass('success');
            $(formMessages).addClass('error');

            // Set the message text.
            if (data.responseText !== '') {
                $(formMessages).text(data.responseText);
            } else {
                $(formMessages).text('Oops! il\'ya un problem');
            }
        });
                });

我希望页面保持不变并在同一页面上获得结果

【问题讨论】:

  • 尝试在您的 html 表单中删除 action="http://website.fun/bianca/doc.php"
  • 但想处理该页面上存在于外部 url 上的表单
  • 您可以将ajax url设置为http://website.fun/bianca/doc.php作为字符串。
  • @abdessamadBen 你试过在提交按钮上使用 onclick="return false"
  • 是的,我试过了,同样的问题

标签: javascript php ajax shopify


【解决方案1】:

试试这个,

$( "#your_form_id" ).submit(function(event) {
    event.preventDefault();
    $.ajax({
            beforeSend: function () {
                //something to do before ajax call
            },
            complete: function () {
               //something to do after ajax call
            },
            type:"POST",
            data:$(this).serialize(),
            url:"<?php echo $url;?>",
            success:function(data){
                $('#divId').html(data); // id where you want to retrive and show your data
            }
    });

});

请再次检查您的操作网址

【讨论】:

    【解决方案2】:

    试试这个

    像这样改变表格。

    <form method="post" id="fastform" onsubmit='return submitForm()' action="http://website.fun/bianca/doc.php" class="form" enctype="multipart/form-data">
    

    然后像这样改变函数,

      function submitForm(){
    
      var formData = $('#fastform').serialize();
      $.ajax({
          type: 'POST',
          url: $('#fastform').attr('action'),
          data: formData
      }.done(function(response) {
          // Make sure that the formMessages div has the 'success' class.
          $(formMessages).removeClass('error');
          $(formMessages).addClass('success');
    
          // Set the message text.
          $(formMessages).text("votre commande et bien traiter");
    
          // Clear the form.
          $('#name').val('');
          $('#email').val('');
          $('#message').val('');
      })).fail(function(data) {
          // Make sure that the formMessages div has the 'error' class.
          $(formMessages).removeClass('success');
          $(formMessages).addClass('error');
    
          // Set the message text.
          if (data.responseText !== '') {
              $(formMessages).text(data.responseText);
          } else {
              $(formMessages).text('Oops! il\'ya un problem');
          }
      });
    
      return false;
    
    }
    

    【讨论】:

    • 感谢您的回复,但我在 shopify 网站上工作并编辑产品页面时仍然遇到同样的问题,但我看到很多人在商店里这样做
    【解决方案3】:

    试试这个代码:

    $(function (){ 
     $('form').bind('submit', function () { 
           $.ajax({ type: 'post', 
            url: 'libs/send.php', 
            data: new FormData($('form')[0]),
            cache: false, 
            contentType: false,
            processData: false,
            success: function () { 
          alert("Data has been successfully inserted"); 
       } 
        }); 
       return false; 
     }); 
    });
    

    【讨论】:

    • 同样的问题,我认为 shopify 有问题,我从 ajax 发送请求,它在不使用的情况下发送
    • 好的,你可以试试这个代码:$(document).ready(function(){ $("#addcate").submit(function(e){ var data1=$('#data1 ').val(); var data=$('#data').val(); $.ajax({ url: "libs/send.php", method:"post", data: {data1:data1,数据:数据},成功:功能(响应){警报(响应);}});});});
    猜你喜欢
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    相关资源
    最近更新 更多