将我的收音机的值插入数据库
问题描述:
I can insert the values of the radio's in the database but it only works if I have selected the radio 'vrouw' and if select 'man' it does not insert into the database.
Php function:
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>
Database:
答
What you should do in your processing page is check if the radio buttons are set first, then if they are, get the value.
Like this...
if( isset($_POST['keuzegender']) ) {
$radio_value = $_POST['keuzegender'];
}
Other than that, your code looks correct.
答
Radio buttons (and checkboxes) only send values if they are selected. They will not appear in the POST variables otherwise.