views:

196

answers:

5

I had a similar question posted here a few hours ago, just now I got the answer that I should look into using AJAX to do this. Since I want to complete this part of the site today I can't afford to learn AJAX from the basics to do this now.. This shouldn't be something difficult and I thougt that I would be able to do this but I'm not skilled enough...

I have a form, when you click submit, it posts to twitter.com/statuses/update.xml and I need to be able to do so without being redirected there.

Is there an easy way to do this or do I need to learn AJAX?

Thankfull for any answer at all..!

edit:

I'm using this to submit:

$(function() {  
    $("#skikka").click(function() {  
        var dendar = 
            "http://" + 
            $("#usernam").val() + ":" + $("#passwo").val() +
            "@twitter.com/statuses/update.xml";

        $("#formen").attr("action", dendar);
        $("#formen").submit();
        alert(dendar);
        return false;

    });  
});
A: 

You could use type="button" instead of type="submit".

Danten
Updated my question.. now you'll see why I can't
Noor
A: 

Just redirect back to your page in your server-side script. What language are you using?

Aaron Mc Adam
I'm only using jQuery, javascript and simple html. The XML I'm posting to is on twitters servers, I can't tell the XML to redirect me back to my site..
Noor
A: 
// post via ajax
$.post(dender, $("#formen").serialize());

Full Example:

$(function() {
$("#skikka").click(function() {
var dendar = "http://" + $("#usernam").val() + ":" + $("#passwo").val() + "@twitter.com/statuses/update.xml";

// post via ajax
$.post(dender, $("#formen").serialize()); 

alert(dendar); return false;

});
});
climbage
I'll try to figure out how to use this, looks like JohnFxs respond to, reading the API now!
Noor
You're going to need to use proxy running on your own server for that... you can't use XHR across domains and I'm sure your code is not running on twitter.com.
Matti Virkkunen
I'm trying to rationalize the full example,so far I've understod that the .serialize function is there to take the inputs one by one from the form and post them, the alert is there to alert and the return false is there to stop everything?I tested it out and the script gives me alert but I don't think the post is getting through to the XML.
Noor
@Noor: Read my comment. Also read http://en.wikipedia.org/wiki/Same_origin_policy
Matti Virkkunen
Matti, I understand what you're saying to a level, but not quite fully. Is there really no simple way to tell my form to send information to twitters xml and then just show me a little alert telling me that it went through successfully.. I'm trying really hard to understand you!Thanks, all of you for your efforts.
Noor
Oh.. security reasons.. I think I simply need to find some other script that lets me update twitter statuses. The thing is that this is pretty handy, 3 input boxes, username, password and the tweet.. Couldn't be easier. I'll just have to start over. Thanks!
Noor
@Matti Just a follow up Q, there reason for not being able to do this cross-server post is because it's through AJAX, correct? because I was able to do it with simple HTTP, it was meant for that..Thanks
Noor
XMLHttpRequest (XHR) is subject to same-origin policy, but normal HTML forms are not, if that's what you meant
Matti Virkkunen
Matti is right. $.post uses an XHR.
climbage
+2  A: 

Here is a quick example using JQuery, which is probably the easiest way to do AJAX without investing too much time learning it. Although it sounds like you'd have to learn a whole other technology (Jquery) it really isn't that hard. Mostly it just involves adding one more javascript include file reference to the page.

    $.post(dendar , $("#formen").serialize());

More Details on AJAX with Jquery here.

Since you are posting back to a different domain, you will have to do a little bit of extra work. Check out this article on using JSONP against twitter.

Also, here is a JQUERY plugin specifically for working against twitter: jTwitter

JohnFx
Thanks, reading it right now!
Noor
A: 

If you must do it that way ( via click event and without just having the server send a redirect ), you can add an onSubmit event to the form that handles actual submission via ajax and then cancels the event propagation.

Event.observe( 
  $('searchfield').form , 
  'submit' , 
  function( e ){ 
    alert('No YOU' ); 
    /* secretly submit using ajax and clear form */ ; 
    Event.stop( e ) ;
  });

Something like that.

Michael Speer