2

I have this HTML form that I need to do some processing before submitting:

<form action="http://appid.appspot.com/api/foo" id="contact-form" method="post"
name="contact-form">
    <fieldset>
        <label><input name="email" onblur=
        "if(this.value=='') this.value='Email'" onfocus=
        "if(this.value =='Email' ) this.value=''" value="Email"></label>
        <label><input name="subject" onblur=
        "if(this.value=='') this.value='Subject'" onfocus=
        "if(this.value =='Subject' ) this.value=''" value="Subject"></label> 
        <textarea name="message" onblur=
        "if(this.value=='') this.value='Message'" onfocus=
        "if(this.value =='Message' ) this.value=''">
Message
</textarea>

        <div class="buttons">
            <a href="#" onclick=
            "document.getElementById('contact-form').reset()">Clear</a>
            <a href="#" onclick=
            "document.getElementById('contact-form').submit()">Send</a>
        </div>
    </fieldset>
</form>

Basically, what I need to do are:

  • When the submit button is clicked, a popup window will appear to indicate that the message have been submitted (by checking the server response) anyway,

  • I need to copy the value of a specific form input, say the value of the input element. And then prepend, say, the email to the actual message, such that the form "message" will be for example "Email: [email protected] Message: Hello world" <-- this will be the value of the input element with name="message" just before sending the form fields to the api

Update:

What I mean in my question is that I want to migrate this basic HTML form based POST into an Ajax call if that would be better, so I can easily achieve the goals I have outlined above.

4
  • and where exactly are you getting a cross domain error? Commented Apr 22, 2013 at 16:49
  • @AkshaySinghal actually there is no cross domain error, this form actually can send to a different domain. However not sure if this will work on all browsers. Commented Apr 22, 2013 at 17:09
  • im sorry but i dont understand what your question is. Commented Apr 22, 2013 at 17:17
  • @AkshaySinghal I mean, I want to migrate this code into Ajax code Commented Apr 22, 2013 at 17:42

1 Answer 1

2

You want to use the form's onsubmit.

I changed your html (and especially inline javascript) a bit (for clarity):

<form action="http://appid.appspot.com/api/foo" id="contact-form" method="post"
name="contact-form" onsubmit="return doStuff() ? true:false;">
    <fieldset>
        <input name="email" value="Email"
             onblur="if(this.value===''){this.value=this.defaultValue}" 
            onfocus="if(this.value===this.defaultValue){this.value=''}" > <br>
        <input name="subject" value="Subject"
             onblur="if(this.value===''){this.value=this.defaultValue}" 
            onfocus="if(this.value===this.defaultValue){this.value=''}" > <br>
        <textarea name="message" 
                onblur="if(this.value===''){this.value=this.defaultValue}" 
               onfocus="if(this.value===this.defaultValue){this.value=''}"
                      >Message</textarea>
        <div class="buttons">
            <a href="#" onclick=
            "document.getElementById('contact-form').reset()">Clear</a>
            <a href="#" onclick=
            "if(doStuff()){document.getElementById('contact-form').submit();}">Send</a>
        </div>
    </fieldset>
</form>

As you can see the autofill/autoclear on the input-elements now kind of repeats, so you could factor it out in a separate universal function.

Here is the javascript function that runs before the form actually submits:

function doStuff(){
    var eml=document.getElementsByName('email')[0].value;
        msg=document.getElementsByName('message')[0];
    msg.value = 'Email: ' + eml + ' Message: ' + msg.value;
    alert ('message has been submitted');
    return true;
}

Working JSFiddle here

Doing this cross-domain might get tricky if the other domain uses session-cookies and/or checks document referrer.

Update: I see you updated your question and now want to check and display the server-response in your message to?

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

4 Comments

even the message box don't show up when I try to do a quick Form send--when I try to do button submit...
What do you mean? I prepared the form for <input type="submit" value="send"> (instead of your current <a href="#" ...>), see fiddle v2
Actually if HTML form upload is better then i'd be better off from ajax,
I am quite sleepy so i'm not making a sense out of what i need to achieve... good thing it works now, now I can go to sleep...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.