Stelio Kontos Stelio Kontos - 12 days ago 11
Ajax Question

php PDO Insert issue (Ajax drop-down extraction)

(support : Ubuntu --> phpstorm, apache server, PDO)

Hi everyone,

I'm actually trying to save the selected value of a drop-down list into my PDO database using Ajax.

PROBLEM : I do manage to add my $selectedOpt into my database, but as I try to do the same with $numLigne, the INSERT doesn't work anymore...
(I want to get the value of the selected option's row)

Here's my code :

$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 }


In the very same file, I have my Ajax call :

<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();
}
});
});
});




And finally, my PDO connection statement : (located in another php file)

<?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', 'palinome23');
}
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();


My problem is located in my "numLigne" variable, but I can't understand where precisely...

Some help would be greatly appreciated as I can't get any advance since 9:00 AM...

Thanks in advance,

Regards,

Stelio KONTOS.

Answer

As I mentioned, the $key will only pass through if you tell it. If you add it to the value like so:

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

You can get it back out like this:

$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]);