Как редактировать данные в mysql через модальные

У меня были ошибки с этим кодом. Я новичок в загрузке и использовании модалов для вещей. Раньше я запускал дочерние формы для всех моих «причудливых» форм, но теперь я делаю переход к модальным. Это мой код для захвата модалов:

<button class="btn btn-primary" data-toggle="modal" data-target="#newmemberModal">Add new Member</button> <br /> <br /> <?php $result = mysql_query("SELECT * FROM `players` ORDER BY `players`.`user` ASC") or die(mysql_error()); echo " <div class='table-responsive'><table class='table'>"; echo "<tr><th>Username</th> <th>Rank</th> <th>Position</th> <th>DoP</th> <th>Tag</th> <th>AiT</th> <th>Service Stripes</th> <th>Notes</th> <th></th> <th></th><th></th></tr>"; while($row = mysql_fetch_array( $result )) { echo "<tr>"; echo '<td>' . $row['user'] . '</td>'; echo '<td>' . $row['rank'] . '</td>'; echo '<td>' . $row['position'] . '</td>'; echo '<td>' . $row['date'] . '</td>'; echo '<td>' . $row['tag'] . '</td>'; echo '<td>' . $row['ait'] . '</td>'; echo '<td>' . $row['ss'] . '</td>'; echo '<td>' . $row['notes'] . '</td>'; echo '<td><button class="btn btn-default" data-toggle="modal" data-target="#editModal">Edit</button></td>'; echo '<td><a href="classes/forms/delete.php?id=' . $row['id'] . '">Delete</a></td>'; echo '<td><a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick=window.open("dd.php?id=' . $row['id'] . '","Ratting","width=350,height=550,0,status=0,");>DD</a><BR /><a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick=window.open("hd.php?id=' . $row['id'] . '","Ratting","width=350,height=550,0,status=0,");>HD</a></td>'; echo "</tr>"; } echo "</table> </div>"; ?> 

