0

I have an input field on my PHP page. When a value is entered into the input box a javascript is called and mysql values returned next to the input box. This works 100% without errors.

My code for this is:

<input type=text id=sku1 name=sku1 onchange="showUser(1, this.value)" onkeypress="return enter(document.orderform.sku2)"  value=<? echo $sku1; ?> >

When the user submits the form, the page returns to itself as some calculations are performed. When the page reloads the input box has the value the user entered ($sku1). my problem is that the mysql values are no longer next to the input box as the input box hasnt changed since reloading.

I want to use something like the javaascript OnLoad functionality.

<input type=text id=sku1 name=sku1 onchange="showUser(1, this.value)" OnLoad="showUser(1, this.value)" onkeypress="return enter(document.orderform.sku2)"  value=<? echo $sku1; ?> > 

This however does not work.

Any ideas as how to get the javascript to execute on load as well as when the user changes the value of the input box?

Thanks and Regards, Ryan Smith

0

3 Answers 3

2

Use jquery's document ready method or try putting showUser in body onload

Sign up to request clarification or add additional context in comments.

Comments

1

You can use onload event like this:

<script>
  var value = document.getElementById('sku1').value;
  window.onload = function() { showUser(1, value); }
</script>

3 Comments

you can assign a function to the onload - here you assigned returned value of a function - which probably would be undefined :) wrap it with an anonymous function to get what you want
onload is a callback and it takes a function reference not function return value if you want it to be called - this would work only if showUser would have returned a function that would show the user
@TomTu: I stumbled upon this question today and understood now what you actually meant, you were right, updated now :)
1

I'd suggest using DOMContentReady, rather than Load, as it will fire earlier. You'll also need to attach the listener to document, rather than the input element itself, as the input element doesn't provide a load event (http://www.w3.org/TR/html4/interact/forms.html#h-17.4)

So, in a script tag, or a separate javascript file:

document.addEventListener('DOMContentReady', function(){
  showUser(1,document.getElementById('sku1'));
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.