This is my code and it is inside table tags. It grabs data from my database and put it in table. My database also has auto increment column named id. What I want to do is when I click the cross icon that is in anchor, I will get the id for that column.
I tried putting the value of $row['id'] in an input type='hidden' but had no success with it.
Another idea I has is making an anchor id=".$row['id']." so every icon has a unique id pertaining to the id for that line. Though I don't if it is good. I can't check because I have no idea how to get an element's id so I can check. And I don't know how to pass this value to php.
<?php
$varSQL = "SELECT empNum, empName FROM tool.users";
$result = mysql_query($varSQL, $Con);
while ($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['empNum']."</td>";
echo "<td>".$row['empName']."</td>";
echo "<a href='#'><img src='icons/cross.ico' alt='Delete' height='16' width='16'></a></td>";
echo "</tr>";
}
?>
You can use data attribute -
echo "<a href='#'><img src='icons/cross.ico' alt='Delete' data-rowid='" . $row['id'] . "' class='cross-icon' height='16' width='16'></a></td>";
Also added a class to be be used as selector(Not required for all cases). And with jquery access them when the icon is click -
$('.cross-icon').on('click', function() {
var id = $(this).data('rowid');
})
Fiddle Example
For hidden fields there is no need of jQuery -
<input type="hidden" name="id" value="<?php echo $row['id']?>"
Or pass it as query string -
href='page-to-redirect.php?id=<?php echo $row["id"]?>'
What you want to do with this id? You can get the id using javascript (not PHP) and then using this value in an AJAX request or put this value into an input form and send it throw post but always you need to use JS not PHP.
It is because you do not have id in your query. It should look like this:
SELECT id, empNum, empName FROM tool.users
<?php
$varSQL = "SELECT id, empNum, empName FROM tool.users";
.........
echo "<td><a href='#'><img src='icons/cross.ico' data-rowid=$row['id'] alt='Delete' height='16' width='16'></a></td>";
........
Try this one.
In select query add id also and Get the data-rowid value for your further operations
Related
I have a music database with a PHP front end where you can add/edit/delete artists, albums, tracks using the web client. The last real problem I have is getting a select box to automatically select an option I pass to the page.
Example:
On a page called 'createCD.php' I have this code:
echo "<td><a href=\"editCD.php?cdTitle=".rawurlencode($row['cdTitle'])."&cdID=$row[cdID]&artID=$row[artID]&cdGenre=".rawurlencode($row['cdGenre'])."&cdPrice=$row[cdPrice]\" </a>Edit</td>";`
This is used as a link to the next page, and collects all the information about an album in the database and sends in to a page called 'editCD.php'.
Now on this page, all the information is used to fill out the webpage as shown here (there is more but for the purposes of this post, only the first select box matters):
Artist Name:
<!-- dropdown with artist name -->
<?php
echo '<select name= "artID" id="artID">';
while ($row = mysqli_fetch_assoc($result)){
echo '<option value="'.$row['artID'].'">'.$row['artName'].'</option>';
}
echo '</select>';
?>
<p>
Album Title:
<input id="cdTitle" type="text" name="cdTitle" value ="<?php echo htmlspecialchars($cdTitle); ?>" />
</p>
What I would like is for the "selected" option for 'artID' to be the value that is passed to the page. Using the associative array, I was able to display the 'artName' associated with the 'artID'. Currently, all the information about the album appears correctly apart from the 'artName' and it defaults to the first value. This is a problem as if a user simply clicks "Update" it will update the name to the default name, therefore changing the database entry by accident.
I know I need to be using
<option selected ...>
but I'm not sure on the syntax to use.
<?php
$artID = $_GET['artID']; // get the artID from the URL, you should do data validation
echo '<select name= "artID" id="artID">';
while ($row = mysqli_fetch_assoc($result)){
echo '<option value="'.$row['artID'].'"';
if ($artID == $row['artID']) echo ' selected'; // pre-select if $artID is the current artID
echo '>'.$row['artName'].'</option>';
}
echo '</select>';
?>
$artId = $_GET['artID'];
while ($row = mysqli_fetch_assoc($result)) {
$selected = $artId == $row['artID'] ? 'selected' : '';
echo '<option value="'.$row['artID'].'" '.$selected.'>'.$row['artName'].'</option>';
}
First you get the id via $_GET['artID']. (In a real scenario use intval or something to prevent sql injection)
Then check in the loop if the id from database is the same as the id from GET and when it is print "selected, else nothing.
I have a problem on selecting an id on div. I use a while loop on my div.
So i'am creating a unique ID using the id row on my sql. My question is, is it possible to select the id using Javascript? I use bootstrap modal to update my information details (i dont actually know how to ask this question because my english is poor).
example:
while($row = mysqli_fetch_array($result)) {
<div id="'details-'<?php echo $row['Room_ID'];?>">
//code
</div>
}
To fix your Php code:
while($row = mysqli_fetch_array($result)) {
echo "<div id='details-" . $row['Room_ID'] ."'>";
// CODE
echo "</div>";
}
The problem you got in the id, where you stopped the value by ' already, so the result was something like:
<div id='details-'28>//Code</div> and fixed to <div id='details-28'>//Code</div>
and to select the ID with pure JavaScript use document.getElementById( ID )
If you are not sure of the ID and you just wanna select all items, add a class to the item and select them all by document.getElementsByClassName
So to select all your stuff by javascript:
1) Add into your PHP code class called "details"
2) Create a javascript to select all items with the class name "details"
3) Do a foreach loop for the selected items and split them by "-" to devide the "details" from the actual ID
4) Do whatever you want now with the ID
Practical showcase:
Php:
while($row = mysqli_fetch_array($result)) {
echo "<div class='details' id='details-" . $row['Room_ID'] ."'>";
// CODE
echo "</div>";
}
JavaScript:
<script>
var itemDetails = document.getElementsByClassName("details");
for(var i = 0; i < itemDetails.length; i ++) {
var ID = itemDetails[i].getAttribute("id").split("-")[1];
// Now you got the ID stored in the variable ID
</script>
Here you got an example on JSFiddle
As for as my understanding from your code, you are want to create a div with id details from your database. For that you should correct your code as given blew
<?php
while($row = mysqli_fetch_array($result)) {
echo "<div id='details-'". $row['Room_ID'];">";
//code
echo "</div>";
}
?>
If it is problem to select it and perform some manipulations using jQuery/javascript you may consult the official API documentation at
This link
I tried combining two answers from here:
counting how many checkbox are checked php/html
And here:
validating input types "checkbox" and "number" php/html
My goal is to count how many checkboxes were selected, and insert their values along with the number value in the number input.
I found various other posts about it, but couldn't figure out an answer.
Here is the code:
echo '<form action="" method="post">';
Then I have a for loop, where I create my table rows, with the checkboxes.
Where $id = $row[0] is updated on each loop, which is the id equivalent of my entry.
(The output of
echo '<td style="vertical-align: top;">' . $row[0] . '</td>';
Shows the id correctly)
echo '<tr>';
echo "<td style='vertical-align: top;'><input type='checkbox' name='choice[$id][id]' value='$id'></td>";
echo "<td style='vertical-align: top;'><input type='number' name='choice[$id][order]' size='20'></td>";
echo '</tr>';
echo '<b> <input type="submit" value="Insert"></b>';
Then, on the next page, I have this:
$var_checkbox=$_POST['choice'];
$sql_var_id = "SELECT id FROM custom_form WHERE id=(SELECT max(id) FROM custom_form)";
$var_id_result = mysqli_query($link,$sql_var_id);
$var_id = mysqli_fetch_array($var_id_result);
$count=count($var_checkbox[$var_id[0]]);
for($i=0; $i<$count; $i++){
if($var_checkbox[$i]!= NULL){
$sql1 = sprintf("INSERT INTO custom_form_has_property (custom_form_id,property_id,field_order) VALUES ('%d','%d','%d');",
$var_id[0],
$var_checkbox[$var_id[0]][id],
$var_checkbox[$var_id[0]][order]
);
$result1 = mysqli_query($link,$sql1);
}
}
The problem is, that no values of checkboxes or numbers are inserted.
(As a side note, to try and be more specific)
If instead of matrixes, I use
echo "<td style='vertical-align: top;'><input type='checkbox' name='choice[]' value='$row[0]'></td>";
echo '<td style="vertical-align: top;"><input type="number" name="order[]" size="2"></td>';
And
$var_checkbox=$_POST['choice'];
$order = $_POST['order'];
Then
echo "Orders: $order[0],$order[1],$order[2],$order[3],$order[4],$order[5],$ordemr[6]";
echo "Choices: $var_checkbox[0],$var_checkbox[1],$var_checkbox[2],$var_checkbox[3],$var_checkbox[4],$var_checkbox[5],$var_checkbox[6]";
Will output
Orders: 1,,2,,3,,Choices: 1,3,13,,,,
I need the choiced from the checkboxes to be somehow linked to the orders from the number field. And the only way to achieve that, is if they have the same name, but different indexes, which is why I have the matrix, in the first index, it's saved the id number, which is generated on each loop, and in the second, the actual name, which makes the distinction between them (id and order).
I tried various other things, but it all comes back to the same problem...
The value of order (number) is stored into the array, even when null, while the value of choices (checkbox) doesnt.
I could do an array_filter(), but there is no guarantee that the user will not input an order, while not ticking a choice checkbox.
If I would compare the length of choice with the length of order, after filtered, and if they output the same size, there is still no guarantee, that the user didnt check checkbox of line x, and added a value in the order field, on line y.
Maybe there is a way to pass unchecked values to the choice[] variable as null, the same way it happens in order[]?
What is this? choice[.$id.] ? what are the dots for?
I believe this is the bug!
You probably were trying to concatenate $id into the string before and just forgot to remove them?
Corrected line:
echo "<td style='vertical-align: top;'><input type='checkbox' name='choice[$id][id]' value='$id'></td>";
there are some way to do this ..
you have to count total no of selected check box via jQuery and set those value in hidden field at view page on each check box check or on form submit
you have to take counter on insert part and save the ids and at last update ids value with counter value in database ....
I have a mysql table I want to echo the name of every user and on click on a certain user show his profile:
$user=mysql_query("SELECT * FROM signup ORDER BY name ASC ");
while($row = mysql_fetch_array($user)){
echo ''.$row['nume'].' '.$row['prenume']."";
echo "<br>";}
The problem is that i can not figure out how to concatenate $row['username'] with href
The output should look like this: www.abcde.de/profil?user=username
Try this
echo ''.$row['nume'].' '.$row['prenume'].'';
I would like to delete a row from mysql database. The row I would like to delete is the 'selected item' in the combobox.
My code so far:
$sql = "SELECT data_id FROM projects";
$result = mysql_query($sql);
echo "<select name='data_id '>";
echo "<option selected>Please select...</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['data_id '] . "'>" . $row['data_id'] . "</option>";
}
echo "</select>";
?>
<tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table>
<?php
//delete selected item from projects table...
?>
The combobox is showing all the items from my projects table. When I select an item, then click on 'delete' I would like to delete that item from the table.
Any help will be appreciated.
I did not get why do they suggest you to use ajax, while you can make simple form
<form method="post" action="">
<tr><td>
//SOME PHP where you obtain from the database the data_id (this you have done already)
<option value='" . $row['data_id '] . "'>" . $row['data_id'] . "</option>
//
<input type="submit" name="delete" value="Delete" id="delete">
</td></tr>
</form>
and on same page at the top before HTML
<?php
if(isset($_POST['delete']) {
//and now mysql DELETE FROM .... WHERE id = $_POST['data_id']
//dont forget to escape and use mysli instead of mysql
}
?>
Btw. dont use mysql_* its deprecated, + escape everything in mysql + dont use fetch_array when you dont need an array, fetch_assoc is fine ;) Hope that helps.
Your logic should be:
At top of script before html, check for POST values from form
if they exist, use the variables to delete the appropriate database value
use header('location') back to the same page to reload the page (so value is removed from form below)
where you have your select box, make that a form, action= same page, method = post
tomorrow I will post example code if you have not worked it out by then
first off all you are using type submit and by that you need a form,and when you use option you need to set a value or the option
1
2
3
if you want to delete a particular row in the database and let say the id is 1 so it is the first row do it like this
if(isset($_GET['to_be_deleted']))
{
$id=mysql_real_escape_string($_GET['to_be_deleted']);
mysql_query("DELETE FROM name_table where id='$id'");
}
if my answer is not good please ask me with more details