我已经使用java脚本(内部Html)创建了该字段,当我使用$_POST['field name']
时,它显示 undefined index 。
以下是该字段的代码:
function showfield(name){
if(name=='Masters'){
document.getElementById('div1').innerHtml = '<label>College/University</label><input class="form-control " type="text" id="course" name="uni" placeholder="College/University" /> <br> <label>Course</label><input class="form-control " type="text" name="crse" placeholder="Course"/><br>';
}
}
我想在用户单击“提交”后将类(class)和大学的值获取到下一页
最佳答案
您需要将该输入插入到具有相应提交按钮的 HTML 表单元素中。因此,如果您已经有一些包含提交按钮的表单,请将此新输入插入到该表单中。如果您没有任何表单元素,只需将提交按钮包装到其上,并确保在该表单中插入此新输入。
这是一个小例子(HTML 表单)。
<form id="awesome-form" method="POST" action="path/to/php/script.php">
<div id="place-for-input"></div>
<button type="submit" name="awesome-form-submit">Submit</button>
</form>
这是您的 JavaScript。
function showfield(name){
if(name=='Masters'){
document.getElementById('place-for-input').innerHtml = '<label>College/University</label><input class="form-control " type="text" id="course" name="uni" placeholder="College/University" /> <br> <label>Course</label><input class="form-control " type="text" name="crse" placeholder="Course"/><br>';
}
}
在 PHP 中,您将像这样访问此信息。
if (isset($_POST["awesome-form-submit"])) {
$collegeOrUniversity = $_POST["uni"];
}
顺便说一句,更好的方法是使用 insertAdjacentHTML 将新输入直接插入到表单中。
document.getElementById('awesome-form').insertAdjacentHTML("afterbegin", '<label>College/University</label><input class="form-control " type="text" id="course" name="uni" placeholder="College/University" /> <br> <label>Course</label><input class="form-control " type="text" name="crse" placeholder="Course"/><br>');
关于javascript - 我怎样才能在php中获取表单字段的值(字段是从javascript创建的)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52134958/