TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-02-2011, 05:51 PM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default 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:
<form name="addimage" method="post" enctype="multipart/form-data"  action="">
<
table class="forms">
    <
tr>
    <
td colspan="2">
    <
h3>Add Picture</h3>
    </
td>
    </
tr>

    </
tr>
    <
tr>
    <
td colspan="2"><span class='red'>
        </
span></td>
    </
tr>
    <
tr>
    <
td>
        <
label for="alt">Image Text*</label>

    </
td>
    <
td>
        <
input type='text' name='alt' id='alt' size='20' maxlength='100'>
    </
td>
    </
tr>
    <
tr>
    <
td><input type="file" name="image"></td>
     <
td><input name="Submit" type="submit" value="Upload Picture"></td>
    </
tr>

</
table>
</
form
captainmerton is offline  
Reply With Quote
Old 03-02-2011, 07:23 PM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

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.
tony is offline  
Reply With Quote
Old 03-02-2011, 08:08 PM   #3 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

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:
    <tr>
    <
td colspan="2">
        <
img src='images/pleasewait.gif'>
    </
td>
    </
tr
captainmerton is offline  
Reply With Quote
Old 03-08-2011, 05:22 PM   #4 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

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.
__________________
Eric
wGEric is offline  
Reply With Quote
Old 03-08-2011, 05:52 PM   #5 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Thanks - I'm halfway there. I've added the image:

PHP Code:
<img class="pleasewait" src="images/pleasewait.gif"
and the CSS:

PHP Code:
img.pleasewait {displaynone;} 
And the image is not shown. What should my form onSubmit javascript be?
captainmerton is offline  
Reply With Quote
Old 03-08-2011, 06:08 PM   #6 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

I would suggest separate the javascript and the html:
javascript Code:
<script>
document.getElementsByName("addimage").onSubmit = function (e) {
    document.getElementsByClassName("pleasewait")
        .setAttribute("style","display: block;");
};
</script>

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.
tony is offline  
Reply With Quote
Old 03-08-2011, 06:40 PM   #7 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

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:
<form name="addimage" method="post" enctype="multipart/form-data"  action="">
<
table class="forms">
    <
tr>
    <
td colspan="2">
    <
h3>Add Picture</h3>
    </
td>
    </
tr>
    <
tr>
    <
td colspan="2"><img class="pleasewait" src="images/pleasewait.gif"></td>
    </
tr>
    <
tr>
    <
td>
        <
label for="alt">Image Text*</label>
    </
td>
    <
td>
        <
input type='text' name='alt' id='alt' size='20' maxlength='100'>
    </
td>
    </
tr>
    <
tr>
    <
td><input type="file" name="image"></td>
     <
td><input name="Submit" type="submit" value="Upload Picture"></td>
    </
tr>
</
table>
</
form
captainmerton is offline  
Reply With Quote
Old 03-08-2011, 07:39 PM   #8 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

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:
<script>
document.getElementsByName("addimage").onSubmit = function (e) {
    document.getElementsByClassName("pleasewait")
        .setAttribute("style","display: block;");
    return true;
};
</script>
</body>
In jQuery it would be something like:
(assuming the jQuery js file has already been included)
javascript Code:
<script>
$('form[name="addimage"]').live('submit', function () {
    $('.pleasewait').css('display','none');
    return true;
};
</script>
</body>
tony is offline  
Reply With Quote
Old 03-08-2011, 07:54 PM   #9 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Can i debug the code in firefox? It isnt working i.e. nothing is happening:

PHP Code:
<script>
document.getElementsByName("addimage").onSubmit = function (e) {
    document.getElementsByClassName("pleasewait")
        .setAttribute("style","display: block;");
    return true;
};
</script>
<form name="addimage" method="post" enctype="multipart/form-data"  action="">
<table class="forms">
    <tr>
    <td colspan="2">
    <h3>Add Picture</h3>
    </td>
    </tr>
    <tr>
    <td colspan="2"><img class="pleasewait" src="images/pleasewait.gif"><span class='red'>
    <?php print $view->getFeedback(); ?>
    </span></td>
    </tr>
    <tr>
    <td>
        <label for="alt">Image Text*</label>
    </td>
    <td>
        <input type='text' name='alt' id='alt' size='20' maxlength='100'>
    </td>
    </tr>
    <tr>
    <td><input type="file" name="image"></td>
     <td><input name="Submit" type="submit" value="Upload Picture"></td>
    </tr>
</table>
</form>
captainmerton is offline  
Reply With Quote
Old 03-09-2011, 04:57 PM   #10 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

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>
#pleasewait {
    display: none;
}
</style>
<form name="addimage" id="addimage" method="post" enctype="multipart/form-data"  action="">
<table class="forms">
    <tr>
    <td colspan="2">
    <h3>Add Picture</h3>
    </td>
    </tr>
    <tr>
    <td colspan="2"><img class="pleasewait" id="pleasewait" src="http://www.talkphp.com/images/pleasewait.gif"><span class='red'>
   <?php print $view->getFeedback(); ?> 
    </span></td>
    </tr>
    <tr>
    <td>
        <label for="alt">Image Text*</label>
    </td>
    <td>
        <input type='text' name='alt' id='alt' size='20' maxlength='100'>
    </td>
    </tr>
    <tr>
    <td><input type="file" name="image"></td>
     <td><input name="Submit" type="submit" value="Upload Picture"></td>
    </tr>
