选择列表中的信息并使用PHP添加到数据库中

选择列表中的信息并使用PHP添加到数据库中,php,html,Php,Html,我有一张可供选择的清单(见图) 用户选择一本书,点击“Ajouter”,它将被添加到我的数据库中。 如果用户单击第一个按钮“ajouter”,我如何获取数据 以下是生成html的代码: <? function generateHTMLView($xmlBook) { $date = time(); $owner = $_SESSION['user']; echo(' <div class="row"> <div cl

我有一张可供选择的清单(见图)

用户选择一本书,点击“Ajouter”,它将被添加到我的数据库中。 如果用户单击第一个按钮“ajouter”,我如何获取数据

以下是生成html的代码:

<?
function generateHTMLView($xmlBook)
{
    $date = time();
    $owner = $_SESSION['user'];
    echo('
        <div class="row">
        <div class="col-lg-12">
        <table class="table" id="table">
            <thead>
            <tr>
                <th>Titre</th>
                <th>Editeur</th>
                <th>Auteur</th>
                <th>ISBN</th>
            </tr>
            </thead>
            <tbody>
            ');
    $id=0;
    foreach ($xmlBook->data as $item) {
        $title = $item->title;
        $lengthTitle=strlen($title);
        if($lengthTitle >30){
            $numberToDelete='-'.$lengthTitle+30;
            $titleTemp=mb_substr($title,0,$numberToDelete).'...';
        } else {
            $titleTemp=$title;
        }
        $publisher = $item->publisher_name;
        foreach ($item->author_data as $authorData) {
            $author = $authorData->name;
        }

            $pages = $item->physical_description_text;
            $resume = $item->summary;
            $resUn = addcslashes($resume, ",'-");
            $isbn13 = $item->isbn13;
            $languageB = $item->language;
            $this->consctructArrayBooks($id,$title,$isbn13,$publisher,$author,$pages,$languageB,$date,$owner,$resume);
            $id++;
            $_POST['id']=$title;
            echo('
              <form method="POST" >
                 <tr>
                    <td>' . $titleTemp . '</td>
                    <td>' . $publisher . '</td>
                    <td>' . $author . '</td>
                    <td>' . $isbn13 . '</td>
                    <td><button type="submit">Ajouter</button></td>
                   </tr>
                   </form>
                   ');
      }
      echo ('
        </tbody>
           </table>
           <hr>
           </div>
           </div>
          ');
}
数据作为$item){
$title=$item->title;
$lengthTitle=strlen($title);
如果($lengthTitle>30){
$numberToDelete='-'。$lengthTitle+30;
$titleTemp=mb_substr($title,0,$numberToDelete)。“…”;
}否则{
$titleTemp=$title;
}
$publisher=$item->publisher\u name;
foreach($item->author\u数据为$authorData){
$author=$authorData->name;
}
$pages=$item->physical\u description\u text;
$resume=$item->summary;
$resUn=addcslashes($resume,“,”-”);
$isbn13=$item->isbn13;
$languageB=$item->language;
$this->consctructArrayBooks($id、$title、$isbn13、$publisher、$author、$pages、$languageB、$date、$owner、$resume);
$id++;
$\u POST['id']=$title;
回声('
“.$titleTemp。”
“.$publisher。”
“.$作者。”
“.$isbn13。”
阿约特
');
}
回声('

'); }
如果您将“Ajouter”按钮设置为按项目编号索引的数组,例如

<input type="submit" value="Ajouter" name="ajouter[1]" />
<input type="submit" value="Ajouter" name="ajouter[2]" />
<input type="submit" value="Ajouter" name="ajouter[3]" />


然后,您的服务器代码就可以遍历数组来确定按下了哪一个。

以下是为您编写的5个步骤

第1步:

在循环中,您已经声明了一个类和id

<td><button class="triggerClass" id="<?php echo $item->id?>">Ajouter</button></td>
第五步:

在php脚本中,获取值并将其存储在数据库中

yourScript.php
中,只需获取
$\u POST['id']
并通过实现简单的插入操作将其存储到数据库中


就是这样:)希望这能帮助你

我不知道如何知道用户想插入哪本书。
那么,我们怎么知道?很高兴看到你的更新,那么你为什么不能编写按钮点击操作?我已经编辑了问题。好的,那么你应该编写代码来检测按钮被按下的时间@SulthanAllaudeen我在html代码中使用了一个表单。我只是不知道怎样才能得到数组的值。这对我真的很有帮助,我要测试它。我正在努力让你发给我的代码正常工作。POST请求已发送,但我不知道如何在php代码中获取它。如果我在我的页面中执行
echo$\u POST['id']
,什么都不会发生。也许你能帮助我理解如何做到这一点,这是我第一次使用AjaxSure,你能说一下控制台中的输出是什么吗POST数据是发送的,但控制台中没有输出,当我执行
echo$\u POST['id'时出错这是正常的,因为id没有初始化,但当我点击“Ajouter”时,什么都没发生。那么你能先初始化然后回显吗?
var yourId = $(this).attr('id');
var request = $.ajax({
  url: "yourScript.php",
  method: "POST",
  data: { id : yourId },
  dataType: "html"
});