Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2009
    Location
    Odense, Denmark
    Posts
    72
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Jquery form submission seems to remove form elements...

    I have a simple form like this:

    PHP Code:
    <form action="#" method="post">
    <
    input type="hidden" name="hidden" value="value" />
    <
    input type"submit" name="submit" value="submit" />
    </
    form
    I've added the following javascript/jQuery:

    Code:
    $("input[type=submit]").live("click", function (e) 
    {
    
    	e.preventDefault();
    	
    	var $button = $(this);
    	var $form = $(this).parent("form:first");
    	
    	// Disable
    	$button.attr("disabled", true);
    	
    	// Ændr tekst
    	$button.val("Sender...");
    	
    	//Submit form
    	$form.submit();
    });
    ... to make sure that the form isn't submitted more than once (which it sometimes is).

    Most of it works fine but when the page returns from the server this output of $_POST is only the hidden element. It seems to remove the submit-button.

    Can anybody tell me why this is?


    Much regards, very help, wow
    deafdigit

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    In a form a button and its value is submitted if and only if the button was used to submit the form. In your case you are clicking the button, but you prevent its default behavior and submit the form "manually" which removes the button value. Why would you want to send the button value if you hardcode it to be "Sender..."? You could assume on PHP side that $_POST["submit"] would always be "Sender..." then

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,466
    Thanks
    23
    Thanked 634 Times in 633 Posts
    <form action="#" tells me the php code is combined with your html and unless you have a stop/die clause in it your html will just reload the page and the submit button will be working.
    This
    It seems to remove the submit-button.
    doesn't make sense unless you mean it can be clicked again.
    You might want to use ajax to submit to a seperate php file so your final html page(with the submit button disabled) don't change.
    You might want to publish the entire code for us instead of parts that only has us/me guessing.

    FYI: You must have an old copy of jquery because .live is discontinued. Use .on
    Code:
    $("input[type=submit]").live("click", function(e){
    might be less code to use the form as a selector, a;though your code above works and is OK:
    Code:
    $("form").on("submit", function(e){
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •