![]() |
form button action
Hi - I have a form for uplaoding inamges to my website. Sometimes if the image size is large it takes 5 seconds or so. I want to try and use javascript to kick off an action that adds an image href indicating "Image Upload Processing" when the Submit button is pressed. How would i best go about doing this? My php is good but javascript isnt so advanced. here's th form html:
PHP Code:
|
That could be done deferent ways. One way is having backend script that handles the upload with ajax dealing with the waiting (while showing the uploading image) and responding to the result of the backend script.
It's been a while since I've done AJAX, but here it is a simple tutorial. |
I'm not looking to do something too complicated i.e. when some clicks the "Upload Picture" button i just want an image to appear so looking to run a simple function onClick but struggling to know how to make the function work. So onClick i require the following html added:
PHP Code:
|
Add the image to the page and use css display: none to hide the image. Set an onsubmit event for the form that would then switch the css for the image to display: block or whatever you want it to be to show the image.
|
Thanks - I'm halfway there. I've added the image:
PHP Code:
PHP Code:
|
I would suggest separate the javascript and the html:
javascript Code:
that is a very crude javascript example (not even tested). It does not takes into consideration other factors. I would suggest to use a framework like jQuery. |
I cant get this to work. Think i need to go read a javascript book. So i still have the img in the html then hidden using the css as detailed above. I assume i can just cut and paste the script section above into my html and it should work or am i missing something?
The form remains unchanged apart from the img: PHP Code:
|
to test the code I posted (or any javascript code) you can put it anywhere in the html document, I will suggest right before the <body> tag closes. Oh an I forgot to return true; so it will be something like this.
javascript Code:
(assuming the jQuery js file has already been included) javascript Code:
|
Can i debug the code in firefox? It isnt working i.e. nothing is happening:
PHP Code:
|
The <script> needs to go below the form. What is happening is the <script> is looking for elements on the page that don't exist yet because the browser hasn't gone that far down the page.
For debugging in Firefox you can use the Error Console or Firebug. The following code uses IDs to target the elements and changes the CSS instead of changing an attribute. Also the event name is all lowercase. HTML Code:
<style> |
Sorted. many thanks to all for your help.
|
I've been using the above javascript to hife css divs and then unhide on click etc. Quick question. Using the following javascript (as an example):
PHP Code:
|
It is because IDs are unique within an html document. If you want to target multiple elements use a class. I know the getElementsByClass method is fairly supported in modern browsers but to make sure you can have a safe-alternate in case the browser doesn't support it. Like the Dustin Diaz method:
javascript Code:
But if you are going to this lengths and if you are allowed by the project requirements, I would suggest you use jquery or another framework like mootools or any other. They make this stuff easier. For example in jquery: javascript Code:
|
Going to assume the browser supports it. Still having problems however when i try and unhide the class. Here's the main pieces of code:
PHP Code:
PHP Code:
By the way - many thanks for the help thus far Tony. |
so when somebody clicks the #editmypictureclick it will hide or unhide the .editmypictures element based on if it is hidden or not. Right now your code only displays as block. Just put a condition if it is none change it to block, if not, the other way:
javascript Code:
|
Looking to nothing more complicated than simply unhiding a lot of elements when the Edit div id is clicked. I've changed the function name to getElementByClassName:
PHP Code:
|
| All times are GMT. The time now is 09:22 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0