我需要创建一个动态行添加和删除功能。当它结束时,我需要将它们保存到我的数据库中。我尝试了很多东西,但都失败了。因为当我添加一行时,我的函数自动完成功能不起作用,然后我的列表框的功能将无法工作。
我的添加行功能:
// --------------AJOUTER LIGNE A DIMANCHE0000000000
$(function() {
$("button#addNode").click(function() {
var lambda = document.createElement('div');
document.getElementById('name').appendChild(lambda)
var rowNum = 0;
function addRow(frm) {
rowNum ++;
var row = '<p id="rowNum'+rowNum+'"> Item name: <input type="text" name="name[]" value="'+frm.add_name.value+'"> Item client[]: <input type="text" name="client1[]" size="4" value="'+frm.client1.value+'"><input type="button" value="Remove" onclick="removeRow('+rowNum+');" > </p>';
jQuery('#itemRows').append(row);
frm.client1.value = '';
frm.add_name.value = '';
var monInput = document.getElementById('add_name');
var monElement = document.createElement('div');
monInput.parent.insertBefore(monElement, monInput.nextElementSibling);
}
function removeRow(rnum) {
jQuery('#rowNum'+rnum).remove();
}
这是我星期日的结构和输入
</br>
<table>
<!-- en tete du tableau -->
<tr>
<td>
Date/Jour
</td>
<td>
# de Projet
</td>
<td>
Client
</td>
<td>
Courte description du projet
</td>
<td>
Description de la tâche effectué
</td>
<td>
Lieu
</td>
<td>
Tache
</td>
<td>
Total
</td>
<tr>
<tr>
<td>
<!--------------------------------- JOURNÉE DE DIMANCHE --------------------------->
<b>Dimanche</b> </br><?php echo $date1 ?>
</td>
<!-- numéro de projet du dimanche -->
<td>
<span id="numpro" >
<form method="post" action="" onsubmit="return false;">
<input type="text" id="name" name="add_name"onkeypress="returnhandleEnter(event, this, 'task');"/>
<?php
if($result!=false && mysqli_num_rows($result)>0)
{
while($product = mysqli_fetch_array($result)): ?>
<p id="oldRow<?=$product['id']?>"> <input type="text" name="name<?=$product['id']?>" value="<?=$product['name']?>" /> </p>
<?php endwhile;
}
?>
</span>
<!-- Bouton ajouter une rangée -->
<input onclick="addRow(this.form);" type="button" value="+" />
</form>
</td>
<!-- client du dimanche -->
<td>
<span id="proclient">
<input type="text" name="client1" size="12" class = "client1" id ="client1" disabled />
</span>
</td>
<!-- description du projet de dimanche -->
<td>
<span id="prodesc">
<input type="text" name="desc1" size="30" id ="desc1" class "desc" disabled />
</span>
</td>
<!-- ddescription de la tache du dimanche -->
<td>
<span id="protache">
<textarea rows="1" cols="20" name="taskDesc1" id ="task1" class "task"> </textarea>
</span>
</td>
<!-- lieu pour dimanche -->
<td>
<span id="prolieu">
<input type="text" name="prolieu1" size="10" id ="lieu1" class "lieu">
</span>
</td>
<!-- tache -->
<td>
<span id="tache">
<!-- <input type="text" name="tache" size="30" id="tache"class= "tache" /> -->
<!-- début section combobox tache avec tool tip -->
<label title="Select your state"> <select title="Select your state" id="state" name="state">
<?php
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo ' <option title="';
echo $row['tacName'];
echo '">';
echo $row['tacId'];
echo '</option>'."\n";
$task = array();
//echo '<option value="'.$row['tacName'].'">'.$row['tacId'].'</option>'."\n";
}
?>
</select>
</label>
<!-- Fin section cobobox tache avec tool tip -->
</span>
</td>
<!-- calculter le temps pour le diamnche -->
<td>
<span id="calculTemps">
<input type="number" name="tempsd" size="30" id="temps1"class= "temps" min= "0" max="24" value="0" />
</span>
</td>
这是我与DATABASE的连接
// Connect to the DB
$link = mysqli_connect("localhost","root","","testlp") or die("Error " . mysqli_error($link));
// store in the DB
if(!empty($_POST['ok']))
{
// first delete the records marked for deletion. Why? Because we don't want to process them in the code below
if( !empty($_POST['delete_ids']) and is_array($_POST['delete_ids']))
foreach($_POST['delete_ids'] as $id)
{
$sql = "DELETE FROM recherche WHERE id=$id";
$link->query($sql);
}
}
// adding new recherche
if(!empty($_POST['name']))
{
foreach($_POST['name'] as $name)
{
//escape special characters from inputed "name" to prevent SQL injection.
$sql = "INSERT INTO recherche (name) VALUES ('".mysqli_real_escape_string($link,$name)."')";
$link->query($sql);
}
}
// select existing recherche here
$sql="SELECT * FROM recherche ORDER BY id";
$result = $link->query($sql);
include("Log.php");
// include("dynamic-form-fields.html.php");
//chercher pour la colone tache
$pdo = new PDO("localhost","root","","testlp");
#Set Error Mode to ERRMODE_EXCEPTION.
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('Select * from taches');
$stmt->execute();
这几乎是我的所有代码;这部分是最难的部分。