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 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Inserting php variables using javascript

    Hi,

    I am using javascript to collect hidden form elements and then send the values to a php page, without reloading or changing the page. Basically i have a list of job vacancies, with just an apply button, when a person clicks on the apply button, the form collects some information from their account and sends it using the javascript code.

    Now this works fine, except for if somebody then clicks on the second job it sends the information from the first form again. And it seems to remember the information.

    What i am trying to find out is it possible to have multiple forms on one page which once submitted send variables to javascript which then send them to a php but clear themselves so that it doesn't remember the previous values.

    Here is the code for the form:

    Code:
    <form id="submit" method="post">
                                <fieldset>
                           <input type="hidden" name="usern" id="usern" value="php variable" />
                           <input type="hidden" name="joborder_id"  id ="joborder_id" value="php variable" />
                           <input type="hidden" name="site_id"  id ="site_id" value="php variable" />
                           <button class="button positive">Apply For This Job </button>
                           </fieldset>
                           </form>
                           <div class="success" style="display: none;">Your Application has been sent.</div>
                                  &nbsp;
                                </form>
    And the Javascript code is here:

    Code:
    $(document).ready(function(){
    	$("form#submit").submit(function() {
    	// we want to store the values from the form input box, then send via ajax below
    	var usern     = $('#usern').attr('value');
    	var joborder_id    = $('#joborder_id').attr('value');
    	var site_id    = $('#site_id').attr('value');
    		$.ajax({
    			type: "POST",
    			url: "apply.php",
    			data: "usern="+ usern +"& joborder_id="+ joborder_id +"& site_id="+ site_id,
    			success: function(){
    				$('form#submit').hide(function(){$('div.success').fadeIn();});
    
    			}
    		});
    	return false;
    	});
    });
    Any ideas or suggestions would be appreciated,

    Thanks

    Lee

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,334 Times in 4,300 Posts
    Just call the <form>'s reset() method, which acts the same as the user pushing a RESET button.

    But if that doesn't work, then it's because your jQuery code can't figure out that it's only supposed to process a single <form>.

    If all the forms (and form fields) have the same id, that makes tons of sense. It's illegal to use the same id more than once on a page.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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