Using post method in javascript - post

I would like to send text inside textarea to php using post method. I created a button, and I can extract the text from the textarea into a variable.
How can I pass the variable to php using POST and NOT using ajax or other libraries? Is there a simple way?
If not, how does ajax do it?

Do you have to use javascript? If you don't want AJAX, then why not just use a standard HTML form submission? You can setup everything using javascript to build the HTML form and the browser will do the submission for you?
JavaScript post request like a form submit
If it must be javascript, then it might be simplest to use JQuery to make an AJAX call.
http://www.w3schools.com/jquery/ajax_post.asp

Related

Fill the entire form based on selection (onchange)

I have an ingredient form and I want the user to type only the name and then the carb, protein and fat get automatically loaded. I know I need an ajax request but I don't know the path to learn how to accomplish this.
Can anyone gives an example or tell me where I can find it?
Learn Jquery too. Jquery is a widely used Javascript library and you will find a lot of help too.
Coming back to the question, $ajax method will be used to send an ajax request from your .js file. You will find a lot of documentation on it.
If you want to send an ajax request using rails helpers and views use the
remote : true
option. it will process your request as a XmlHttpRequest one. You can check the type of request by naigating to your browsers console and see the type of request. In case of Ajax request it will be XmlHttpRequest type.
This is not a thorough answer since you will find a lot of documentation on internet. But just to get you started.

Submit webform via URL only?

