Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-28-2011, 07:52 PM   PM User | #1
leest
New Coder

 
Join Date: Sep 2011
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
leest is an unknown quantity at this point
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
leest is offline   Reply With Quote
Old 09-29-2011, 12:17 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:05 AM.


Advertisement
Log in to turn off these ads.