javascript - incorrect run php by button click -
i have php function update records in data table , need run click button in html.
my php function this:
<?php try { $sql = 'select id_data, date_record, value1, value2, value3 data '; $s = $pdo->prepare($sql); $s->execute(); } catch (pdoexception $e) { $error = 'error select data' . $e->getmessage(); include 'error.html.php'; exit(); } while ($row = $s->fetch()) { $dane[] = array( 'id_data' => $row['id_data'], 'date_record' => $row['date_record'], 'value1' => $row['value1'], 'value2' => $row['value2'], 'value3' => $row['value3'] ); } if (isset($_get['edytion'])) { foreach ($data $data2) { try { $sql = 'update data set date_record = :date_record, value1 = :value1, value2 = :value2, value3 = :value3 id_data= :id_data'; $s = $pdo->prepare($sql); $s->bindvalue(':date_record', $_post['date_record']); $s->bindvalue(':value1', $_post['value1']); $s->bindvalue(':value2', $_post['value2']); $s->bindvalue(':value3', $_post['value3']); $s->bindvalue(':id_data', $_post['id_data']); $s->execute(); } catch (pdoexception $e) { $error = 'edit data error ' . $e->getmessage(); include 'error.html.php'; exit(); } } header("location: theme3.php"); } ?>
and form in html try run look:
<div class="table-responsive"> <table class="table table-bordered"> <thead> <tr> <th>date</th> <th>value 1</th> <th>value 2</th> <th>value 3</th> </tr> </thead> <?php if (isset($data)): ?> <?php foreach ($data $data1): $a = 0 ?> <form action="?edytion" method="post" id='ed'> <tr class="bg-primary"> <input type="hidden" name="id_data" id="id_data" value="<?php echo $data1['id_data']; ?>"> <td><input type="date" name="date_record" id="date_record" value="<?php echo $data1['date_record']; ?>"> </td> <td><input type="text" name="value1" id="value1" value="<?php echo $data1['value1']; ?>"> </td> <td><input type="text" name="value2" id="value2" value="<?php echo $data1['value2']; ?>"> </td> <td><input type="text" name="value3" id="value3" value="<?php echo $data1['value3']; ?>"> </td> <!-- <input type="hidden" ondblclick="default" id="id_buttona" value="edit"/> --> </tr> </form> <?php $a++; endforeach; ?> <?php endif; ?> </tbody> </table> <input type="submit" id="id_buttona" onclick="document.getelementbyid('ed').submit();" value="edit"/> </div>
ultimately when try update data update first record in table, rest of them invariable.
know wrong , have idea how correct it? grateful help!
you create many forms same id
. surround table <form>
, foreach
on <tr>
only. this
<div class="table-responsive"> <form action="?edytion" method="post" id='ed'> <table class="table table-bordered"> <thead> <tr> <th>date</th> <th>value 1</th> <th>value 2</th> <th>value 3</th> </tr> </thead> <?php if (isset($data)): ?> <?php $a = 0; foreach ($data $data1): ?> <tr class="bg-primary"> <input type="hidden" name="id_data<?php echo $a; ?>" id="id_data" value="<?php echo $data1['id_data']; ?>" /> <td><input type="date" name="date_record<?php echo $a; ?>" value="<?php echo $data1['date_record']; ?>"> </td> <td><input type="text" name="value1<?php echo $a; ?>" value="<?php echo $data1['value1']; ?>"> </td> <td><input type="text" name="value2<?php echo $a; ?>" value="<?php echo $data1['value2']; ?>"> </td> <td><input type="text" name="value3<?php echo $a; ?>" value="<?php echo $data1['value3']; ?>"> </td> </tr> <?php $a++; endforeach; ?> <?php endif; ?> </tbody> </table> <input name="row_count" value="<?php echo isset($a) ? $a : 0; ?>" type="hidden"/> </form> <input type="submit" id="id_buttona" onclick="document.getelementbyid('ed').submit();" value="edit"/> </div>
the way created each form each row not work because: (1) forms have same javascript id
, when getelementbyid
first form affected, (2) when submit 1 form page reloads , changes other rows lost. 1 solution make 1 form , have different name fields. forms fields sent name
, value
need different names fields , don't need ids.
you can add row count somewhere in form , change php this:
$row_count = $_post['row_count']; for($i = 0; < $row_count; i++) { try { $sql = 'update data set date_record = :date_record, value1 = :value1, value2 = :value2, value3 = :value3 id_data= :id_data'; $s = $pdo->prepare($sql); $s->bindvalue(':date_record', $_post['date_record' . $i]); $s->bindvalue(':value1', $_post['value1' . $i]); $s->bindvalue(':value2', $_post['value2' . $i]); $s->bindvalue(':value3', $_post['value3' . $i]); $s->bindvalue(':id_data', $_post['id_data' . $i]); $s->execute(); } catch (pdoexception $e) { $error = 'edit data error ' . $e->getmessage(); include 'error.html.php'; exit(); } }
wiki
Comments
Post a Comment