[英]Javascript added form element not available via $_POST
I am using radio buttons in a form to hide/show a member id field in a contact form. 我在表单中使用单选按钮来隐藏/显示联系人表单中的会员ID字段。 Problem is when javascript changes the hidden id field included in html which is set to a value of "None", the field is no longer available via post, even if javascript changes it back to the original when the no radio box is checked.
问题是,当javascript将html中包含的隐藏ID字段更改为“无”时,即使选中了“否”单选框,即使javascript将其更改回原始字段,该字段也无法通过发布获得。 Please help, I am new to javascript, thanks.
请帮助,我是javascript新手,谢谢。
Relevant Form Code: 相关表格代码:
echo '<tr><td>IVAO Member:</td><td><input type="radio" name="ivao" value="yes" onchange="ivaoVID()">Yes | <input type="radio" name="ivao" value="no" onchange="ivaoVID()" checked>No</td></tr>';
echo '<tr id="ivaovid"><input type="hidden" name="vid" id="vid" value="None"></tr>';
Javascript: Javascript:
function ivaoVID() {
var selected = document.getElementsByName("ivao");
var row = document.getElementById("ivaovid");
for(i = 0; i <selected.length; i++) {
if (selected[i].checked == true) {
if (selected[i].value == "yes") {
row.innerHTML = "<td>IVAO VID:</td><td><input type='text' name='vid' id='vid'></td>";
} else {
row.innerHTML ="<input type='hidden' name='vid' id='vid' value='None'>";
}
}
}
}
Action php file: 动作php文件:
public function submit(){
$model = new contactModel;
$name = isset($_POST['name']) ? $_POST['name']:$this->registry->getData('fname').' '.$this->registry->getData('lname');
$vid = isset($_POST['vid']) ? $_POST['vid']:$this->registry->getData('vid');
$email = isset($_POST['email']) ? $_POST['email']:$this->registry->getData('email');
$message = $_POST['message'];
echo $name . '<br> ' . $vid . '<br>' . $email . '<br>' . $message;
}
Overwriting the HTML like that messes up the form for some reason. 出于某种原因覆盖这样的HTML会使表单混乱。 No idea why exactly, but I see the same thing in both Chrome and IE.
不知道为什么会这样,但是我在Chrome和IE中都看到了同样的事情。
If you go into the console and enter: 如果进入控制台并输入:
document.forms[0].vid
When you do it before changing the radio button you get a result, and when you do it after you get "undefined". 如果在更改单选按钮之前执行此操作,则会得到结果,而在获得“未定义”之后执行该结果。 The especially weird thing about it is that if you enter this in the console:
尤其奇怪的是,如果您在控制台中输入以下内容:
document.getElementById("vid")
You get a result in both cases. 在两种情况下都可以得到结果。
One possible alternative would be to hide and show the "vid" text input instead of trying to overwrite it with a hidden input. 一种可能的选择是隐藏和显示“ vid”文本输入,而不是尝试用隐藏的输入覆盖它。 To do that, you would change your PHP to write the text input version of the "ivaovid" row, then change your JavaScript to this:
为此,您将更改PHP以编写“ ivaovid”行的文本输入版本,然后将JavaScript更改为此:
function ivaoVID() {
var selected = document.getElementsByName("ivao");
var row = document.getElementById("ivaovid");
var vid = document.getElementById("vid");
for(i = 0; i <selected.length; i++) {
if (selected[i].checked == true) {
if (selected[i].value == "yes") {
row.style.display = "";
vid.value = "";
} else {
row.style.display = "none";
vid.value = "None";
}
}
}
}
Another option would be to hide and show the text input without modifying the value and ignore the value in your PHP post handler depending on the value of $_POST['ivao']
. 另一种选择是隐藏和显示文本输入而不修改值,并根据
$_POST['ivao']
的值忽略PHP后期处理程序中的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.