Вот мой edit.php:

  <?php include_once('classes/check.class.php'); ?> <?php function renderForm($id, $user, $rank, $position, $date, $tag, $ait, $ss, $notes, $error) { ?> <?php if ($error != '') { echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; } ?> <?php if( protectThis("1, 2") ) : ?> <center><form action="" method="post"> <input type="hidden" name="id" value="<?php echo $id; ?>"/> <div> <strong>Username: *</strong> <br/><input class="tb5" type="text" name="user" value="<?php echo $user; ?>" /><br/><br/> <strong>Rank: * Current is </strong> <?php echo $rank; ?> <br/> <select name="rank"> <option value="recruit" <?php if ($rank == 'recruit') echo 'selected'; ?>>Pending Rank</option> <option value="officer" <?php if ($rank == 'officer') echo 'selected'; ?>>PVT</option> <option value="leader" <?php if ($rank == 'leader') echo 'selected'; ?>>PV2</option> </select><br/><br/> <strong>Position: </strong> <br/><input class="tb5" type="text" name="position" value="<?php echo $position; ?>" /><br/><br/> <strong>Date: *</strong> <br/><input class="tb5" type="text" name="date" value="<?php echo $date; ?>" /><br/><br/> <strong>Tag: *</strong> <br/><input class="tb5" type="text" name="tag" value="<?php echo $tag; ?>" /><br/><br/> <strong>AiT's: </strong> <br/><input class="tb5" type="text" name="ait" value="<?php echo $ait; ?>" /><br/><br/> <strong>Service Stripes: </strong><br/> <input class="tb5" type="text" name="ss" value="<?php echo $ss; ?>" /><br/><br/> <strong>Notes: </strong><br/> <input class="tb5" type="text" name="notes" value="<?php echo $notes; ?>" /><br/><br/> <input class="btn-style" type="submit" name="submit" value="Submit"></center> </div> </form> <?php elseif( protectThis(6) ) : ?> <center><form action="" method="post"> <input type="hidden" name="id" value="<?php echo $id; ?>"/> <div> <strong>Username: *</strong> <br/><input class="tb5" type="test" name="user" value="<?php echo $user; ?>" disabled /><br/><br/> <strong>Rank: * Current is </strong> <?php echo $rank; ?> <br/> <select name="rank"> <option value="recruit" <?php if ($rank == 'recruit') echo 'selected'; ?>>PVT</option> <option value="officer" <?php if ($rank == 'officer') echo 'selected'; ?>>PV2</option> </select><br/><br/> <strong>Position: </strong> <br/><input class="tb5" type="hidden" name="position" value="<?php echo $position; ?>" /><br/><br/> <strong>Date: *</strong> <br/><input class="tb5" type="text" name="date" value="<?php echo $date; ?>" /><br/><br/> <strong>Tag: *</strong> <br/><input class="tb5" type="text" name="tag" value="<?php echo $tag; ?>" /><br/><br/> <strong>AiT's: </strong> <br/><input class="tb5" type="text" name="ait" value="<?php echo $ait; ?>" /><br/><br/> <strong>Service Stripes: </strong><br/> <input class="tb5" type="hidden" name="ss" value="<?php echo $ss; ?>" /><br/><br/> <strong>Notes: </strong><br/> <input class="tb5" type="hidden" name="notes" value="<?php echo $notes; ?>" /><br/><br/> <input class="btn-style" type="submit" name="submit" value="Submit"></center> </div> </form> <?php endif; ?> <?php } include('connect-db.php'); if (isset($_POST['submit'])) { if (is_numeric($_POST['id'])) { $id = $_POST['id']; $user = mysql_real_escape_string(htmlspecialchars($_POST['user'])); $rank = mysql_real_escape_string(htmlspecialchars($_POST['rank'])); $position = mysql_real_escape_string(htmlspecialchars($_POST['position'])); $date = mysql_real_escape_string(htmlspecialchars($_POST['date'])); $tag = mysql_real_escape_string(htmlspecialchars($_POST['tag'])); $ait = mysql_real_escape_string(htmlspecialchars($_POST['ait'])); $ss = mysql_real_escape_string(htmlspecialchars($_POST['ss'])); $notes = mysql_real_escape_string(htmlspecialchars($_POST['notes'])); if ($user == '' || $rank == '' || $date == '' || $tag == '') { $error = 'ERROR: Please fill in all required fields!'; @renderForm($id, $user, $rank, $position, $date, $tag, $ait, $ss, $notes, $error); } else { mysql_query("UPDATE players SET user='$user', rank='$rank', position='$position', date='$date', tag='$tag', ait='$ait', ss='$ss', notes='$notes' WHERE id='$id'") or die(mysql_error()); header("Location: home.php"); } } else { echo 'Error!'; } } else { if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) { $id = $_GET['id']; $result = mysql_query("SELECT * FROM players WHERE id=$id") or die(mysql_error()); $row = mysql_fetch_array($result); if($row) { $id = $row['id']; $user = $row['user']; $rank = $row['rank']; $position = $row['position']; $date = $row['date']; $tag = $row['tag']; $ait = $row['ait']; $ss = $row['ss']; $notes = $row['notes']; @renderForm($id, $user, $rank, $position, $date, $tag, $ait, $ss, $notes, ''); } else { echo "No results!"; } } else { echo 'Error!'; } } ?> в  <?php include_once('classes/check.class.php'); ?> <?php function renderForm($id, $user, $rank, $position, $date, $tag, $ait, $ss, $notes, $error) { ?> <?php if ($error != '') { echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; } ?> <?php if( protectThis("1, 2") ) : ?> <center><form action="" method="post"> <input type="hidden" name="id" value="<?php echo $id; ?>"/> <div> <strong>Username: *</strong> <br/><input class="tb5" type="text" name="user" value="<?php echo $user; ?>" /><br/><br/> <strong>Rank: * Current is </strong> <?php echo $rank; ?> <br/> <select name="rank"> <option value="recruit" <?php if ($rank == 'recruit') echo 'selected'; ?>>Pending Rank</option> <option value="officer" <?php if ($rank == 'officer') echo 'selected'; ?>>PVT</option> <option value="leader" <?php if ($rank == 'leader') echo 'selected'; ?>>PV2</option> </select><br/><br/> <strong>Position: </strong> <br/><input class="tb5" type="text" name="position" value="<?php echo $position; ?>" /><br/><br/> <strong>Date: *</strong> <br/><input class="tb5" type="text" name="date" value="<?php echo $date; ?>" /><br/><br/> <strong>Tag: *</strong> <br/><input class="tb5" type="text" name="tag" value="<?php echo $tag; ?>" /><br/><br/> <strong>AiT's: </strong> <br/><input class="tb5" type="text" name="ait" value="<?php echo $ait; ?>" /><br/><br/> <strong>Service Stripes: </strong><br/> <input class="tb5" type="text" name="ss" value="<?php echo $ss; ?>" /><br/><br/> <strong>Notes: </strong><br/> <input class="tb5" type="text" name="notes" value="<?php echo $notes; ?>" /><br/><br/> <input class="btn-style" type="submit" name="submit" value="Submit"></center> </div> </form> <?php elseif( protectThis(6) ) : ?> <center><form action="" method="post"> <input type="hidden" name="id" value="<?php echo $id; ?>"/> <div> <strong>Username: *</strong> <br/><input class="tb5" type="test" name="user" value="<?php echo $user; ?>" disabled /><br/><br/> <strong>Rank: * Current is </strong> <?php echo $rank; ?> <br/> <select name="rank"> <option value="recruit" <?php if ($rank == 'recruit') echo 'selected'; ?>>PVT</option> <option value="officer" <?php if ($rank == 'officer') echo 'selected'; ?>>PV2</option> </select><br/><br/> <strong>Position: </strong> <br/><input class="tb5" type="hidden" name="position" value="<?php echo $position; ?>" /><br/><br/> <strong>Date: *</strong> <br/><input class="tb5" type="text" name="date" value="<?php echo $date; ?>" /><br/><br/> <strong>Tag: *</strong> <br/><input class="tb5" type="text" name="tag" value="<?php echo $tag; ?>" /><br/><br/> <strong>AiT's: </strong> <br/><input class="tb5" type="text" name="ait" value="<?php echo $ait; ?>" /><br/><br/> <strong>Service Stripes: </strong><br/> <input class="tb5" type="hidden" name="ss" value="<?php echo $ss; ?>" /><br/><br/> <strong>Notes: </strong><br/> <input class="tb5" type="hidden" name="notes" value="<?php echo $notes; ?>" /><br/><br/> <input class="btn-style" type="submit" name="submit" value="Submit"></center> </div> </form> <?php endif; ?> <?php } include('connect-db.php'); if (isset($_POST['submit'])) { if (is_numeric($_POST['id'])) { $id = $_POST['id']; $user = mysql_real_escape_string(htmlspecialchars($_POST['user'])); $rank = mysql_real_escape_string(htmlspecialchars($_POST['rank'])); $position = mysql_real_escape_string(htmlspecialchars($_POST['position'])); $date = mysql_real_escape_string(htmlspecialchars($_POST['date'])); $tag = mysql_real_escape_string(htmlspecialchars($_POST['tag'])); $ait = mysql_real_escape_string(htmlspecialchars($_POST['ait'])); $ss = mysql_real_escape_string(htmlspecialchars($_POST['ss'])); $notes = mysql_real_escape_string(htmlspecialchars($_POST['notes'])); if ($user == '' || $rank == '' || $date == '' || $tag == '') { $error = 'ERROR: Please fill in all required fields!'; @renderForm($id, $user, $rank, $position, $date, $tag, $ait, $ss, $notes, $error); } else { mysql_query("UPDATE players SET user='$user', rank='$rank', position='$position', date='$date', tag='$tag', ait='$ait', ss='$ss', notes='$notes' WHERE id='$id'") or die(mysql_error()); header("Location: home.php"); } } else { echo 'Error!'; } } else { if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) { $id = $_GET['id']; $result = mysql_query("SELECT * FROM players WHERE id=$id") or die(mysql_error()); $row = mysql_fetch_array($result); if($row) { $id = $row['id']; $user = $row['user']; $rank = $row['rank']; $position = $row['position']; $date = $row['date']; $tag = $row['tag']; $ait = $row['ait']; $ss = $row['ss']; $notes = $row['notes']; @renderForm($id, $user, $rank, $position, $date, $tag, $ait, $ss, $notes, ''); } else { echo "No results!"; } } else { echo 'Error!'; } } ?> 

