I have a conmysql.php with this function:
function addTeacher($idTeach,$nameTeacher,$teacherDay){
$adicionar = sprintf("INSERT INTO `test`.`teacher` (`$idTeacher`, `$nameTeacher`, `$teacherDay`) VALUES ('$idTeacher', '$nameTeacher', '$teacherDay');");
$queryadd = mysql_query($adicionar);
}
I'm filling these variables with 3 forms, and it's working but i don't know how to trigger the addTeacher (); on a HTML button.
I should use AJAX? If yes, how?
Try like,
HTML
<input type="button" id="addTeacher" value="Add teacher"/>
SCRIPT
$(function(){
$('#addTeacher').on('click',function(){
$.ajax({
url:'conmysql.php',
type:'POST',
data:{type'addTeacher','name':'ABC','day':'day to add'},
success:function(data){
alert(data);
}
});
});
});
PHP
function addTeacher($idTeach,$nameTeacher,$teacherDay){
// Id of teacher should be primary and autoincrement
// so there is no need if teacher id
$adicionar = sprintf("INSERT INTO `test`.`teacher` (`$idTeacher`, `$nameTeacher`, `$teacherDay`) VALUES ('$idTeacher', '$nameTeacher', '$teacherDay');");
$queryadd = mysql_query($adicionar);
}
if(isset($_POST['type']) and $_POST['type']=='addTeacher')
{
addTeacher(1,$_POST['name'],$_POST['day']);
}
You can't call PHP functions from HTML.
You'll need to think of a solution where your HTML triggers the PHP page to run, one common method is to use AJAX to do this, although you could also do it in a form submit, it's up to you.