corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » JQuery Again (lol)


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author JQuery Again (lol)
Brett
Premium Member

Avatar

Registered: 16th Dec 02
Location: Manchester
User status: Offline
   1st Mar 15 at 23:08   View Garage View User's Profile U2U Member Reply With Quote

What have I done wrong below?

The code works, except for the form being submitted bit. It submits straight away no matter what, with or without the "This is a primary key and requires a unique value" text being present on the page. If I remove that bit then it behaves as it should, taking the next number and padding the value with the zeros (obviously not submitting after doing so).

<script>
$(document).ready(function(){
var poo = $('*:contains("This is a primary key and requires a unique value")');

if (poo.length > 0) {
$( "input[name='table2field1']" ).val(parseInt($( "input[name='table2field1']" ).val(), "10")+1);
$( "input[name='table2field1']" ).val(String("0000" + $( "input[name='table2field1']" ).val()).slice(- 4));
$("form[name='the_form_id']").submit();
}
});
</script>

Like last time this may seem strange to people and you may question why, but gimme a break please
Dom
Member

Registered: 13th Sep 03
User status: Offline
2nd Mar 15 at 09:38   View User's Profile U2U Member Reply With Quote

Either you intercept the forms submit event -
code:
<script>
// intercept submit event
$( "form[name='the_form_id']" ).submit( function(e) {

var poo = $( "*:contains('This is a primary key and requires a unique value')" );

if (poo.length > 0) {
$( "input[name='table2field1']" ).val( parseInt( $( "input[name='table2field1']" ).val(), "10") +1 );
$( "input[name='table2field1']" ).val( String( "0000" + $( "input[name='table2field1']" ).val() ).slice(- 4) );

return true; // submit form
} else {
e.preventDefault(); // prevent default submit functionality if above condition isn't met
}
});
</script>


Or, replace the form's submit button with a 'vanilla' button (give it an id or name attribute) and trigger it all off a .click() event -
code:
<button type="button" name="gogogo">Submit</button>
<script>
$( "button[name='gogogo']" ).click( function() {
// Guff here

$("form[name='the_form_id']").submit(); // submit form
});
</script>



Edit - If going down the button route, then you'd probably want to disable the forms default submit event as it might be possible for a user to trigger a submit off a form elements key press.

[Edited on 02-03-2015 by Dom]
Brett
Premium Member

Avatar

Registered: 16th Dec 02
Location: Manchester
User status: Offline
2nd Mar 15 at 10:23   View Garage View User's Profile U2U Member Reply With Quote

Cheers Dom. I think doing it on the form submit or click event is the right way to go here actually. I can't get it to work though

I just can't get the damn thing to capture the form submission or even the button click ffs. Does nothing.

quote:
<INPUT TYPE="submit" VALUE="Submit" id="button">


Bit of background what this is for..

What happens is a user will submit the form. Occasionally if two users have tried to at once then it'll come back and give that 'primary key' error on the screen. What my code does then is take the next sequential number so there's no conflict and automatically resubmit. This is so the user doesn't really know what's going on.

[Edited on 02-03-2015 by Brett]
Dom
Member

Registered: 13th Sep 03
User status: Offline
2nd Mar 15 at 19:31   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by Brett
Occasionally if two users have tried to at once then it'll come back and give that 'primary key' error on the screen.....


Can you not deal with that server-side? That'd be the typical route for something like this......


Although if i'm understanding you correctly, the user submits the form (/page) and an error is returned which is requiring you to re-submit the page with the adjusted data; if that's the case then what you had original should work, ie - on page load/ready you check for the error and re-submit the form.

Is your IF statement firing correctly on page load (stick an alert in place of the .submit() to test) / Is 'poo' returning a value?
Is the JQuery selector working correctly (perhaps use an ID attribute on the form tag and use vanilla JS - http://www.w3schools.com/jsref/met_form_submit.asp)?
Brett
Premium Member

Avatar

Registered: 16th Dec 02
Location: Manchester
User status: Offline
2nd Mar 15 at 20:09   View Garage View User's Profile U2U Member Reply With Quote

I'll take a look. The strange bit is the number increase and padding works exactly how it should implying the if is correct. Once the form submit is added also the it just submit straight away on page load whether or not required text is present. Strangely, it behaves perfectly in an old browser like IE8, but when in a later version or Chrome it behaves incorrectly as described. I'll stick an alert in tho and see what's happening

Any of these strange JavaScript posts I make would no doubt be better fixed in the back end but unfortunately that just isn't possible. Seems to be an old system that's around to stay but with a few correct tweaks could end up being semi-ok. This is the same system that wouldn't allow multiple file select.


[Edited on 02-03-2015 by Brett]
Brett
Premium Member

Avatar

Registered: 16th Dec 02
Location: Manchester
User status: Offline
2nd Mar 15 at 22:01   View Garage View User's Profile U2U Member Reply With Quote

Well, it just kept annoying me so I did it a different way entirely and this works perfectly Job done I guess. Cheers for your time Dom

code:

<script>
$(document).ready(function(){
myDivObj = document.getElementById("error");
if ( myDivObj.innerHTML.length > 10 ) {
$( "input[name='table2field1']" ).val(parseInt($( "input[name='table2field1']" ).val(), "10")+1);
$( "input[name='table2field1']" ).val(String("0000" + $( "input[name='table2field1']" ).val()).slice(- 4));
$("form[name='the_form_id']").submit();
}
});
</script>


 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
flash script help Matt L Geek Day 14 578
11th Jan 09 at 01:40
by Ian
 
CSS help Butler Geek Day 3 651
1st Aug 09 at 14:00
by liamC
 
Any Javascript peeps on here? ed Geek Day 7 467
10th Mar 10 at 18:40
by ed
 
Need a form making... Half way there... need some Java guys in here! Gary Geek Day 32 1785
4th Nov 10 at 13:32
by ed
 
Database Gurus Bart Geek Day 12 708
23rd Apr 12 at 15:00
by Bart
 

Corsa Sport » Message Board » Off Day » Geek Day » JQuery Again (lol) 29 database queries in 0.0101700 seconds