如果由js创建,如何在php中获取文本框值
我使用下面的代码生成多个文本框标签点击,它的工作正常,多个文本框创建不同的名称。我想在php端获取textbox的值,并为每个textbox运行insert查询。但我不知道如何在php中使用它。这是我的代码plz帮助如果由js创建,如何在php中获取文本框值,php,Php,我使用下面的代码生成多个文本框标签点击,它的工作正常,多个文本框创建不同的名称。我想在php端获取textbox的值,并为每个textbox运行insert查询。但我不知道如何在php中使用它。这是我的代码plz帮助 <html> <head> <title> </title> <script> var index = 1; function insertRow(){ var table=document
<html>
<head>
<title>
</title>
<script>
var index = 1;
function insertRow(){
var table=document.getElementById("rep1");
var row=table.insertRow(table.rows.length);
var cell1=row.insertCell(0);
var t1=document.createElement("input");
t1.id = "txtName"+index;
t1.name = "txtName"+index;
cell1.appendChild(t1);
index++;
}
</script>
</head>
<body>
<label id="btnAdd" style=" width: 10px;" class="button-add" onclick="insertRow();">add</label>
<table id="rep1">
</table>
</body>
</html>
PHP代码:
<?php
if(isset($_POST["submit"]))
{
$t1=$_POST['name'];// i am confused here how to take value and run query for all
?>
<?php
if(isset($_POST["submit"]))
{
$t1=$_POST['txtName'.$index];
?>
客户端代码
<?php
if(isset($_POST["submit"]))
{
$t2=$_POST['txtName'];
$query_parts = array();
$qu = "INSERT INTO try VALUES";
foreach($t2 as $val)
{
$query_parts[] = "('', '" . $val . "')";
}
$qu .= implode(',', $query_parts);
$res=mysqli_query($con,$qu);
if(!$res) { echo("Error description: " . mysqli_error($con)); }
}
?>
必须对多个名称字段使用数组
<script>
var index = 1;
function insertRow(){
var table=document.getElementById("rep1");
var row=table.insertRow(table.rows.length);
var cell1=row.insertCell(0);
var t1=document.createElement("input");
t1.id = "txtName"+index;
t1.name = "txtName[]"; //Use array of names not id
cell1.appendChild(t1);
index++;
}
</script>
上面的代码不会阻止数据。您可以使用prepared语句在PHP中阻止SQL注入
使用此代码。它会帮助你的
<?php
print_r($_POST['txtName']); // Here you can get you all values.
?>
<html>
<head>
<title>
</title>
<script>
var index = 1;
function insertRow(){
var table=document.getElementById("rep1");
var row=table.insertRow(table.rows.length);
var cell1=row.insertCell(0);
var t1=document.createElement("input");
t1.id = "txtName"+index;
t1.name = "txtName[]"; // Use array in textbox name..
cell1.appendChild(t1);
index++;
}
</script>
</head>
<body>
<form method="POST" action="" >
<label id="btnAdd" style=" width: 10px;" class="button-add" onclick="insertRow();">add</label>
<table id="rep1">
</table>
<input type="submit">
</form>
</body>
</html>
var指数=1;
函数insertRow(){
var table=document.getElementById(“rep1”);
var row=table.insertRow(table.rows.length);
var cell1=行插入单元格(0);
var t1=document.createElement(“输入”);
t1.id=“txtName”+索引;
t1.name=“txtName[]”;//在文本框名称中使用数组。。
细胞1.附属物(t1);
索引++;
}
添加
为此,您还必须存储总索引值(该值应指定您创建输入字段的次数)。之后,您的php代码应如下所示
PHP代码:
<?php
if(isset($_POST["submit"]))
{
$t1=$_POST['name'];// i am confused here how to take value and run query for all
?>
<?php
if(isset($_POST["submit"]))
{
$t1=$_POST['txtName'.$index];
?>
其中索引变量将显示循环中当前索引值的总输入字段长度请查看@JigarShah我看到了链接。但只有2个数组元素。我想将foreach用于7个阵列或多个阵列。好的,我已经共享了链接,这样您就可以开始更改您的code@JigarShah感谢您提供的链接,您建议它适用于多个阵列。您建议的是有效的,但我无法通过查询发送值。请您帮助说明您是如何发送这些值的@shalinif(isset($u POST[“submit”]){$t2=$u POST['txtName'];$qu=“INSERT INTO
try
values(''''$t2');$res=mysqli\u查询($con,$qu);if($res){echo'警报(“数据成功保存”);}否则{echo”错误:“.mysqli\u错误($con);退出();echo'alert(“保存数据时出错。请检查数据并重试!!”;}}好的,您正在插入整个数组,请使用foreach或其他循环插入多个数据。数组不能存储在数据库中。我使用此代码可以工作。但是如果有两个不同的文本框,那么如何为每个循环执行它们。如果使用:foreach($t2作为$value){$qu=“INSERT-INTOtry
VALUES(''''.$value')”;$res=mysqli_query($con,$qu);}使用未定义的常量索引在php代码中创建隐藏字段。创建一个变量以在javascript insertRow()中存储总索引值之后,将该变量值放入隐藏的输入字段值中。现在,您可以在PHP代码中获取该隐藏字段值,通过此操作,您可以在$t1=$\u POST['txtName'][index]之前应用循环;此答案将不起作用。如果您查看OP的代码(t1.name=“txtName”+index;
),这些字段将被称为txtName1、txtName2、txtName3等
而不是数组。我编辑了我的答案和评论,如下所示-在php代码中创建隐藏字段。创建一个变量以在javascript insertRow()中存储总索引值之后,将该变量值放入隐藏的输入字段值中。现在,您可以在PHP代码中获取该隐藏字段值,这样您就可以在$t1=$\u POST['txtName.$index]之前应用循环;你应该解释为什么OP应该尝试这个。你改变了什么?它解决了什么问题?@MagnusEriksson这是建议