繁体   English   中英

如何在不使用表单标签的情况下将值提交到另一页[JAVASCRIPT]

[英]how to submit value to another page without using form tag [JAVASCRIPT]

大家都可以帮我吗? 如何在不使用表单标签(method = post)的情况下将值提交到另一页,所以只能使用javascript。

function runScript(e) {
   if(e.keyCode==13){
      var text=document.getElementById('edtsearch').value
      // bla bla must be filled with function to submit value
   }
   return false
}

使用jQuery库:

您可以使用jquery POST请求。

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

没有图书馆,只需简单的JAVASCRIPT。

window.location = "http://www.page.com"; //this get another page

form=document.getElementById('formName'); //this fills the form
        form.target='_blank';
        form.action='whatever.html';
        form.submit();
        form.action='whatever.html';
        form.target='';

这是纯JS中最小的AJAX实现:

function ajaxRequest(){
    var hr = new XMLHttpRequest();

    function ajaxResponse(){
        try {
            if(hr.readyState === 4){
                if(hr.status === 200){
                    // do something with hr.responseText
                } else {
                    alert('There was a problem with the request.');
                }
            }
        } catch(er){}
    }

    hr.onreadystatechange = ajaxResponse;
    hr.open('post', 'http://targeturl.com/targetfile.php'); 
    hr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    hr.send('variable1=value1&variable2=value2&variable3=value3');
}

并在服务器上的targetfile.php中:

$response = '';
$var1 = '';
if ( !empty( $_POST['variable1'] ) ) {
    $var1 = $_POST['variable1'];
}
$var2 = '';
if ( !empty( $_POST['variable2'] ) ) {
    $var1 = $_POST['variable2'];
}
$var3 = '';
if ( !empty( $_POST['variable3'] ) ) {
    $var1 = $_POST['variable3'];
}
// do something
echo $response;
exit;

这是一个可以解释这实际上是什么的资源(强烈建议阅读): https : //developer.mozilla.org/en-US/docs/AJAX/Getting_Started

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM