[英]HTML Submit form on dropdown change
Good morning! 早上好!
I know that this topic has been covered on SO before but I couldn't find the full answer to my specific situation below so I apologize if I missed the answer elsewhere. 我知道这个话题已经在SO上面了,但我找不到下面我具体情况的完整答案,所以如果我在其他地方错过了答案我会道歉。
Scope: 范围:
What I am trying to achieve is a drop down list (either done with pure HTML or java script, HTML is preferred) that can load a php file. 我想要实现的是一个可以加载php文件的下拉列表(使用纯HTML或java脚本,首选HTML)。 Inside that php file it should be able to access the selected drop down's value.
在该php文件中,它应该能够访问所选的下拉值。
What I have tried: 我尝试过的:
Method 1: 方法1:
I have used a method like this before: 我之前使用过这样的方法:
<form action="test.php" method="post">
<select name="dropdown" id="dropdown">
<option value="value1">Option 1</option>
<option value="value2">Option 2</option>
</select>
<input name="submitbutton" type="submit" value="submit" />
</form>
Then clicking on the submit button, the test.php page loads and I can get the value of the dropdown using $_POST("dropdown"). 然后单击提交按钮,加载test.php页面,我可以使用$ _POST(“下拉列表”)获取下拉列表的值。 This method works perfectly fine!
这种方法完美无缺! However I want to submit the form WITHOUT need of the button, simply using the drop down onchange event instead.
但是我想提交表单而不需要按钮,只需使用下拉onchange事件。
Method 2: 方法2:
I also tried using: 我也试过用:
<form>
<select name='dropdown' onchange='this.form.submit()'>
<option value="value1">Option 1</option>
<option value="value2">Option 2</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
which will reload the page onchange but not the test.php page.. Changing "this.form.submit()" to test.php doesn't work. 这将重新加载页面onchange而不是test.php页面。将“this.form.submit()”更改为test.php不起作用。
I feel like I am close with both but not quite there. 我觉得我和两个人都很亲密,但并不完全相同。 I hope what I am trying to achieve is explained well enough.
我希望我想要实现的目标得到了充分的解释。
Thank you for any responses ahead of time! 感谢您提前回复!
Add: 加:
<form action="test.php" method="post">
To method2 and it should work fine. 对于method2,它应该工作正常。
Give your form a name: 给你的表单一个名字:
<form name="form1" action="test.php">
....then your onchange would be: document.form1.submit();
....那么你的onchange将是:
document.form1.submit();
BTW, this has nothing to do with PHP, since it is 100% on the client. 顺便说一句,这与PHP无关,因为它在客户端上是100%。
You forgot to add ' action="test.php"
' and ' method="post"
' in your for tag.. So, your IInd method should like below: 你忘记在你的for标签中添加'
action="test.php"
'和' method="post"
'所以,你的IInd方法应该如下:
HTML Code: HTML代码:
<form action="test.php" method="post">
<select name='dropdown' onchange='this.form.submit()'>
<option value="value1">Option 1</option>
<option value="value2">Option 2</option>
</select>
<noscript><input type="submit" value="Submit"/></noscript>
</form>
and it will work and you can show your post data with print_r($_POST)
in test.php
. 它会工作,你可以在
test.php
中用print_r($_POST)
显示你的帖子数据。 It will show you dropdown
value :) 它会显示
dropdown
值:)
Try using with ajax 尝试使用ajax
jQuery: jQuery的:
$(document).on("change", "select#dropdown", function(){
$.ajax({
url: 'http://localhost/test/action.php',
type: "post",
data: $("#myform").serialize();,
success: function(response) {
$('select#dropdown').after("dropdown changed!");
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.