开发者

Get POST response from a url and print response to page?

开发者 https://www.devze.com 2023-01-19 20:26 出处:网络
I\'m trying to get a POST response from a url and I can not get the response to print to my html page instead it just redirects me to the url in the action with the response.

I'm trying to get a POST response from a url and I can not get the response to print to my html page instead it just redirects me to the url in the action with the response. Is there a way to grab the response with html? php? Code of html page i'm using

<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<form 
 method="post" 
 action="http://poster.decaptcher.com/" 
 enctype="multipart/form-data">
 <input type="hidden" name="function"  value="login">
 <input type="text"   name="username"  value="client">
 <input type="text"   name="password"  value="qwerty">
 <input type="file"   name="upload">
 <input type="text"   name="uplo开发者_开发百科ad_to"   value="0">
 <input type="text"   name="upload_type" value="0">
 <input type="submit" value="Send">
</form>
</head><body></body></html>

Note: The url in the action will only show the response and nothing else is shown on the page.


Let's see if I can give this a try, because you seem to be a bit confused about how an HTML form works.

First and foremost, your website looks like so, correct?

<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<form 
 method="post" 
 action="http://poster.decaptcher.com/" 
 enctype="multipart/form-data">
 <input type="hidden" name="function"  value="login">
 <input type="text"   name="username"  value="client">
 <input type="text"   name="password"  value="qwerty">
 <input type="file"   name="upload">
 <input type="text"   name="upload_to"   value="0">
 <input type="text"   name="upload_type" value="0">
 <input type="submit" value="Send">
</form>
</head><body></body></html>

One thing to point out before we explain an HTML form, is that you have your form in the <head> of the webpage. Any element which is supposed to be seen by the user (or anything that you want to appear within the browser's main viewing area) should be in the <body>. Failure to do this puts the browser into a "quirks mode", where it actually doesn't know what you're talking about and it makes its best guess to try and build the website that it thinks you wanted. Mind you that modern browsers are very good guessers, but you should still re-write it as:

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form 
 method="post" 
 action="http://poster.decaptcher.com/" 
 enctype="multipart/form-data">
 <input type="hidden" name="function"  value="login">
 <input type="text"   name="username"  value="client">
 <input type="text"   name="password"  value="qwerty">
 <input type="file"   name="upload">
 <input type="text"   name="upload_to"   value="0">
 <input type="text"   name="upload_type" value="0">
 <input type="submit" value="Send">
</form>
</body>
</html>

As far as explaining the <form> tag... When you submit a form in HTML, it actually loads the other website. It doesn't secretly send data in the background, it will take you away from the page you're viewing and take you to the page that you are sending the data to. At first this may sound silly. Why should it take you away from the page you're viewing just to send the data to another website? If you wanted to be redirected after sending the data, you'd redirect them there after sending the data.

The reason it's done this way is to greatly simplify the HTTP protocol. Whenever you load any website, you send and HTTP request. This request contains butt-loads of information. Among this information is:

  • Your IP address
  • What browser you're using
  • The page you were last visiting
  • How you accessed this page (clicked a link or typed the URL into the address bar)
  • The page you want to view (is it index.html or mysite.html?)
  • Any cookies related to that server
  • Any POST information (extra information which the server may or may not have asked for)

Every time the server receives one of these requests, it looks at all of the information and decides what to do. Usually a server will just look at the page you want to view and send it to you. Sometimes the page you want to view will need some extra work before it's ready to show, though. For instance, if a page ends in .php then it will search through the page for <?php, and everything after that point will be executed as a script. Only the output of the script is sent to the person who requested the page, not the script itself.

If you were to send your POST information to a website, wait 10 minutes, THEN go to the website, it would have no way of remembering that it was you who sent the post information before or what information you sent. Web servers have a very short attention span. For that reason if you sent a form to log into a website, then waited 10 minutes, then tried to view a member's only page- it would forget that you were logged in. For this reason it sends you the page as you're submitting the form. It does it while it still remembers that you're logged in, before it has a chance to forget. There's a good chance that the page it sends you will include a cookie which you can use to remind the server you were logged in next time you request a page.

If this made sense, then you should understand what happens when you submit a form. It doesn't just take your information and give it to the server. It sends that information to the server as part of an entire request, then the server sends you back a webpage and your browser displays that webpage. There is really only one way to send data to a server without redirecting you to that server afterwards. There are multiple ways to do this trick, however. You have to send a "dummy request", requesting a webpage with certain POST data, but ignoring the webpage that's returned.

In your example, you wanted to send data to http://poster.decaptcher.com. To do this without redirecting the user to http://poster.decaptcher.com, your easiest solution would be to use javascript and AJAX. Javascript has certain functions that allow you to send an HTTP request without reloading the page, then you let the javascript determine what to do with the page that's returned.

This is generally used when you want to reload a part of a webpage without reloading the whole thing. For instance, if you have a chat program and you want to update the chat window without refreshing the entire page. The javascript would request a webpage which contains ONLY the new lines of chat, minus any <html>, <head>, or <body> tags. It then takes those lines and displays them in the chat window.

You can, however, use AJAX to request a page and then ignore what's returned instead of display it on the page. By doing this you will have sent the POST data but not redirect the user.

Another option is to send the request to a third website, which can then send its own dummy request. For instance, submit the form to a PHP page that you own. The PHP script can then tell your server to send a dummy request to http://poster.decaptcher.com and ignore the response, then you can send them a webpage containing whatever you want.

Now that I've described both of these processes in adequate detail, I'll leave it as an exercise to the reader to figure out exactly how to do these. =)


The page refresh on submitted form is the default behavior of HTML.

For people who need to display the response into the same page without refresh, they will want to use Ajax. Here is how it could be done with jQuery:

$('#the_form').submit(function (e) {
  e.preventDefault();
  the_form = $(this);
  $('#response_container').load(
    the_form.attr('action')
  , the_form.serialize()
  );
})


the action defines the redirect to that page. If you want to catch the response, make your own script and place it in between the two. This is a bad way of doing it though. We developers call it hack coding. lol.


Not quite sure what you want to do. If you want to show the POST content on the page, just do this:

print_r($_POST);

If you want to see what is getting POSTed to the action URL, and you don't have access to that URL, just use the HTTP Headers plugin for Firefox.


action should go to a PHP file belonging to you! ie - action="/ProcessMyForm.php"

On that file, simply use $_POST and those form elements are in there, indexed by name, in an associative array.

Also - it may have been accidental, but post parameters dont go up in the URL like get, they are "behind the scenes" (invisible to the user) and also capable of being far larger.

PS - if you want to go to that other site afterwards, use header("Redirect: other-website-here.com")


First of all, mention your question specifically. If you want to fetch data from a URL than you can't use the form method="post". If you want to fetch data from URL, you have to use method "get". Calling print_r($_GET) can be used to retrieve data from HTML page to controller page.

0

精彩评论

暂无评论...
验证码 换一张
取 消