我可以在数据库中插入收音机的值,但只有在我选择了收音机' vrouw'如果选择' man'它不会插入数据库。
Php功能:
if (isset($_POST['submitForm'])) {
$dbhost = "localhost";
$dbname = "schoolopdrachten";
$dbusername = "root";
$dbpassword = "";
$link = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbusername,$dbpassword);
$stmt = $link->prepare("INSERT INTO javascript (naam, gender, land, provincie, suboptionman, suboptionvrouw) VALUES (:naam, :gender, :land, :provincie, :achternaam, :suboptievrouw)");
$stmt->bindParam(':naam', $_POST['inputName']);
$stmt->bindParam(':gender', $_POST['keuzegender']);
$stmt->bindParam(':land', $_POST['select1']);
$stmt->bindParam(':provincie', $_POST['select2']);
$stmt->bindParam(':achternaam', $_POST['keuzeMan']);
$stmt->bindParam(':suboptievrouw', $_POST['keuzeVrouw']);
$stmt->execute();
}
HTML:
<div class="container">
<form method="post" id="form" class="form-horizontal">
<fieldset>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="inputName">Naam</label>
<div class="col-md-4">
<input id="inputName" name="inputName" type="text" placeholder="" class="form-control input-md formFunction">
<p id="errorNaam" class="help-block formError"></p>
</div>
</div>
<!-- Radio Keuze -->
<div class="form-group">
<label class="col-md-4 control-label">Geslacht</label>
<div class="col-md-4">
<div class="radio">
<label for="radioMan">
<input class="formFunction" type="radio" name="keuzegender" id="radioMan" value="man">
Man
</label>
<label for="radioVrouw">
<input class="formFunction" type="radio" name="keuzegender" id="radioVrouw" value="vrouw">
Vrouw
</label>
</div>
<p id="errorRadioGroup1" class="help-block formError"></p>
</div>
</div>
<!-- Keuze als man is gekozen -->
<div id="keuzeMan" class="form-group hidden">
<label class="col-md-4 control-label" for="optionMan">Achternaam</label>
<div class="col-md-4">
<input id="optionMan" name="keuzeMan" type="text" placeholder="" class="form-control input-md formFunction">
<p id="errorKeuzeMan" class="help-block formError"></p>
</div>
</div>
<!-- Keuze als vrouw is gekozen -->
<div id="keuzeVrouw" class="form-group hidden">
<label class="col-md-4 control-label">Leeftijd</label>
<div class="col-md-4">
<label class="radio-inline" for="optionVrouw1">
<input class="formFunction" type="radio" name="keuzeVrouw" id="optionVrouw1" value="Jonger dan 50">
Jonger dan 50
</label>
<label class="radio-inline" for="optionVrouw2">
<input class="formFunction" type="radio" name="keuzeVrouw" id="optionVrouw2" value="Ouder dan 50">
Ouder dan 50
</label>
<p id="errorKeuzeVrouw" class="help-block formError"></p>
</div>
</div>
<!-- Select Land -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectLand">Land</label>
<div class="col-md-4">
<select id="selectLand" name="select1" class="form-control formFunction">
<option value="empty">--Kies Land--</option>
<option value="nederland">Nederland</option>
<option value="belgie">België</option>
</select>
<p id="errorSelectLand" class="help-block formError"></p>
</div>
</div>
<!-- Select Provincie -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectProvincie">Provincie</label>
<div class="col-md-4">
<select id="selectProvincie" name="select2" class="form-control formFunction">
<option value="empty">--Kies provincie--</option>
<option value="overijssel">Overijssel</option>
<option value="noordHolland">Noord-Holland</option>
</select>
<p id="errorSelectProvincie" class="help-block formError"></p>
</div>
</div>
<!-- Multiple Checkboxes -->
<div class="form-group">
<label class="col-md-4 control-label" ></label>
<div class="col-md-4">
<div class="checkbox">
<label>
<input class="formFunction" type="checkbox" name="checkboxes" id="checkBox" value="checkBox">
Check het formulier & enable de verstuur button
</label>
</div>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label"></label>
<div class="col-md-4">
<button id="submitForm" name="submitForm" class="btn btn-success" disabled="disabled">Verstuur</button>
</div>
</div>
</fieldset>
</form>
</div>
数据库:
答案 0 :(得分:0)
您应该在处理页面中执行的操作是检查单选按钮是否先设置,如果是,则获取值。
喜欢这个......
if( isset($_POST['keuzegender']) ) {
$radio_value = $_POST['keuzegender'];
}
除此之外,您的代码看起来是正确的。
答案 1 :(得分:-1)
单选按钮(和复选框)仅在选中时发送值。否则它们不会出现在POST变量中。