php 如何在php中动态创建文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4397238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:55:56  来源:igfitidea点击:

how to create textbox dynamically in php

php

提问by hunter

hay, i have two textboxes i.e book_name and author and a button that save the data to database. my question is it possible to create textbox dynamically i.e when user click on add another book button two textboxes generated and when he click on save button both of rows(data of 4 texboxes) saved into database. Is it possible in php...? (in phpmyadmin) we can save multiple records in a single table on clicking at save button any help would be greatly appriciated

干草,我有两个文本框,即 book_name 和 author 以及一个将数据保存到数据库的按钮。我的问题是可以动态创建文本框,即当用户单击添加另一本书按钮时生成两个文本框,当他单击保存按钮时,两行(4 个文本框的数据)都保存到数据库中。是否可以在 php 中...?(在 phpmyadmin 中)我们可以通过单击保存按钮将多条记录保存在一个表中,任何帮助都会非常有用

采纳答案by wouter

Not with PHP, but with Javascript. PHP is a server-side language, all the code is processed before your browser gets the HTML.

不是用 PHP,而是用 Javascript。PHP 是一种服务器端语言,在浏览器获取 HTML 之前处理所有代码。

Try using this code:

尝试使用此代码:

<html>
<head>
<title>Dynamic Form</title>
<script language="javascript">
var i = 1;
function changeIt()
{

my_div.innerHTML = my_div.innerHTML +"<br><input type='text' name='mytext'+ i>"
i++;
}
</script>
</head>
<body>

<form name="form" action="post" method="">
<input type="text" name=t1>
<input type="button" value="test" onClick="changeIt()">
<div id="my_div"></div>

</body>

回答by Hacker

u need to add new textboxes using javascript and build ur sql based on the number of text boxes being added.

您需要使用 javascript 添加新的文本框并根据添加的文本框的数量构建您的 sql。

回答by Dan Grossman

If you want things to happen on a webpage without a new page load occurring, you must use JavaScript, which runs in the browser.

如果您希望在不加载新页面的情况下在网页上进行操作,则必须使用在浏览器中运行的 JavaScript。

Your button that creates new textboxes will need to be attached to a JavaScript event you write to do so.

创建新文本框的按钮需要附加到您编写的 JavaScript 事件中。

Your PHP script will have to be updated to look for these new inputs and save them the same way it saved the first one.

您的 PHP 脚本必须更新以查找这些新输入并以与保存第一个相同的方式保存它们。

回答by Caner

It is possible, but you need to use javascript. Check thisfor an example.

这是可能的,但您需要使用 javascript。检查一个例子。

PHP is run on the server side, you have to do this on the browser side.

PHP 在服务器端运行,您必须在浏览器端执行此操作。

回答by Chandresh M

you can do that with the use of JavaScript.

你可以使用 JavaScript 做到这一点。

try below code:

试试下面的代码:

function addElement() {
  var ni = document.getElementById('myDiv');
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = 'my'+num+'Div';
  newdiv.setAttribute('id',divIdName);
  newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
  ni.appendChild(newdiv);
}

Thanks.

谢谢。