</table>
</form>
<script>
document.getElementById('addimage').onsubmit = function (e) {
    document.getElementById("pleasewait").style.display = 'block';
};
</script>
__________________
Eric
wGEric is offline  
Reply With Quote
Old 03-09-2011, 05:47 PM   #11 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Sorted. many thanks to all for your help.
captainmerton is offline  
Reply With Quote
Old 05-02-2011, 09:07 AM   #12 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

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:
<script>
document.getElementById('unhide').onclick = function (e) {
    
document.getElementById("hiddendiv").style.display 'block';
};
</script> 
...I've been trying to onclick reveal all the div ids hiddendiv. This works ok however it finds the first occurance of this then unhides it and doesnt do any other later occurances. Is there a way round this before i use the javascript to unhide all potential occurances i.e. hiddendiv1, hiddendiv2 etc?
captainmerton is offline  
Reply With Quote
Old 05-02-2011, 01:16 PM   #13 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

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:
function getElementsByClassName(node,classname) {
  if (node.getElementsByClassName) { // use native implementation if available
    return node.getElementsByClassName(classname);
  } else {
    return (function getElementsByClass(searchClass,node) {
        if ( node == null )
          node = document;
        var classElements = [],
            els = node.getElementsByTagName("*"),
            elsLen = els.length,
            pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;

        for (i = 0, j = 0; i < elsLen; i++) {
          if ( pattern.test(els[i].className) ) {
              classElements[j] = els[i];
              j++;
          }
        }
        return classElements;
    })(classname, node);
  }
}

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:
$('.unhide').live('click', function (e) {
    $('.hiddendiv').toggle();
});
tony is offline  
Reply With Quote
Old 05-03-2011, 05:56 PM   #14 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

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:
<div id='editmypicturesclick'>Edit</div>

<
span class='editmypictures'>Delete</span>

<
script>
document.getElementById('editmypicturesclick').onclick = function (e) {
    
document.getElementByClass('editmypictures').style.display 'block';
};
</script> 
And here's the CSS:

PHP Code:
.editmypictures {float:leftwidth:100%; displaynone;} 
When i change the CSS display style to Block then the Delete text appears. Its the onclick on the Edit text that doesnt seem to work. Any ideas?

By the way - many thanks for the help thus far Tony.
captainmerton is offline  
Reply With Quote
Old 05-03-2011, 06:25 PM   #15 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

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:
document.getElementById('editmypicturesclick').onclick = function (e) {
    var delete_el = document.getElementByClassName('editmypictures');
    delete_el.style.display = (delete_el.style.display == 'none') ? 'block' : 'none';
};
notice that the actual name of the method is getElementByClassName not getElementByClass at least from mozilla docs.
tony is offline  
Reply With Quote
Old 05-03-2011, 07:20 PM   #16 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

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:
<script>
document.getElementById('editmypicturesclick').onclick = function (e) {
    
document.getElementByClassName('editmypictures').style.display 'block';
};
</script> 
Still wont work. It works when i use this with DIV IDs but something about the way I'm doing it with a class is stopping it from working?
captainmerton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with php form kphigh Absolute Beginners 4 11-22-2010 08:20 PM
Form Validation Not Working :( Guezala Absolute Beginners 27 05-13-2009 08:28 PM
Form to submit to another form patmagpantay General 8 07-21-2008 07:42 AM
Button Tanax XHTML, HTML, CSS 28 01-15-2008 09:39 AM
Form Processing William Tips & Tricks 8 04-17-2005 03:24 PM


All times are GMT. The time now is 02:05 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design