I have a form that looks like:
|-----------------------|
|Text |
|(dropdown) |
------------------------|
|label (value = 1) | //get this by loop using while
|input | //get this by loop using while
|-----------------------|
|label (value = 2) | //get this by loop using while
|input | //get this by loop using while
|-----------------------|
|label (value = ...) | //get this by loop using while
|input | //get this by loop using while
|-----------------------|
|submit |
I dunno why my form just insert last value
example
- choose dropdown value = 1
- add value of input = 80 on label 1
- add value of input = 79 on label 2
FYI
- [ia] = dropdown value
- [ik] = label value
- [nn] = input value
when I click the submit button with print_r($_POST);
the output is Array ( [ia] => 1 [ik] => 2 [nn] => 79 )
I want to get
Array ( [ia] => 1 [ik] => 1 [nn] => 80 )
Array ( [ia] => 1 [ik] => 2 [nn] => 79 )
this is my code about using while
to display another form:
if($_POST){
include_once 'includes/rangking.inc.php';
$eks = new rangking($db);
$eks->ia = $_POST['ia'];
$eks->ik = $_POST['ik'];
$eks->nn = $_POST['nn'];
if($eks->insert()){
?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Berhasil Tambah Data!</strong> Tambah lagi atau <a href="rangking.php">lihat semua data</a>.
</div>
<?php
}
else{
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Gagal Tambah Data!</strong> Terjadi kesalahan, coba lagi.
</div>
<?php
}
}
?>
<form method="post">
<div class="form-group">
<label for="ia">Alternatif</label>
<select class="form-control" id="ia" name="ia">
<?php
$stmt3 = $pgn1->readAll();
while ($row3 = $stmt3->fetch(PDO::FETCH_ASSOC)){
extract($row3);
echo "<option value='{$id_alternatif}'>{$nama_alternatif}</option>";
}
?>
</select>
</div>
<div class="form-group">
<?php
$no=1;
$stmt2 = $pgn2->readAll();
while ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)){
extract($row2);
?>
<label for="ik"><?php echo $nama_kriteria; ?></label>
<input type="hidden" name="ik" id="ik" value=<?php echo $id_kriteria ?>>
<input type="text" class="form-control" id="nn" name="nn">
<?php
}
?>
</div>
<button type="submit" class="btn btn-primary">Simpan</button>
<button type="button" onclick="location.href='rangking.php'" class="btn btn-success">Kembali</button>
</form>
rangking.inc.php
function insert(){
$query = "insert into ".$this->table_name." values(?,?,?,'','')";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(1, $this->ia);
$stmt->bindParam(2, $this->ik);
$stmt->bindParam(3, $this->nn);
if($stmt->execute()){
return true;
}else{
return false;
}
}