我在<select
php/xhtml 页面上有一个元素。我没有使用提交按钮,而是尝试实现 Javascriptonchange()
和submit()
方法。我需要传递多个变量来弥补丢失的隐藏字段。onchange 和 submit 方法正在触发,但我无法让它触发同一页面上的 PHP isset。
<?php
if(isset($_POST['companykey'])){
$nowIveGot1 = $_POST['COMPANYKEY'];
$nowIveGot2 = $_POST['USERNAME'];
?>
<script language="javascript">
function copy(selectedValuePassed, selectedNamePassed, userIDPassed, formNamePassed) {
var formNameReceived = formNamePassed;
var userIDReceived = userIDPassed;
var selectedValueReceived = selectedValuePassed;
var selectedNameReceived = selectedNamePassed;
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "");
form.setAttribute("name", formNameReceived);
form.setAttribute("id", formNameReceived);
var valueSelected = document.createElement("input");
valueSelected.setAttribute("type", "hidden");
valueSelected.setAttribute("name", selectedNameReceived);
valueSelected.setAttribute("id", selectedNameReceived);
valueSelected.setAttribute("value", selectedValueReceived);
form.appendChild(valueSelected);
var passusername = document.createElement("input");
passusername.setAttribute("type", "hidden");
passusername.setAttribute("name", "USERNAME");
passusername.setAttribute("id", "USERNAME");
passusername.setAttribute("value", userIDReceived);
form.appendChild(passusername);
document.body.appendChild(form);
form.submit();
}
</script>
<select onchange="copy(this, 'COMPANYKEY','jarrett', 'companykey');">
<option value="Text1">Text 1</option>
<option value="Text2">Text 2</option>
</select>
已解决:为 valueSelected 和 passusername 创建一个父元素。将其命名为 isset 检查名称。