corsasport.co.uk
 

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

Post Reply
Who Can Post? All users can post new topics and all users can reply.
Icon:
Formatting Mode:
Normal
Advanced
Help

Insert Bold text Insert Italicized text Insert Underlined text Insert Centered text Insert a Hyperlink Insert Email Hyperlink Insert an Image Insert Code Formatted text Insert Quoted text
Message:
HTML is Off
Smilies are On
BB Code is On
[img] Code is On
Post Options: Disable smileys?
Turn BBCode off?
Receive email notification of new replies?

Brett

posted on 2nd Mar 15 at 22:01

Well, it just kept annoying me so I did it a different way entirely and this works perfectly :lol: 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>


Brett

posted on 2nd Mar 15 at 20:09

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 :thumbs:

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]


Dom

posted on 2nd Mar 15 at 19:31

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

posted on 2nd Mar 15 at 10:23

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

posted on 2nd Mar 15 at 09:38

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

Icon depicting mood of post posted on 1st Mar 15 at 23:08

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 :lol: