Javascript 使用php在数据输入期间防止重复输入

Javascript 使用php在数据输入期间防止重复输入,javascript,php,Javascript,Php,我想防止用户使用php输入重复记录(至少警告他们)。以下代码与组合框(Txt\u Gen)中的相同代码相呼应,只是输入了不同的值并警告重复输入。表中的行是动态生成的。所以它使用相同的ID,这是问题的根本原因。请帮忙 代码如下: <script type="text/javascript"> // Add rows to table // function addRow1(tableID) { var table = document.getElementById(tableID)

我想防止用户使用php输入重复记录(至少警告他们)。以下代码与组合框(Txt\u Gen)中的相同代码相呼应,只是输入了不同的值并警告重复输入。表中的行是动态生成的。所以它使用相同的ID,这是问题的根本原因。请帮忙

代码如下:

<script type="text/javascript">

// Add rows to table //
function addRow1(tableID) 
{
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}

var gensel = []; // array //

//getting the value of the combobox //

function myFunction1(form) 
{
var gval = document.getElementById('Txt_Gen').value; 

//var gval = form.Txt_Gen.options[form.Txt_Gen.options.selectedIndex];

var a = gensel.indexOf(gval);
//alert(a);//
if ( a==-1 ) {
gensel.push(gval);
}  else  {
alert('Duplicate Entry');  // Displays Error Message every time //
}
}

</script> 

</head>

<body>

<!-- DB Query for populating the combo box -->
<?Php $genquery= " SELECT GENCODE, DESCRIPTION FROM xxxx";?>

<!-- HTML Part -->
<div id="container">

<form action="indent_dml.php?t=R" class="register" method="POST">
<div id="Layer_Ind_Container" >

<!-- Btn_Add is used to add row to the table -->

<input type="button" id="Btn_Add" name="Btn_Add" value="Add Record"  onClick="addRow1('Table_IndNew');" /> 
<input type="submit" id="Btn_Submit" name="Btn_Submit" value="Submit" >
</div>

<table id="Table_IndNew">
<tr>
<td>
    <select name="Txt_Gen[]" size="1" id="Txt_Gen"  onchange="myFunction1(this.form)">
    <option value=''>--- Select ---</option>
    <?php $listemp = $dbo->query($genquery);
    while ($rowemp = $listemp->fetch(PDO::FETCH_ASSOC))
    {?>
    <option value="<?php echo $rowemp['GENCODE']?>"><?php echo $rowemp['DESCRIPTION']?></option>
    <?php    }    ?>
    </select>
</td>

<!-- Some other elements are also present in the table such as quantity etc -->

</tr>
</table>
</form>
</div>
</body>
<!-- HTML part Ends -->

//向表中添加行//
函数addRow1(tableID)
{
var table=document.getElementById(tableID);
var rowCount=table.rows.length;
var row=table.insertRow(rowCount);
var colCount=table.rows[0].cells.length;
对于(var i=0;i

我个人会建议使用PDO(php数据对象)来实现这一点,但是我不确定您在这里实际想要实现什么,因为您没有给出太多的上下文?这将连接到某种数据库吗?您是否成功连接到它?如果是这样,您可以使用PDO进行简单计数以检查数据库

$duplicate = "SELECT COUNT(table) AS num FROM table WHERE [whatever you don't 
want to be duplicated] = :btn-add"; // where you bind the input from HTML field
  $stmt = $pdo->prepare($duplicate);

  $stmt->bindValue(':btn-add', $duplicate);

  $stmt->execute();


  $row = $stmt->fetch(PDO::FETCH_ASSOC);


  if($row['num'] > 0){
      die('That table exists already!');
  }

然而,这在很大程度上取决于您想要实现什么?这就是我解释您想要做什么的方式,我建议您在此时阅读PDO。请尝试更具体地回答您的问题,因为很难回答一些写得不清楚的问题

我正在使用p连接到数据库它是一种类似excel表格的表格输入表单,用户从数据库中填充的组合框中选择数据。新记录(组合框)只能在按下“添加记录”按钮后才能添加到表单中。完成所有条目后,用户才提交表单。因此,您想检查他们何时从下拉菜单中选择内容吗?我假定您已连接到数据库,并且知道如何操作PDO对象?是。用户将从从数据库动态填充。那么你能不能不把我上面回答的原则应用到它上?