Thursday, March 28, 2013

Disable Enter Key from Submit on Forms

Know:
Forms, Javascript, patience.

NOTHING and I mean NOTHING has frustrated me more than this annoyance.

By default, ALL forms treat <ENTER> the same as clicking a button.  This means the moment you press enter the form is submitted.  What, but what if I have a form with multiple fields and every one needs to be entered BEFORE submission.
Sure, you could disable the enter key but wow, that's fun for the user.  Pressing tab and not enter gets old REAL FAST.  So...what do I do.

Step 1:
Disable the form.

This is pretty easy and requires NO coding.

<Form id='myform' name='myform action='dosomething.php' >

Nothing special but we need to stop submit.  We will add ONE line.

onsubmit='return false'

Now...

<form id='myform' name='myform' action='dosomething.php' onsubmit='return false'>

This essentially is taking advantage of Javascript.  If you want to hook up an event to onsubmit you can do that.  Returning false will keep you in the form.  This is great for validating text, etc.  For now though, we're just going to return false.  OH NO.  Now I will NEVER have the form submit right?

WRONG!!!!

This is the key.
Create a button that will submit the form.  We're going to add a PAINLESS function in Javascript to submit the form.  Really, it's quick.

First the function.  You can either add it to the top of your html, OR, add it to your javascript library.  For this example, we'll just add it to a script.

<script>
function SubmitForm ('IdOfForm')  // that's the ID of the form.  Handy for submitting
{
    var SelectedForm = document.getElementById (IDOfForm);
     SelectedForm.submit ();

}

</script>

OK so now you have the script.  BUT WAIT???? Where do I put it?  Well an easy solution is with a button.

<!-- this is all one line but is wrapping on this blogspot --!>
<input type='button' class='your class (optional) value='Save' onsubmit='SubmitForm(this.id); return false;"></input>

The return false is VERY important.  Take it out and you'll have the return key submitting your form.  Try it.  Works great for me and have fun.

Too many quotes

KNOW:
php, Javascript

Ever came across this problem?

