Ajax不断重定向到php页面

时间:2019-02-12 02:58:27

标签: javascript php ajax shopify

我有一家shopify商店,我想在产品页面上添加表格,问题是当我想通过外部链接使用ajax提交表单,但我希望停留在同一页面上时,它会转到外部链接< / p>

<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');
            }
        });
                });

我希望页面保持原样而不刷新,并将结果显示在同一页面上

3 个答案:

答案 0 :(得分:1)

尝试一下

#include <stdio.h>

int main ()
{
  int a, b;

  int nums[48];

  for (int i = 0; i < 47; i++)

    {
      printf ("Pick a number between 1 - 47\n");
      scanf ("%d", &a);

      printf ("Pick a number between 1 - 47\n");
      scanf ("%d", &b);

      if (a >= 47 || a <= 1)
    {
      printf ("Out of range pick another number between 1 - 47\n");
      scanf ("%d", &a);
    }

      if (b >= 47 || b <= 1)
    {
      printf ("Out of range pick another number between 1 - 47\n");
      scanf ("%d", &b);
    }

      nums[i] = a;
      nums[i + 1] = b;

      int c = a + b;

      printf ("The sequence is: %d\n", c);
    }
  return 0;
}

请再次查看您的操作网址

答案 1 :(得分:0)

尝试一下

像这样更改表格。

<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;

}

答案 2 :(得分:0)

尝试此代码:

$(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; 
 }); 
});