Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以编程方式按下按钮以在Javascript的另一页上提交表单_Javascript_Php_Html - Fatal编程技术网

以编程方式按下按钮以在Javascript的另一页上提交表单

以编程方式按下按钮以在Javascript的另一页上提交表单,javascript,php,html,Javascript,Php,Html,请原谅这个基本问题,我对Javascript和web开发一般来说都是新手。我想在我的网站的一个页面上使用脚本来编程。我按下按钮在网站的另一部分提交表单,发出POST请求。我必须访问的html如下: html <form action="thing.jsp" method="post"> // Beginning of form ... <input type="submit" id="submit" value="Do something"> // Button code

请原谅这个基本问题,我对Javascript和web开发一般来说都是新手。我想在我的网站的一个页面上使用脚本来编程。我按下按钮在网站的另一部分提交表单,发出
POST
请求。我必须访问的
html
如下:

html

<form action="thing.jsp" method="post"> // Beginning of form
...
<input type="submit" id="submit" value="Do something"> // Button code
...
</form>
通过在线阅读,我怀疑我可能只需要获得
参数的正确值?虽然如果有其他方法可以达到相同的结果(例如,只发送
POST
请求,而不按按钮执行任何操作),我非常乐意接受

提前感谢您的时间和智慧。

使用JS库,您只需发送一封电子邮件即可。然后可以在PHP中使用保存该值的
$\u GET['key']
获取该值

$(函数(){
$(“#唯一id btn”)。单击(函数(){
$.get('file.php',{key:$('unique-id-input').val()}).done(函数(响应){
警报(响应);
});
});
});


单击我
您不需要使用ajax,只需使用以下命令:

<input type="button" value="GO" id="buttonId" />
<script>
function go() {
    document.location.href = 'http://google.com';
}

document.getElementById('buttonId').onclick = go;
</script>

函数go(){
document.location.href=http://google.com';
}
document.getElementById('buttonId')。onclick=go;

请注意,按钮类型应该是“button”,而不是“submit”

,这看起来像我需要的,但它给了我一个
未捕获的类型错误:无法在window.onload(myPage.html:20)中将属性“onclick”设置为null
。知道是什么引起的吗?可能getElementById找不到元素。检查元素是否具有id属性,以及getElementById拼写是否正确。显然,“buttonId”应该被元素的id替换。检查是否有拼写错误
id=“submit”
就像我给出的示例代码一样,这就是我将代码中的
按钮nid
替换为的内容。还尝试将整个内容包装在
window.onload=function(){}。我只是想再次确认一下,我没有把自己解释得太糟糕:我要找的是一个脚本,一个网站的一页,它会在另一页上提交表单。我知道这个问题。将按钮类型更改为“按钮”而不是“提交”。请注意,我说的是更改type属性,而不是id。这就回答了我的问题。不过,如果我能在不改变输入类型的情况下管理它会更好。我会看看我能不能想出一个。
<input type="button" value="GO" id="buttonId" />
<script>
function go() {
    document.location.href = 'http://google.com';
}

document.getElementById('buttonId').onclick = go;
</script>