Answer the question
In order to leave comments, you need to log in
Write to database checkbox php?
Good time of the day, there is a form with checkboxes, if they are checked they need to be written to the database, if not, then by default there is 0, now it writes every other time, the form does not work stably.
I am attaching the code.
<form action="" method="post" class="formochka">
<a href="#" id="close"></a>
<fieldset><label for="name" class="first_label"> Введите название ресурса</label><input type="text" name="Name"></fieldset>
<fieldset><label for="link" class="first_label"> Введите ссылку на ресурс</label><input type="text" name="Link"></fieldset>
<fieldset><label for="description" class="first_label"> Введите описание ресурса</label><textarea name="description" id="" cols="39" rows="19" style="resize: none;"></textarea></fieldset>
<fieldset><label for="" class="first_label"> Выберите охват ресурса</label>
<label><input type="checkbox" name="rf" data-index="1" value="9">РФ</label>
<label><input type="checkbox" name="rt" data-index="2" value="9">РТ</label>
<label><input type="checkbox" name="regions" data-index="3" value="9">Регионы</label>
<label><input type="checkbox" name="inno" data-index="4" value="9">ИННО</label>
<label><input type="checkbox" name="msk" data-index="5" value="9">Москва</label>
</fieldset>
<fieldset><label for="" class="first_label"> Выберите целевую аудиторию ресурса</label>
<label><input type="checkbox" name="kids" data-index="1" value="9">Дети</label>
<label><input type="checkbox" name="young" data-index="2" value="9">Молодежь</label>
<label><input type="checkbox" name="par" data-index="4" value="9">Родители</label>
<label><input type="checkbox" name="teach" data-index="5" value="9">Наставники</label>
<label><input type="checkbox" name="buis" data-index="3" value="9">Бизнес</label></fieldset>
<fieldset><label for="" class="first_label"> Выберите направления работы ресурса</label>
<label><input type="checkbox" name="obr" data-index="1" value="9">Образование</label>
<label><input type="checkbox" name="prof" data-index="2" value="9">Профориентация</label>
<label><input type="checkbox" name="car" data-index="3" value="9">Карьера</label>
<label><input type="checkbox" name="psy" data-index="4" value="9">Психологическая поддержка</label>
<label><input type="checkbox" name="soc" data-index="6" value="9">Социальные практики</label>
<label><input type="checkbox" name="pred" data-index="5" value="9">Предпринимательство</label></fieldset>
<fieldset><label for="" class="first_label"> Введите Профориентационно значимые материалы ресурса</label>
<label><input type="checkbox" name="info" data-index="1" value="9">Информация о профессиях</label>
<label><input type="checkbox" name="art" data-index="2" value="9">Профориентацияонные статьи, рекомендации</label>
<label><input type="checkbox" name="games" data-index="3" value="9">Игры, треннинги, мастер классы, конкурсы, мероприятия</label>
<label><input type="checkbox" name="diag" data-index="4" value="9">Диагностические материалы</label>
<label><input type="checkbox" name="method" data-index="5" value="9">Методические материалы</label></fieldset>
<input type="submit" name="add_now">
</form>
if (isset($_POST['add_now'])) {
$Name = $_POST['Name'];
$Link = $_POST['Link'];
$Description = $_POST['description'];
$cover_rf = $_POST['rf'];
$cover_rt = $_POST['rt'];
$cover_regions = $_POST['regions'];
$cover_ino = $_POST['inno'];
$cover_msk = $_POST['msk'];
$aud_kids = $_POST['kids'];
$aud_young = $_POST['young'];
$aud_buis = $_POST['buis'];
$aud_par = $_POST['par'];
$aud_teach = $_POST['teach'];
$direct_educ = $_POST['obr'];
$direct_prof = $_POST['prof'];
$direct_carer = $_POST['car'];
$direct_psy = $_POST['psy'];
$direct_buis = $_POST['pred'];
$direct_soc = $_POST['soc'];
$prof_info = $_POST['info'];
$prof_art = $_SESSION['art'];
$prof_games = $_SESSION['games'];
$prof_diagnostics = $_SESSION['diag'];
$prof_teaching = $_SESSION['method'];
mysql_query("INSERT INTO structure(Name, Link, Description, cover_rf, cover_rt, cover_regions, cover_ino, cover_msk, aud_kids, aud_young, aud_buis, aud_par, aud_teach, direct_educ, direct_prof, direct_carer, direct_psy, direct_buis, direct_soc, prof_info, prof_art, prof_games, prof_diagnostics, prof_teaching)
VALUES('$Name', '$Link', '$Description', '$cover_rf','$cover_rt', $cover_regions,'$cover_ino','$cover_msk','$aud_kids','$aud_young','$aud_buis','$aud_par','$aud_teach','$direct_educ','$direct_prof','$direct_carer','$direct_psy','$direct_buis', '$direct_soc', '$prof_info', '$prof_art', '$prof_games', '$prof_diagnostics', '$prof_teaching')");
}
Answer the question
In order to leave comments, you need to log in
multiple checkboxes are handled via []
<input type="checkbox" name="p[1]"
<input type="checkbox" name="p[2]"
<input type="checkbox" name="p[3]"
<input type="checkbox" name="p[4]"
$p=$_POST['p'];
echo $p[1] . $p[2] . $p[3] . $p[4];
<input type="checkbox" name="p[par]"
<input type="checkbox" name="p[teach]"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question