kisukedeath

Submit form data into iframe fancybox

May 20th, 2014
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 1.82 KB | None | 0 0
  1. //How do I submit the data of a form to an iframe in Fancybox?
  2.  
  3. Option 1: Show just the results (as HTML) in Fancybox:
  4.  
  5. $("#my-form").submit(function() {
  6.  
  7.     $form = $(this);
  8.  
  9.     $.ajax({
  10.       url: $form.attr("action"),
  11.       type: 'POST',
  12.       dataType: 'html',
  13.       data: $form.serialize(),
  14.       success: function(data, textStatus, xhr) {
  15.             $.fancybox({
  16.                     'title': "form submission",
  17.                     'content': data
  18.             });
  19.       },
  20.       error: function(xhr, textStatus, errorThrown) {
  21.         alert("An error occurred.");
  22.       }
  23.     });
  24.  
  25.     return false;
  26.  
  27. });
  28.    
  29.  
  30. Option 2: Change to a GET request and use a Fancybox Iframe:
  31.  
  32. $(function() {
  33.  
  34.     $("#my-form").submit(function() {
  35.  
  36.         $form = $(this);
  37.  
  38.         $.fancybox({
  39.                 'title': "form submission",
  40.                 'href': $form.attr("action") + "?" + $form.serialize(),
  41.                 'type': 'iframe'
  42.         });
  43.  
  44.         return false;
  45.  
  46.     });
  47.  
  48.  
  49. });
  50.    
  51.  
  52. Option 3: Forget FancyBox and do this thing yourself
  53.  
  54. <script>
  55.  
  56.  
  57.     $(function() {
  58.  
  59.         $("#my-form").submit(function() {
  60.  
  61.             $form = $(this);
  62.             $iframe = $("#form-submission");
  63.             $window = $(window);
  64.  
  65.             $iframe.css({
  66.                 height: 500,
  67.                 width: 500,
  68.                 position: "absolute",
  69.                 left: ($window.width() / 2) - 250,
  70.                 top: ($window.height() / 2) - 250
  71.             });
  72.  
  73.             $iframe.fadeIn();
  74.  
  75.         });
  76.  
  77.  
  78.     });
  79.  
  80.  
  81.  
  82. </script>
  83.  
  84. <form action="test.php" method="post" target="form-submission" id="my-form">
  85.     <input name="q" value="myvalue">
  86.     <p><input type="submit" value="submit"></p>
  87. </form>
  88.  
  89. <iframe src="#" name="form-submission" id="form-submission" style="display: none;
Advertisement
Add Comment
Please, Sign In to add comment