php PDO插入问题(Ajax下拉提取)

Stelio Kontos

(支持:Ubuntu-> phpstorm,apache服务器,PDO)

大家好,

我实际上是在尝试使用Ajax将下拉列表的选定值保存到我的PDO数据库中

问题:我确实设法将$ selectedOpt添加到数据库中,但是当我尝试对$ numLigne进行相同操作时,INSERT不再起作用...(我想获取所选选项所在行的值)

这是我的代码:

$file = fopen($fichier_txt, 'r+');
if ($file)
{
    $pattern_GC = '/N°.*\d{4}(\s?\s?[\w\s]*)(\d{5})\s?(\w+)\W+/isU';
    $fichier_txt_content = file_get_contents($fichier_txt);
    $regex_GC = preg_match_all($pattern_GC, $fichier_txt_content, $match_GC);

    // Match regroupant nom/prenom + adresse
    $match_un = explode(PHP_EOL, $match_GC[1][0]);
    $match_nom_prenom = $match_un[2];
    $match_adresse = $match_un[3];
    // Match CP
    $match_deux = $match_GC[2][0];
    // Match ville
    $match_trois = $match_GC[3][0];
    // On place ces matchs dans un tableau pour faciliter la création des DDL
    $opt = array(0 => $match_nom_prenom, 1 => $match_adresse,2 => $match_deux, 3 => $match_trois);
    $numLigne = array();
    $numLigne = array_keys($opt);
    $i = 0;

    ?>

    <!-- DEFINITION DES DROP-DOWN LISTS -->
    <html>
    <br /><br />
            <label>Nom :</label>
            <form method="POST">
                <select name="selectBox" class="drop" id="Combobox1">
                    <option selected hidden value="Fais ton choix !">Choisis le nom</option>
                <?php foreach($opt as $key => $val) {?>
                    <option value="<?= $val ?>"><?= $val ?></option>
                <?php } ?>
                </select>
            </form>
    <br /><br />
    </html>

    <html>
        <label>Prenom :</label>
        <form method="POST">
            <select name="selectBox" class="drop" id="Combobox2">
                <option selected hidden value="Fais ton choix !">Choisis le prenom</option>
                <?php foreach($opt as $key => $val) {?>
                    <option value="<?= $val ?>"><?= $val ?></option>
                <?php } ?>
            </select>
        </form>
        <br /><br />
    </html>
<?php }

在同一文件中,我有Ajax调用

<script>

// JS Script to save to DB + remove selected opt (jQuery + Ajax)
$(document).ready(function saveToDatabase(selectedValue)
{
    var select = selectedValue;
    select = $(this).serialize();
    var selectBoxes = $('select');

    selectBoxes.on('focusin', function ()
    {
        // Store the current value
        $(this).data('value', this.value);
    });

    selectBoxes.on('change', function ()
    {
        // POST to php script
        $.ajax
        ({
            type: 'POST',
            url: 'formulaire_2_test.php',
            data:{selected:this.value}
        });
        var oldValue = $(this).data('value');
        var newValue = this.value;

        // Remove selected option for children selectBoxes
        selectBoxes.filter(':not(#' + this.id + ')').each(function ()
        {
            var options = $(this).children();
            options.filter('[value="' + oldValue + '"]').show();

            if (newValue !== '')
            {
                options.filter('[value="' + newValue + '"]').hide();
            }
        });
    });
});

最后,我的PDO连接语句:(位于另一个php文件中)

<?php

include 'formulaire_de_test.php';
// Connexion à la BDD pour save les lignes du form (PDO)
try
{
    $PDO = new PDO('mysql:host=localhost;dbname=autofill_data', 'root', 'password');
}
catch (Exception $e)
{
    die('Erreur : ' . $e->getMessage());
}

// Récup de l'option selected et envoi vers la base
$selectedOpt = $_POST['selected'];
global $key;
$req = $PDO->prepare("INSERT INTO data_lines(idDistributeur, numLigne, libelle) VALUES(0, :numLigne, :selectedOpt)");
$req->bindParam(':selectedOpt', $selectedOpt);
$req->bindParam(':numLigne', $key);
$req->execute();
$data = $req->fetchAll();

我的问题出在我的“ numLigne”变量中,但我不明白确切的位置...

由于我从9:00 AM开始无法取得任何进展,因此将不胜感激,我们将不胜感激。

提前致谢,

问候,

Stelio KONTOS。

正如我提到的那样,只有在您告诉它时,$ key才会通过。如果将其添加到这样的值中:

<option value="<?= $key."_".$val ?>"><?= $val ?></option>

您可以像这样将其取出:

$value = $_POST['selected'];
$options = explode('_',$value); // Your key is in the first position (0), your option in the second (1)

$req->bindParam(':selectedOpt', $options[1]);
$req->bindParam(':numLigne', $options[0]);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章