Implementation of post passing through URL parameters PHP/Javascript-PHP source code

Source: Internet
Author: User
We generally know that url-based parameters are transmitted in get mode, but we can also use post-based transmission, especially when some interfaces are used. This article lists the methods implemented using php and Javascript. We generally know that url-based parameters are transmitted in get mode, but we can also use post-based transmission, especially when some interfaces are used. This article lists the methods implemented using php and Javascript.

Script ec (2); script

PHP implementation

In the form of interfaces and post transmission, data is transmitted in the form of strings, and the returned data is encapsulated in JSON.

Then we can start various tests.

Share the final method:

Define the capture function:

function http_post_data($url, $data_string) {    $ch = curl_init();    curl_setopt($ch, CURLOPT_POST, 1);    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);    curl_setopt($ch, CURLOPT_HTTPHEADER, array(        'Content-Type: application/json; charset=utf-8',        'Content-Length: ' . strlen($data_string))    );    ob_start();    curl_exec($ch);    $return_content = ob_get_contents();    ob_end_clean();    $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);    return array($return_code, $return_content);}



Then the method is:

$ Url = "path"; $ data = array (); // array $ data = json_encode ($ data); // convert it to a string list ($ return_code, $ return_content) = http_post_data ($ url, $ data); $ return_content = json_decode ($ return_content, 1); var_dump ($ return_content); // output the returned results.





Window. open url parameter post Transmission

Recently, when I was working on a web project, I encountered the function of passing parameters across pages, that is, to bring the content of the current page to a new subform. The previous method was to pass an id, then read the database content in the new window. Although it is not very troublesome, if the content is saved in the database, it cannot be implemented only when the draft status is taken. Users often think it is a bug. Consider using the get method to pass, serialize all the required content, and then use the url to transfer it, it looks very bloated, and the length of the get transfer content is limited. As a result, the post method is used for transmission. The problem is that the open method cannot be used to set the request method. Generally, the post on a webpage is implemented through form. If you only simulate the form submission method, the parameters in the open method that can set the form attributes cannot be used. Finally, we try to combine the two methods to set the target of form to the same value as the name parameter of open, and automatically identify the content through the browser to post the content to the new window.

It is interesting that the onsubmit event cannot be triggered directly by calling the form submit method. The help document must be manually triggered. Otherwise, the page can only be refreshed without opening a new window. Only one parameter content is passed in the code, and multiple parameters can be passed.
The Code is as follows:

function openPostWindow(url,name,data) {     var tempForm = document.createElement("form");     tempForm.id="tempForm1";     tempForm.method="post";     tempForm.action=url;     tempForm.target=name;     var hideInput = document.createElement("input");     hideInput.type="hidden";     hideInput.name= "content"    hideInput.value= data;    tempForm.appendChild(hideInput);      tempForm.attachEvent("onsubmit",function(){ openWindow(name); });    document.body.appendChild(tempForm);     tempForm.fireEvent("onsubmit");    tempForm.submit();    document.body.removeChild(tempForm);    return false;}function openWindow(name) {     window.open('about:blank',name,'height=400, width=400, top=0, left=0, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=yes, status=yes');  } 《script》





Call:

'); "> This is called;



Note that if the red part does not exist This kind of page loss occurs because the href and onclick of The Link coexist,

The requested link uses the tag, and href and onclick events are simultaneously written on. When A user clicks the link to tag A, The onclick part and href part are executed when the object A is triggered.

The solution is:

Write the onclick event in href: href = "javascript: openPostWindow (...) "
There is also a solution: Test
In this way, the href part is ignored, which is useful for passing this through onclick or avoiding object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.