dreq dreq -4 years ago 86
HTML Question

HTML in PHP - insert all value form while some of form display by loop

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


  1. choose dropdown value = 1

  2. add value of input = 80 on label 1

  3. add value of input = 79 on label 2



FYI


  1. [ia] = dropdown value

  2. [ik] = label value

  3. [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">&times;</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">&times;</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;
}

}

Answer Source

You have multiple elements with the same name, if you post that form it will only send the values of the last element.

You should change your these two lines:

<input type="hidden" name="ik" id="ik" value=<?php echo $id_kriteria ?>>
<input type="text" class="form-control" id="nn" name="nn">

to this:

<input type="hidden" name="ik[]" id="ik" value=<?php echo $id_kriteria ?>>
<input type="text" class="form-control" id="nn" name="nn[]">

That way it is passed as an array which should contain every input. You can check this answer for more information.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download