Answer the question
In order to leave comments, you need to log in
`
Is it possible to work like this with ajax`om?
Variables with the correct values, but ajax seems to work only at the moment the value is passed to the button and that's it.
Maybe it's just impossible to pass values to data like that?
tempvar_name_group = $(this).attr("value");
j = 1;
tempvar_surname = $(`#ipt_surname${j}`).val();
tempvar_name = $(`#ipt_name${j}`).val();
tempvar_patronymic = $(`#ipt_patronymic${j}`).val();
number_student = `ipt_insert_number_student${j}`;
surname = `ipt_insert_surname${j}`;
name = `ipt_insert_name${j}`;
patronymic = `ipt_insert_patronymic${j}`;
$.post("ajax_quest.php",
{n_btn_save_students: "n_btn_save_students",
ipt_n_show_table: tempvar_name_group,
number_student: j,
surname: tempvar_surname,
name: tempvar_name,
patronymic: tempvar_patronymic})
.done(function( data ) {
});
if ( isset ($_POST['n_btn_save_students'])){
for ($j = 1; $j <=25; $j++){
if($_POST['ipt_insert_surname'.$j.''] == true){
if($_POST['ipt_insert_name'.$j.''] == true){
if($_POST['ipt_insert_patronymic'.$j.''] == true){
$num_stud = $_POST['ipt_insert_number_student'.$j.''];
$surname = $_POST['ipt_insert_surname'.$j.''];
$name = $_POST['ipt_insert_name'.$j.''];
$patronymic = $_POST['ipt_insert_patronymic'.$j.''];
$query_insert_fio = "INSERT INTO `".$_SESSION['temp_name_group']."` (`id`, `surname`, `name`, `patronymic`) VALUES (:num_stud, :surname, :name, :patronymic)";
$query_insert_fio_params = [
':num_stud' => $num_stud, ':surname' => $surname, ':name' => $name, ':patronymic' => $patronymic];
$stmt = $pdo->prepare($query_insert_fio);
$stmt->execute($query_insert_fio_params);
}
}
}
}
$mess = "Студенты успешно добавлены в БД!";
echo '<div id = "message">'.$mess.'</div>';
}
Answer the question
In order to leave comments, you need to log in
First, you have incorrect PHP code. Even if empty values come from the form, your code will display a message about the successful addition of students to the database, but in fact they are not added. It is better to get rid of nesting (if->if->if). Checking for the existence of a non-empty variable is best done using isset(). Regarding Ajax, as far as I understand, you send it immediately after the page is loaded, so the data from the input is not transmitted. Try to replace
$.post("ajax_quest.php",
{n_btn_save_students: "n_btn_save_students",
ipt_n_show_table: tempvar_name_group,
number_student: j,
surname: tempvar_surname,
name: tempvar_name,
patronymic: tempvar_patronymic})
.done(function( data ) {
});
$( "#target" ).click(function() {
$.post("ajax_quest.php",
{n_btn_save_students: "n_btn_save_students",
ipt_n_show_table: tempvar_name_group,
number_student: j,
surname: tempvar_surname,
name: tempvar_name,
patronymic: tempvar_patronymic})
.done(function( data ) {
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question