实际工作Ubuntu的(工作PHP语言), 我有转换成文本的PDF文件,然后我的preg_match为了提取我需要的数据。PHP:插入到数据库与阿贾克斯
之后,我把我的数据线放在一个下拉列表。
问题:我想用Ajax(据我了解),到得到选中的选项并保存到我的数据库。
我读过许多话题关于这个问题,白白...
这里有一块我的代码,它可能会更形而下!
我的PHP文件:
$file = fopen($fichier_txt, 'r+');
if ($file)
{
$lines = array();
$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];
$opt = array($match_nom_prenom, $match_adresse, $match_deux, $match_trois);
$i = 0;?>
<html>
<form>
<select name="selectBox" class="drop" id="Combobox1" onchange="saveToDatabase(this.value)">
<?php foreach($opt as $val) {?>
<option value="$opt[$i]"><?=$val?></option>
<?php } ?>
</select>
</form>
</html>
我formulaire_2_test.php文件:
<?php
// Database connection to save form lines (PDO)
try
{
$PDO = new PDO('mysql:host=localhost;dbname=autofill_data', 'root', 'password');
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
// Get selected option and save into database
$selectedOpt = $_POST['selected'];
//exit($selectedOpt); --> I put this line in comments since I don't use it everytime.
$req = $PDO->prepare("INSERT INTO data_lines(idDistributeur, numLigne, libelle) VALUES(1, 2, :selectedOpt)");
$req->bindParam(':selectedOpt', $selectedOpt);
$req->execute($selectedOpt);
$data = $req->fetchAll();
}
?>
喏,这就是我的Ajax脚本(我在JS是新的,我知道有些enormeous错误可能会弹出在你面前,对此感到遗憾,关于我的法语命名......)
阿贾克斯:(位于我的PHP文件)
<style>
.ui-autocomplete
{
cursor:pointer;
height:120px;
overflow-y:scroll;
}
</style>
<script>
function saveToDatabase(selectedValue)
{
var select = selectedValue;
select = $(this).serialize();
$('#Combobox1').on("change", function()
{
// POST to php script
$.ajax
({
type: 'POST',
url: 'formulaire_2_test.php',
data:{selected:this.value}
}).then(function(data){alert(data)});
});
}
$(document).ready(function()
{
saveToDatabase();
});
</script>
我测试了粗略的数值我的PDO连接,它的工作,但我不知道我怎么能忽略我的PHP变量到它(我不知道使用$ _POST来检索这个数据,因为我不刷新任何页面...) 我也试过INSERT到表VALUES(:名称,2,3),但它didn也不管用...... 我是否在钻机中前进?方向? 你会如何考虑这样做?
PS:我之后的下一步是从下面的下拉列表中删除选定的选项(为了在填写订阅表格时为用户节省一些宝贵的时间)。
EDIT年11月24:我需要我的“FAIS吨喜爱将”选项出现在我的下拉为默认值,而不是在列表中选择:
我的最后一个问题:我想删除所选择的选项的下拉列表中,因此它不会出现在其他下拉列表中。 这里是我试过的代码(不工作):
function removeSelected(value)
{
$('.drop').change('select', function()
{
// Definition des variables
var value = this.value;
var id = this.id;
// We del selects with a != id containing options with the same value of the selected one.
$("select:not(#" + id + ") option[value='" + value + "']").hide()
});
}
我也试图与一个.remove()代替.hide()没有成功!
由于提前,
问候,
斯泰利奥康托斯。
我想补充一点,我在Firefox Consol上测试了我的代码,我可以看到我的XHR请求,因为我选择了一个选项,但它不保存到数据库中...... –
您可以使用'''$ _POST '''。 Ajax只是一个由JavaScript执行的HTTP请求,而不是浏览器窗口。而不是'''data:'selected ='+ select''',只要做'''data:{selected:select}'''。然后你可以在'''$ _POST ['selected']''' –
处选择你的选项。谢谢你,在我的这条路上! :) –