Okay, so i am developing a class project and I'm building a website with basic functionality. I'm new to php and javascript.
So here is the problem. I have created a database in phpMyAdmin called 'itemdb'. I am able to add items to it through html+php and also delete them. Now what i want to do is to edit the data in the database and that is where I can't find a fix for it.
This is what my idea is. 1. clicks on edit button. 2. Opens Bootstrap Modal. 3. Displays the item information. 4. Hits save changes. Database updated.
My problem is If I set the button/input type to "submit" surrounded with a form tag the modal will crash. However, the current code below does not have a form tag and the button is set as type"button" and when I click the button, it only displays one item's information even if I click another button(same data).
Here is my code.
for testing purpose, I have created a table displaying "hello" in the first row and a button in the second row, and when I click the button, it shall display the info in the modal.
<div class = "row col-xs-10 col-sm-10 col-md-10 col-lg-10 ">
<?php
include 'connectDB.php';
$query = "SELECT * FROM `itemdb`;";
$result = mysqli_query($dbconnect, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{?>
<table>
<tr>
<td>Hellow</td>
<td>
<?php
echo "
<button type=\"button\" class=\"btn btn-primary\" data-toggle=\"modal\" data-target=\"#exampleModal\"
data-whatever=\"editDB[" . $row['itemID'] . "]\" onclick=\"window.Socation.href = 'adminItems3.php?value=" . $row['itemID'] . "'\";>Edit</button>
";?>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="exampleModalLabel">New message</h4>
</div>
<div class="modal-body">
<?php
if(!empty($row['itemID'])){
$ID = $row['itemID'];
echo $ID;
$query1 = "SELECT * FROM `trademe`.`itemdb` WHERE `itemdb`.`itemID` = $ID;";
$result1 = mysqli_query($dbconnect, $query1);
if(mysqli_num_rows($result1) > 0){
$row1 = mysqli_fetch_assoc($result1);
echo "
<table class=\"table-striped\">
<tr>
<td class = \"imgBoxCol\">
<img class=\"imgBox img-rounded imgBox\"src=\"" . $row1['img_path'] . "\"></img>
</td>
<td>
<p >Ad Title: <input name=\"editadTitle\" value=\"" . $row1['adTitle'] . "\"/></p>
<p>Item Name:<input name=\"edititemName\" value=\" " . $row1['itemName'] . "\"/> </p>
<p>Condition:<textarea name=\"editcond\" type=\"text\" >" . $row1['cond'] . "</textarea></p>
<p>Description:<textarea name=\"editdescription\" type=\"text\" > " . $row1['description'] . " </textarea></p>
<p>Method:<input name=\"editmethod\" value=\"" . $row1['method'] . " \"/></p>
<p>Category:<input name=\"editcategory\" value=\"" . $row1['category'] . " \"/></p>
</td>
</tr>
</table>
";
}}?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Send message</button>
</div>
</div>
</div>
</div>
</td>
</tr>
</table>
<?php//opening php// ending html code.
}
}
?>
</div>
script
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var Eid = button.data('whatever') // Extract info from data-* attributes
// If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
// Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
var modal = $(this)
modal.find('.modal-title').text('New message to ' + Eid)
modal.find('.modal-body input').val(Eid)
// document.location="adminItems3.php?idSelected=" + Eid
})
I have been trying to fix this for days. If you guys have better ideas of passing at least the item ID somehow, so that my modal will be able to retrieve it, then that would be really great!
Is there any way to pass the variable, so that i can retrieve it through my modal without crashing?
[Image of the 3rd button when clicked][2]
it displays the information of the first buton Thanks again !!