I'm not really sure this belongs here, so instead of downvoting just lemme know if so and I'll quickly move it on.
Anyway, there is a website that has a search page, that when hitting the search button it doesn't include the search query in the URL.
After searching for something, the page is redirected to ssearch.asp, but as said, the query isn't there.
My question is if there is a way to submit the search values solely via URL.
I was wondering if there is a way to fake the search-submit button and post the search term
via URL according to form field names.
The name of the input box is search, so I tried this URL: http://www.torec.net/ssearch.asp?search=query, but it doesn't work, the server returns:
server error.
Just to be clear, I'm not looking for a server-side solution, and actually nor for a HTML solution, I just want to be able to paste a plain old URL in my browsers address bar and be there.
Is this possible?
Update
This link doesn't work:
http://www.torec.net/ssearch.asp?search=dark&page=1
While this one does:
http://www.torec.net/ssearch.asp?search=dark&page=2
Any way to bypass this?
Sometimes servers conflate GET and POST parameters, as in PHP $_REQUEST hash. However, normally they are separate - and a server that expects its parameters in multipart/form-data might not look at URL at all. In such a case, as it seems to be here, you have to construct a POST request. On the client side you can do it through AJAX or through constructing and posting a form; on the server side, you can use curl, or a library. You did not say what you want to use it for (and where you want to use it), so you just get the general answer, I'm afraid.
EDIT: Here is the JavaScript semi-solution. You have to already be on some page (i.e. can't use it on _blank), and I'm not sure if it works on all browsers.
javascript:d=document;f=d.createElement("form");h=d.createElement("input");f.setAttribute("method","post");f.setAttribute("enctype","application/x-www-form-urlencoded");f.setAttribute("action","http://www.torec.net/ssearch.asp");h.setAttribute("type","hidden");h.setAttribute("name","search");h.setAttribute("value","query");f.appendChild(h);d.body.appendChild(f);f.submit();
Edit: It is not possible to create a link directly to the first page. However, you can easily send a user to the first page by by creating a form:
<form id="postForm" method="post" action="http://www.example.com/search">
<input type="text" name="search" value="q">
</form>
And then submitting the form whenever the user clicks a psuedo-link:
document.getElementById("postForm").submit();
This can also be done by typing JavaScript code into the address bar:
javascript:a=document.createElement("form");a.method="POST";a.action="http://www.torec.net/‌​ssearch.asp?search=dark&page=2";i=document.createElement("input");i.name="search";i.value="q";a.appendChild(inpu‌​t);a.submit();

Get POST response data within Greasemonkey script

I want to get reponse data send by a Jetty Server via POST within a Greasemonkey script.
New data is send every few seconds.
How can I directly access the data, that is displayed in Firebug "Network" tab as response data?
(I tried to get the data with solutions provided here that make use of jQuery and ajaxSuccess, but that did not work. And I am not sure if the POST data is returned from an jQuery ajax request. So is there a way to directly access the POST "stream").
The best way depends highly on details that you've not (yet) provided:
Exactly how does the page send and receive the information. Is it really form posts, or is it AJAX? If AJAX, is jQuery involved? Pages can post without AJAX; are iframes involved?
Link to the target page.
What does the page do with the information? Does it update displays?
What is your ultimate reason for wanting the information? Most of the time, it's easier not to try and intercept -- and parse -- AJAX. Usually, monitoring the DOM tree is much easier and more robust way of getting what you want.
If the page displays the data, you want, to the user, the best way to get it is shown in "How can I detect AJAX node insertion...".
If you really want to intercept the data:
Intercepting jQuery AJAX from Greasemonkey.
Intercepting any target-page AJAX from Greasemonkey.
Intercepting AJAX, bare bones.
If the page is submitting forms, creating iframes, etc., POST DETAILS. But in general, you would replace the javascript function that is receiving (and/or sending) the data.

Server side rendering for dynamic pages with PhantomJS on Ruby On Rails

I have a WebPage made that is 90% Javascript. All of the WebSite is rendered dynamically.
I want this content to be rendered by the server as well so that Google can crawl and index all of my content and links.
I know that in order not to get banned by google, the content of the dynamic page and the server rendered page must be almost identical.
I don't want to code two different pages (one from the client with Handlebars and one from the server with ERB in this case).
So I thought of PhantomJS. What I want is that when I get the _escaped_fragment_ param from google, I open the page without that with PhantomJS and I render this to HTML from PhantomJS and return that from the server to Google. This way, I don't have to create two different pages for anything.
I know that I can use Handlebars for Server Side templating as well, but I'd have to code everything twice anyway.
Does anybody know how to accomplish this with PhantomJS? Is there any other way for not repeating the Logic and code Twice and have Google index the Site?
Thanks!!!
Yes you can.
Add the following to the of your Javascript intensive page
<meta name="fragment" content="!">
When the Google bot finds this tag, it will issue a new http GET request. This time, it will add ?_escaped_fragment_= to your URL.
So if your web page with Javascript is located at:
www.mysite.com/mypage
Google will issue a new GET using the following URL:
www.mysite.com/mypage?_escaped_fragment_=
In your Ruby GET handler, you simply call PhantomJs with the unescaped URL (just do a string replace). In your PhantomJs javascript code, wait for the page to render and then then extract the HTML using regular javascript and return it back to your Ruby GET handler where you will simply respond to the GET with the HTML text string.
In this way you do not have to write your code twice. The solution is generic and will snapshot anything.

How do I make an non-ajax POST request with YUI javascript library?

If I wanted to do a GET request I could just use document.location = "what-ever-I-want". But how do I do that with YUI?
I was told that I can't do it with YUI 2 Connection Manager and afaik I really can not. Then I was told that I can do it with YUI 3, but in trying to do so I've not found the answer and I'm thinking I really can't.
Please notice that I DON'T want to use Ajax, but a regular post.
I need this to make a link send a POST request, instead of GET.
[b]I know I can work around creating a form with JS and I'm doing that right now, but isn't there a more cleaner way?[/b]
Create a form making sure to set
method="post"
then insert it into the document, get a reference to it & call
.submit()
on it.
Tivac answer is the way to go.
I've discussed about this with him and some other people on #yui channel on freenode and yes, what he said is what we should be doing.
I've decided to open a feature request ticket for this to be implemented on YUI. You can check it out at http://yuilibrary.com/projects/yuilibrary/ticket/2528278
This might be of interest: JavaScript post request like a form submit

Resources