I am trying to send the data to the backend when submitting a form so that I can create a new stay in the database.
The thing is that it doesn't give me any errors, and it shows me the alert that it has been done correctly, but it isn't created in the database, I don't know if I'm not treating the data well in the controller or how, the query works correctly because I have tried to add the stay in a hardcoded way and it works.
The form is as follows:
<form id="formCountry">
<div class="modal fade" id="addPaisModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Afegeix País</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label for="agreementCode">Nom</label>
<input type="text" name="nomPais" class="form-control" id="nomPais" placeholder="Nom país">
</div>
<div class="form-group">
<label for="agreementStudies">Programa</label>
<select name="programaPais" class="form-control" id="programaPais">
<?php foreach ($programs as $program): ?>
<option>
<?php echo $program->codiPrograma;?>
</option>
<?php endforeach;?>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary">Tanca</button>
<button type="submit" name="submit" value="Submit" id="addCountry" class="btn btn-primary">Guarda</button>
</div>
</div>
</div>
</div>
</form>
When I hit the submit button through jquery I send it by post to the backend (the 2 alerts show me the values correctly):
$(document).on("submit", "#formCountry", function(e) {
e.preventDefault();
var data = new FormData;
data.append("programaPais", $("#programaPais").val());
data.append("nomPais", $("#nomPais").val());
alert($("#programaPais").val());
alert( $("#nomPais").val());
$.ajax({
type: "POST",
url: "admin.php/updateTableCountries",
data: data,
processData: false,
contentType: false,
success:function(t,e,c){
alert("Pais creat correctament!");
$("#addPaisModal").modal('hide');
},
error: function(e, a, t) {alert("Error al crear el pais!");}
});
})
Controller (I follow MVC architecture):
case 'updateTableCountries':
$data= $_POST['data'];
array_push($parameters, $data);
$actionName = $controller;
break;
Function:
public function updateTableCountries($parameters){
require 'models/CountriesModel.php';
$countriesModel = new CountriesModel();
$data = $parameters[0];
$countriesModel->addCountry($data['programaPais'],$data['nomPais']);
$countriesModel->disconnect();
}
Query:
public function addCountry($codiPrograma,$nomPais){
try {
$consulta = $this->db->prepare("INSERT INTO pais(codiPrograma,nomPais) VALUES (?,?)");
$consulta->execute(array($codiPrograma,$nomPais));
} catch (Exception $e) {
$obj = $e;
}
}
Any idea what can happen?
I was able to fix it. I have added the following changes:
Controller:
Function: