简体   繁体   English

我如何在php中获取表单字段的值(字段是通过javascript创建的)?

[英]how can i get the value of the field of the form (field is created from javascript) in php?

I have created the field using the java script (inner Html) and when I use $_POST['field name'] it shows undefined index . 我已经使用Java脚本(内部HTML)创建了该字段,当我使用$_POST['field name']它显示了未定义的索引

Here is the code for the field: 这是该字段的代码:

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>';
    } 
}

I want to get the value of course and college to next page once the user clicks submit 用户单击提交后,我想将课程和大学的价值下一页

You need to insert that input in HTML form element with corresponding submit button. 您需要将该输入插入带有相应提交按钮的HTML表单元素中。 So if you already have some form that contains submit button insert this new input to that form. 因此,如果您已经有一些包含“提交”按钮的表单,请将此新输入插入该表单。 If you don't have any form element just wrap the submit button to it and make sure you insert this new input in that form. 如果您没有任何表单元素,只需将“提交”按钮包装到其上,并确保将新输入插入该表单中。

Here is small example (HTML form). 这是一个小示例(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>

And here is your JavaScript. 这是您的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>';
    } 
}

And in PHP you will access this information like this. 在PHP中,您将像这样访问此信息。

if (isset($_POST["awesome-form-submit"])) {
    $collegeOrUniversity = $_POST["uni"];
}

By the way, a better path of doing this would be to insert that new input directly to the form with insertAdjacentHTML. 顺便说一句,执行此操作的更好方法是使用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>');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用javascript / jQuery获取表单字段值以求和另一个字段值? - How can I get form field values to sum up to another field value using javascript/jQuery? 如何使用 vanilla JavaScript 从动态创建的输入字段中获取数据? - How can I get data from dynamically created input field using vanilla JavaScript? 如何在不提交表单的情况下获取javascript中输入字段的值? - How can I get the value of an input field in javascript without submitting a form? 如何从 JavaScript 中的元素列表填充表单字段? - How can I populate a form field from a list of elements in JavaScript? 如何在 JavaScript 中获取检查的行输入字段值? - How can i get checked row input field value in JavaScript? 如何使用javascript从输入字段中获取多个值 - How can i get multiple value from an input field using javascript 如何使用JavaScript从div的输入字段中获取输入值 - How can I get my input value from input field in a div using JavaScript 如何从 Javascript 设置 Angular formcontrol 字段的值? - How can I set the value of an Angular formcontrol field from Javascript? 如何使用javascript插入和获取动态创建的输入字段的值? - How to insert and get the value of a dynamically created input field using javascript? 如何为表单中的文本字段设置初始值? 此值是从javascript函数生成的。 没有botton的情况下如何定义事件? - How to set a initial value for a text field in a form? This value is generated from a javascript function. How can i define the event without botton?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM