I want to store data into database.
I tried this:
In the View I created this form:
<form id="addData" action="<?php echo URL ?>controles/addControl" method="POST">
<input type="text" name="text1">
<input type="text" name="text2">
<input type="text" name="text3">
<input type="submit" name="Add">
</form>
Then I created a Model
controles_model.php
public function addControl(){
$text1 = $POST['text1'];
$text2 = $POST['text2'];
$text3 = $POST['text3'];
$stmt = $this->db->prepare("INSERT INTO `controles` (field1, field2, field3 ) VALUES(?,?,?)");
$stmt->execute(array($text1,$text2,$text3));
if($stmt == true){
return "good";
}else{
return "wrong";
}
}
Then I created a Controller:
controles.php
class Controles extends Controller {
function __construct(){
parent::__construct();
}
public function index(){
$this->view->render('controles/index');
}
function addControl(){
$this->model->addControl();
}
}
and finally, for the JS file I created this:
$("#addControl").on('submit', function(e){
e.preventDefault();
var url = $(this).attr('action');
var data = $(this).serialize();
$.post(url, data, function(response) {
if(response == "good"){
$("#insertedSuccessfully").show();
}else if(response == "wrong"){
$("#notInserted").show();
}
});
});
All this doesn't work. How can I fix it?
NB: the database connection is OK, it work fine sine I can retreive data from database.
Aucun commentaire:
Enregistrer un commentaire