【发布时间】:2017-04-12 07:22:11
【问题描述】:
(支持: Ubuntu --> phpstorm, apache server, 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 起无法获得任何预付款,我们将不胜感激......
提前致谢,
问候,
Stelio KONTOS。
【问题讨论】:
-
您已将
numLigne绑定到$key,但它不会在您进行插入的文件中定义。您可以回显$key进行验证。如果您尝试从表单中获取密钥,请通过某种方式传递它。可以试试<option value="<?= $key."_".$val ?>"><?= $val ?></option>,把下划线的值炸开。 -
这就是我对我的 $numLigne 的看法...我确实想存储选定的选项,以及它在下拉列表中的行。做你刚才写的可能会授权我获得与我的价值相关联的密钥?
-
无论如何都要试试这个,谢谢!你能在这里指导我,我必须如何以及更准确地在哪里添加我的爆炸语句来存储我的数据......?
-
有人可以向我解释为什么我的问题是负面标志吗?我做了一些研究,解释得很清楚,可能对像我这样的初学者有用。反对的不是否定标志吗?