echo ('<input id='fred' ...etc)

The problem is that we have an embedded quote but you say -- no problem right?

echo ("<input id='fred' ...etc)

So far so good.  Here we have the id of the tag set to 'fred'.  We use single quotes for fred and double quotes for input...but!


echo ("<input id='fred' onclick='DoSomething ('Bob');'...mess!

The problem is that you only have two normal layers of quotes.  The single can be embedded in the double but the double...we don't have a triple.  What we do have is the escape.

The escape lets us embed as many quotes as we want with the use of the \ character.

\" means to tell the processor that we want a double quote as it.  So when PHP processes the code we will see a double quote.  This won't work for single quotes by the way.  Don't do \' and expect good things to happen.

Now...

echo (\"<input id=\"fred\" onclick=\"DoSomething (\"Bob\")\" >\" );

I agree, this takes a bit of getting used to but the advantage is that you don't have to deal with running out of characters for quotes.  It works this way.

The first quote is always \"
The second quote is always \"
The third quote is always \"

You can do as many as you want without pulling hairs.  Messy -- ya...but it works.

Javascript and the this statement

What to know:
PHP, basic Javascript.


Often times in php, I'm generating lists of objects -- specifically html objects -- and I will include a Javascript link to process the information.  That, in itself, is not my favorite option but php is server and Javascript is client (Browser).  This relationship can be a headache but here are some tips to aid the pain.

First things first.  Javascript does provide a painless way to extract information from any object -- this.  The 'this' object gives you all the tools you need to gather anything you want about an object and pass it along to a procedure. 

Example
<input id='fred' value='bob' onclick='DoSomething (this) >this is nice</input>

This is your gateway to freedom.  The 'this'.  Don't believe me?

In your function

function DoSomething (Value)
{


}


You can extract:
The ID of the object, the value of the object and the text past the object

Now, let's see how

function DoSomething (Value)
{

   alert (Value.id);   // get the ID of the object or in this case 'fred'
   alert (Value.value) // get the value of the object or in this case 'bob'
   alert (Value.innerHTML) // get the text or in this case 'this is nice'
}

Now if you're worried about extracting information about an object, you need worry no more.

I'm no expert on Javascript and try to avoid it when I can but I can't deny its usefulness.



 

Monday, March 11, 2013

VIM Registers and Macros

TO KNOW: Basic VIM beyond :w of course.
TO KNOW: how to copy/paste/delete text.
TO WANT: Make your life much easier.


Registers in VIM may be a lousy term.  A better term might be buffers.  Vim stores a variety of buffers and uses them for a variety of tasks. Copy and paste AND Macros are just two examples.

Now, if you've never seen a vim register, type this:

1) Press [ESC] ...as many times as needed to ensure you are in command mode.
2) Type ':' {minus the quotes} and reg ( :reg) or (:registers).

If you did everything correctly you should see a messy list on the screen.  Pay CLOSE attention to the letters and/or numbers on the LEFT side of the screen.

"0  (blah blah)
"1 (blah blah)
"2 (blah blah)
...
"A (blah blah)

Each of these "0, "1, etc. represents a register -- a letter representing a buffer.  So what right? Well hold on OK!

In case you are wondering...

0 (yank register), 
1 to 9 (shifting delete registers), 
_ (like /dev/null, this is a black hole), 
" (default register, hence the Ctrl-R, "), 
- (small delete register), 
/ (the search pattern register), 
: (last command register), 
a to z for your own use (capitalized A to Z are for appending to corresponding registers).


Well they are important. Really, I get it, but these really are USEFUL!

COPY A BUFFER
First, let's get the contents into our document.

The command to get into the buffers are the double quote (") vs the command mode colon. (:)  Ya, it's strange but really...we're talking VIM here.

The sequence is...

(double quote) (name of buffer) (p for paste)

You may recall that p is pasting.

So, for example we want to access the k register -- assuming we see it in reg.

"ky  (" for the register access, k for k register, and p for paste)

If you did everything right, you will see the contents of the register in your document.

SO WHAT!!!!!!!!!!!!!!
Here is why.  This is ALL leading to Macros and it took me some time to gather these tidbits.  They are all out there...in pieces of documentation.

MACROS!!!!!!!!!!
Anyway:

Let's say you have a set of keystrokes you type OVER and OVER and OVER again.  BORING!
Macro it!!!

The command for macros (assuming command mode of course (press [ESC]) is

q (name of register or  buffer but everyone calls them registers)

So to record a macro in the b register type:

qb (NO COLON!!!!)
and you should see recording

EVERYTHING and I mean everything you type is now stuffed into the register.   Now as you type commands it will be recorded but STOP...I have to stop recording endless commands!

No problem

type
q to end the macro.  YA!

RUNNING A MACRO
:@ (letter of register)  (now you can use that colon)

So in our case...

:@b

But...it go away when me leave VIM so BAD RIGHT?

Yes and we need better English for me to understand your question so let's try again

But...it goes away when we leave VIM?

That's much better.

SAVING A MACRO FOREVER
Here's how.  Using our example of qb for the 'b' buffer and assuming we have the macro.  We need to get it into a document.

type

"bp

That command (not British Petroleum by the way) pastes the b register into the document. (Remember we did that earlier)

Now, load up .vimrc.  That's a hidden file that stores commands that vim can access.  Essentially, it's a huge configuration file.  If you don't have one -- shame on you -- make one and store it in your home directory on the linux workstation.

NOW...

Copy all those messy commands into your .vimrc...and it will work right?

ALMOST

Do this:

let @b = '  (your text in between the quotes -- single quotes mind you)'

Remember the text between quotes is the text from the register

(for this you would type "bp" to paste it in RIGHT BETWEEN the quotes.  This way you make NO typing mistakes

(save the file .vimrc but you know that right :)

NOW when you run vim...ya...it's remembered.  Isn't that AWESOME.

OH YA

to access the macro remember to type:

@b  (where b is the name of the register and @ is the command to run the macro.