Проблема в том, что я продолжаю получать свою «Ошибка!». сообщение в модальном поле, а не что-то еще. Цель, которую я пытаюсь сделать, – это вытащить данные из идентификатора, который хранится в моей базе данных. Я думаю, проблема в том, что она не захватывает идентификатор, когда я нажимаю кнопку редактирования на пользователе. Честно говоря, я не знаю, как я смогу редактировать идентификатор через модальный. Помощь будет высоко оценена. благодаря

Бонус: я не могу открыть два модала на одной странице. Если у меня два на одной странице, ни одна из них не появляется:

 <!--Modals--> <div class="modal fade" id="newmemberModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <h4 class="modal-title">Add New Member</h4> </div> <div class="modal-body"> <?php include("classes/forms/new.php"); ?> </div> </div> </div> </div> <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <h4 class="modal-title">Edit</h4> </div> <div class="modal-body"> <?php include("classes/forms/edit.php"); ?> </div> </div> </div> </div> 

Ваша кнопка редактирования не передает идентификатор в edit.php из-за чего модальность не активирована. Вам нужно заполнить содержание редактируемого модального файла, прежде чем открывать его, а не загружать его. Использование include будет загружать страницу редактирования, не передавая ей идентификатор.

Перепишите свою кнопку редактирования, чтобы содержать идентификатор

 <button class="btn btn-primary" data-toggle="modal" data-target="#editModal" data-formid="<?php echo $id; ?>"> Edit <?php echo $id; ?> </button> 

