Buying Javascript Help with Form Submission

Discussion in 'Programming' started by mcmuney, Jan 6, 2013.

  1. #1
    I'm using a very simple form to submit some data, which is shown below. What I want to do is utilize javascript to do the following:

    1) On form submit button click, replace the submit button with a "Loading... " image/text and show it for about 5 seconds, then
    2) Actually submit the form to execute.
    3) Everything else needs to work the same, including the "if (isset..." function to display the submitted value.

    
    <?
    if (isset($raid_command)){
    	echo $amount;
    }else{
    ?>
    <form method="post" action="">
    <input type="hidden" name="amount" value="100" />
    <input type="hidden" name="raid2_cost" value="200" />
    <input class="btn" type="submit" name="raid_command" value="Raid Now!" />
    <?}?>
    
    Code (markup):
     
    mcmuney, Jan 6, 2013 IP
  2. chanif.alfath

    chanif.alfath Active Member

    Messages:
    148
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    88
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    Hello brow,

    you can use jquery to replace the button with some loading image,
    then redirect it after 5 second..

    try google : "javascript delay form post"

    you'll find a lot of good source :)
     
    chanif.alfath, Jan 6, 2013 IP
  3. mcmuney

    mcmuney Well-Known Member

    Messages:
    834
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    As Seller:
    100% - 0
    As Buyer:
    100% - 3
    #3
    Thanks! I almost got it. Take a look at the code below. This does the 2 steps that I need; however, the only problem is that this works when the button is type="button" and not type="submit". I need it to be type="submit" so that it posts the values. What am I missing here?


    
    <script type="text/javascript">
            function submitForm() { // submits form
                    document.getElementById("ismForm").submit();
            }
            function btnSearchClick()
            {
                    if (document.getElementById("ismForm")) {
       document.getElementById('searchx').style.display = 'none';
                     document.getElementById('loading').style.display = 'block';            
                    setTimeout("submitForm()", 5000); // set timout          
               }
            }
            </script>
    <form method="post" id="ismForm" name="ismForm" action="test3.php?x" class="">
    <input type="hidden" name="amount" value="100" />
    <div id="loading" style="display:none;"><img src="http://article.onlinewebtool.com/wp-content/images/loading.gif" alt="" />Loading!</div>
            <input class="button" onclick="btnSearchClick();" type="button" id="searchx" name="searchx" value="Search" autocomplete="off">
            </form>
    
    Code (markup):
     
    mcmuney, Jan 7, 2013 IP
  4. Basti

    Basti Active Member

    Messages:
    625
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    90
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    Your issue is that you submit the form once you click the button if its type=submit and so the javascript dont fire. Your should add a return false on your onclick like
    
    <input class="button" onclick="btnSearchClick(); return false;" type="submit" id="searchx" name="searchx" value="Search" autocomplete="off">
    
    Code (markup):
    But whats wrong if you use type="button"? See no problem with it
     
    Basti, Jan 8, 2013 IP