简体   繁体   English

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

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

Hii Everyone can help me ? 大家都可以帮我吗? How to submit value to another page without using form tag (method=post), so only using 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
}

With jquery library: 使用jQuery库:

You can use jquery POST request. 您可以使用jquery POST请求。

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

WITHOUT LIBRARY, JUST PLAIN JAVASCRIPT. 没有图书馆,只需简单的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='';

Here is a minimal AJAX implementation in pure JS: 这是纯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');
}

And on the server in targetfile.php: 并在服务器上的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;

And here is a resource that will explain what this actually is (highly recommended reading): https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started 这是一个可以解释这实际上是什么的资源(强烈建议阅读): https : //developer.mozilla.org/en-US/docs/AJAX/Getting_Started

暂无
暂无

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

相关问题 使用javascript在没有提交按钮的情况下显示输入值到另一个页面 - Displaying input value without a submit button to another page using javascript 使用javascript / ajax在另一个页面上提交表单 - Submit form on another page using javascript/ajax 提交带有锚标签的表单而无需刷新页面 - Submit form with an anchor tag without refreshing page 10秒后使用javascript和表单提交重定向到另一个页面 - Redirect to another page after 10 seconds using javascript and a form submit 在不使用 PHP 的情况下单击表单中的提交(输入类型)后,如何使用 javascript 在页面上显示消息 - How to display a message on page using javascript after clicking on submit(input type) in a form without using PHP 如何在不使用控制器的情况下将表单数据提交并显示到Rails的另一页上? - How to submit and display form data onto another page in rails without using controllers? 在不使用url变量的情况下将表单值传递给另一页上的另一表单(javascript) - pass a form value to another form on a different page without using url variables (javascript) 使用提交按钮提交表单时,如何将javascript变量发送到php中的另一页 - How to send javascript variable to another page in php when form is submitted using submit button 如何防止导航到其他页面而不提交 javascript 中的表单? - How to prevent navigating to other page without submit the form in javascript? 如何使用标签将值传递到另一页? - How to pass value using a tag to another page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM