Submit form without page reloading

后端 未结 17 2457
无人及你
无人及你 2020-11-22 00:45

I have a classifieds website, and on the page where ads are showed, I am creating a \"Send a tip to a friend\" form...

So anybody who wants can send a tip of the ad

相关标签:
17条回答
  • 2020-11-22 01:17

    You either use AJAX or you

    • create and append an iframe to the document
    • set the iframes name to 'foo'
    • set the forms target to 'foo'
    • submit
    • have the forms action render javascript with 'parent.notify(...)' to give feedback
    • optionally you can remove the iframe
    0 讨论(0)
  • 2020-11-22 01:19

    I did something similar to the jquery above, but I needed to reset my form data and graphic attachment canvases. So here is what I came up with:

        <script>
       $(document).ready(function(){
    
       $("#text_only_radio_button_id").click(function(){
           $("#single_pic_div").hide();
           $("#multi_pic_div").hide();
       });
    
       $("#pic_radio_button_id").click(function(){
          $("#single_pic_div").show();
          $("#multi_pic_div").hide();
        });
    
       $("#gallery_radio_button_id").click(function(){
           $("#single_pic_div").hide();
           $("#multi_pic_div").show();
                     });
        $("#my_Submit_button_ID").click(function() {
              $("#single_pic_div").hide();
              $("#multi_pic_div").hide();
              var url = "script_the_form_gets_posted_to.php"; 
    
           $.ajax({
           type: "POST",
           url: url,
           data: $("#html_form_id").serialize(), 
           success: function(){  
           document.getElementById("html_form_id").reset();
               var canvas=document.getElementById("canvas");
        var canvasA=document.getElementById("canvasA");
        var canvasB=document.getElementById("canvasB");
        var canvasC=document.getElementById("canvasC");
        var canvasD=document.getElementById("canvasD");
    
                  var ctx=canvas.getContext("2d");
                  var ctxA=canvasA.getContext("2d");
                  var ctxB=canvasB.getContext("2d");
                  var ctxC=canvasC.getContext("2d");
                  var ctxD=canvasD.getContext("2d");
                   ctx.clearRect(0, 0,480,480);
                   ctxA.clearRect(0, 0,480,480);
                   ctxB.clearRect(0, 0,480,480);        
                   ctxC.clearRect(0, 0,480,480);
                   ctxD.clearRect(0, 0,480,480);
                   } });
               return false;
                    });    });
               </script>
    

    That works well for me, for your application of just an html form, we can simplify this jquery code like this:

           <script>
            $(document).ready(function(){
    
        $("#my_Submit_button_ID").click(function() {
            var url =  "script_the_form_gets_posted_to.php";
           $.ajax({
           type: "POST",
           url: url,
           data: $("#html_form_id").serialize(), 
           success: function(){  
           document.getElementById("html_form_id").reset();
                } });
               return false;
                    });    });
               </script>
    
    0 讨论(0)
  • 2020-11-22 01:20

    Here is some jQuery for posting to a php page and getting html back:

    $('form').submit(function() {
        $.post('tip.php', function(html) {
           // do what you need in your success callback
        }
        return false;
    });
    
    0 讨论(0)
  • 2020-11-22 01:21

    This should solve your problem.
    In this code after submit button click we call jquery ajax and we pass url to post
    type POST/GET
    data: data information you can select input fields or any other.
    sucess: callback if everything is ok from server
    function parameter text, html or json, response from server
    in sucess you can write write warnings if data you got is in some kind of state and so on. or execute your code what to do next.

    <form id='tip'>
    Tip somebody: <input name="tip_email" id="tip_email" type="text" size="30" onfocus="tip_div(1);" onblur="tip_div(2);"/>
     <input type="submit" id="submit" value="Skicka Tips"/>
     <input type="hidden" id="ad_id" name="ad_id" />
     </form>
    <script>
    $( "#tip" ).submit(function( e ) {
        e.preventDefault();
        $.ajax({
            url: tip.php,
            type:'POST',
            data:
            {
                tip_email: $('#tip_email').val(),
                ad_id: $('#ad_id').val()
            },
            success: function(msg)
            {
    
                alert('Email Sent');
            }               
        });
    });
    </script>
    
    0 讨论(0)
  • 2020-11-22 01:21

    You can try setting the target attribute of your form to a hidden iframe, so the page containing the form won't get reloaded.

    I tried it with file uploads (which we know can't be done via AJAX), and it worked beautifully.

    0 讨论(0)
  • 2020-11-22 01:23
    function Foo(){  
       event.preventDefault(); 
     $.ajax(   {  
          url:"<?php echo base_url();?>Controllername/ctlr_function",
          type:"POST",
          data:'email='+$("#email").val(),
          success:function(msg)      {  
             alert('You are subscribed');
          }
       }   );
    }
    

    I tried many times for a good solution and answer by @taufique helped me to arrive at this answer.

    NB : Don't forget to put event.preventDefault(); at the beginning of the body of the function .

    0 讨论(0)
提交回复
热议问题