JS для загрузки формы редактирования при открытии модального

 //jQuery function for loading data $("#editModal").on('show.bs.modal', function(event){ var button = $(event.relatedTarget); //bootstrap way of retrieving data-* attributes //data-formid in this case var id = button.data('formid'); $.get(yourSiteUrl+'/classes/forms/edit.php?id='+id, function(data) { $("#editModal .modal-title").html(data); //Resize the modal to the size of the loaded form. $("#editModal").modal("handleUpdate"); }); }); 

Проверьте этот учебник, чтобы узнать о моделях bootstrap

PS: Я знаю, что OP мог бы решить эту проблему к настоящему моменту или использовал бы другой подход, я опубликовал ответ, чтобы он мог быть полезен другим.

попробуй это..
// редактируем ссылку или кнопку.

 <a href="#" data-toggle="modal" data-target="#myModal_23" data-formid="<?php echo $id; ?>" class="button yellow" > <i class="ace-icon fa fa-pencil bigger-130"></i> </a> //ajax_edit.php //modal window <div class="modal fade " id="myModal_23" role="dialog"> $id = $_REQUEST['id']; //echo $id; //code </div> 

// скрипт

 <script> $("#myModal_23").on('show.bs.modal', function(event){ var button = $(event.relatedTarget); var id = button.data('formid'); //alert(id); $.get('ajax_edit.php?id='+id, function(data) { $("#myModal_23").html(data); }); }); </script>