delete items that are checked without $_POST php - php

I've stuck with the following issue:
I have a table with texts and each one has a checkbox next to it. I also have a button-like link "Delete selected". What I want is to give the ability to the user to select many texts through checkboxes and delete the selected all together. The problem is tha I don't want to use a form so I can't use $_POST so I suppose I have to use $_SESSION. I have used sessions before but I don't know how to combine them with checkboxes..
if you could give me some tips or examples I would be very gratefull..
parts of my code to give you a clue..
view_texts.php
<?php
$sql="SELECT * FROM texts";
$res=mysql_query($sql) or die(mysql_error());
$i=1;
while($row=mysql_fetch_array($res))
{
?>
<tr>
<td><input type="checkbox" name="check[]" value="<?php echo $row['id']?>"/></td>
<td><?php echo $i; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo substr($row['description'],0,20); ?></td>
<td><?php echo $row['page_id']; ?></td>
<td><img src="images/user_edit.png" alt="" title="" border="0" /> </td>
<td><a href="delete_text.php?id=<?php echo $row['id'];?>" class="ask"><img src="images/trash.png" alt=""
title="" border="0" /></a></td>
</tr>
<?php
$i++;
}
?>
<span class="bt_red_lft"></span><strong>Διαγραφή επιλεγμένων</strong><span class="bt_red_r"></span>
delete_selected_texts.php
<?php
if (isset($_POST['check']))
{
foreach ($_POST['check'] as $value)
{
$sql = "DELETE FROM texts WHERE id =".$value;
mysql_query ($sql);
}
}
?>
this is the code that i use for textboxes inside a form tag.

If you're stuck on not using a form, for whatever reason, you're looking at using javascript and sending an AJAX request on the POST instead. I'd recommend using jQuery, as that greatly simplifies the process of selecting the elements you want to target.
It would look something like this...
$(document).function(){
$(":checkbox").click(function(e){
var deleteItem = $("input:id").val();
$.ajax({
type: "POST";
data: deleteItem;
url: "path/to/a/file.php";
success: function(data){
if (data="success"){
$(this).css("backgroundcolor", "blue");
} else {
alert("failed to find item");
}
}
});
}
You would need then to have a file that the Ajax request sends the data to that will build a list of items to delete/take items off that list, which will be stored and called up when the delete_selected_texts.php is called up to be deleted.
NB.: The jQuery above hasn't been checked. There's an excellent chance of there being several errors in there.

Related

How to remember which table row is clicked in order to display more data to another page

I want to do next. When I click on a table row in the first HTML page, I want to display additional data for that row on another HTML page. This is the way I displayed data from PostgreSQL DB:
<?php
while($row = pg_fetch_assoc($result)) {
?>
<tr class="click_table" data-href="bsdisplay.php" onclick="show();" style="cursor:pointer">
<td><input type="checkbox" style="width: 20px; background-color: black;"></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['type']; ?></td>
</tr>
<?php
}
?>
The function shown is represented below:
function show() {
$.ajax({
type: "GET",
url: "baseFunctions.php",
success : function() {
location.reload();
}
});
}
baseFunctions is another PHP page where I wrote functions to display data in bsdisplay.php. I am now trying to find a way, to remember the row that is clicked and send that information to baseFunctions page as a number. I am relatively new to all of this, so I may be complicating things more than they are. Can someone help with this way or suggest an easier way to do this?

How to make a button inside a table execute a mysql query

I have a simple html table that displays data from a mysql table containing a list of available tournaments.
<table>
<tr>
<th>Tournament Name</th>
<th>Platform</th>
<th>Date</th>
<th>Venue</th>
<th>Judge</th>
<th>Description</th>
<th>Limit</th>
<th>Register</th>
</tr>
<?php while($row = mysqli_fetch_array($result)):?>
<tr>
<td><?php echo $row['nTournament'];?></td>
<td><?php echo $row['pTournament'];?></td>
<td><?php echo $row['dTournament'];?></td>
<td><?php echo $row['vTournament'];?></td>
<td><?php echo $row['jTournament'];?></td>
<td><?php echo $row['dTournament'];?></td>
<td><?php echo $row['lTournament']; ?></td>
<td><form method="post" action="tournamentlist.php"><button type="submit" name="btn_register"></button></form></td>
</tr>
<?php endwhile; ?>
</table>
What I want to do is that once the current user clicks that button, the button executes a query which takes the current user's id and the tournament id of the selected row to add them to another mysql table. Something like a tournament inscription.
The query/function I want to run is:
if (isset($_POST['btn_register'])) {
singup();
}
function singup() {
global $db;
$idUser = $_POST['idUser'];
$idTournament= $_POST['idTournament'];
$query = "INSERT INTO registeredCompetitor(idUser, idTournament) VALUES ('$idUser', '$idTournament')";
mysqli_query($db, $query);
header('location: tournamentlist.php');
}
The query works just fine on it's own but I don't know if the function itself works because of the button.
Is it possible to do such thing without the use of a form? If not, What other ways are there to do something like this?
EDIT1: Button now is ready but nor the function or the query executes once it's clicked.
You can create a form on each row that has 2 hidden fields in it. Not sure what your DB fields are called though so you'll have to change user_id and tournament_id to something appropriate.
...
<td>
<form action="post" action="the_name_of_your_php_script.php">
<input type="hidden" name="idUser" value="<?php echo $row['user_id']; ?>">
<input type="hidden" name="idTournament" value="<?php echo $row['tournament_id']; ?>">
<button type="submit" name="btn_register"></button>
</form>
</td>
...
Can't wrap it because <form> is not allowed as a child of <td>
All the documentation I see online permits forms inside of <td>. The other alternative is to use a link instead of a form.
You can use javascript to execute this functionality.
<table>
<tr>
<th>Tournament Name</th>
<th>Platform</th>
<th>Date</th>
<th>Venue</th>
<th>Judge</th>
<th>Description</th>
<th>Limit</th>
<th>Register</th>
</tr>
<?php while($row = mysqli_fetch_array($result)):?>
<tr>
<td><?php echo $row['nTournament'];?></td>
<td><?php echo $row['pTournament'];?></td>
<td><?php echo $row['dTournament'];?></td>
<td><?php echo $row['vTournament'];?></td>
<td><?php echo $row['jTournament'];?></td>
<td><?php echo $row['dTournament'];?></td>
<td><?php echo $row['lTournament']; ?></td>
<td><button type="submit" onclick="sendRequest('<?php echo $idUser ?>','<?php echo $idTournament ?>')" name="btn_register"></button></td>
</tr>
<?php endwhile; ?>
</table>
Javascript code
function sendRequest(idUser,idTournament ){
// i am using jquery
$.post(other_page_url,{idUser:idUser,idTournament :idTournament},function(data)
{
window.location = 'tournamentlist.php';
} )}
I hope it's gonna help you
You don't need to wrap this in a <form> inside the table, you can do it all via Javascript / Jquery if you wish. On the html side, add the data you need to the button in data sets:
<td><button type="submit" name="btn_register" id="btn_register" data-user-id="<?php echo $userId ?>" data-tournament-id="<?php echo $row['idTournament']; ?>"></button></td>
Then in your script, you can call the data and send to post via ajax and either load the new page after INSERT, or load as a modal, or whatever you like. You can do something like below if loading a modal (change the load part to refresh the page if you want to go directly):
$('#btn_register').on('click', function () {
var t_id = $(this).data('tournament-id');
var u_id = $(this).data('user-id');
$.ajax({
url: "YourURL",
type: "POST",
data:{
t_id:t_id,
u_id:u_id
},
success: function (h) {
$(".modal-title").text("Table with new stuff");
$(".modal-body").html(h);
$('#modal').modal();
// lots of options here - you could even reload above if desired
}
})
});

Increament id of td by Loop and getting id in function

I'm populating a table with database data, i want to get value of td that is row1, to perform action in PHP function, I dont know how to do this,
<?php
$sql = "SELECT DISTINCT name,c_no,email,speciality FROM doctor_temp";
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result))
{?>
<?php $count=1;
$count++;?>
<tr>
<td><?php echo $row[0];?></td>
<td id=<?php echo $count;?>><?php echo $row[1];?></td>
<td><?php echo $row[2];?></td>
<td><?php echo $row[3];?></td>
<td><button type="button" class="btn btn-success" onclick="insert()">register</button></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<script type="text/javascript">
function insert()
{
var a = document.getElementById($count).innerHTML;
alert(a)
}
</script>
</body>
Watch out! Your code is wrong because you need to put the ID number instead of the PHP variable. If you want to use count you have to put it inside a PHP tag and echo it, but you are mixing the JS with the PHP code.
getElementById(<?php echo $count; ?>).innerHTML
Just look at the console log when you are debugging, please, that makes things a lot easier and it saves you from asking questions that you can answer yourself.
I don’t recommend that you preprocess JS however, that is an extremely bad practise for any programming language.

Sql query on clicked button

I would like to ask how can I call Sql query when HTML button is pressed. I watched a couple tutorials, but never got it to work correct, thats why I would like to ask here.
So..I have a list of items displayed, and the list is generated from database. Here is how the code looks like:
<table width="100%" border="1" cellspacing="1" cellpadding="5" style="background-color:#FFC" >
<td>Id</td>
<td>Text</td>
<td>Solved?</td>
<td></td>
<?php
while( $array = mysql_fetch_assoc($queryRun) ) {
?><tr>
<td><?php echo $id = $array['id'].'<br />';?></td>
<td><?php echo $text = $array['text'].'<br />';?></td>
<td><?php echo $reseno = $array['solved'].'<br />';?></td>
<td><input type="button" value="Solve it" /></td>
</tr><?php
}?>
</table>
Now each item that is generated inside this loops has a button "Solved" at the end of the table, and I want to make it so when user click on that button, do a Sql query which changes a value of solved from 0 (false) to 1 (true).
You need ajax. write button onclick and make ajax call.
I've not tested this, but it should work.
Basically I've applied a class to anything to easily select stuff in jQuery:
<?php while ($array = mysql_fetch_assoc($queryRun)) { ?>
<tr class="row">
<td class="cell_id">
<?php echo $array['id']; ?>
</td>
<td class="cell_text">
<?php echo $array['text']; ?>
</td>
<td class="cell_solved">
<?php echo $array['solved']; ?>
</td>
<td class="cell_solve_it">
<input class="button_solve_it" type="button" value="Solve it" />
</td>
</tr>
<?php } ?>
Then I've created this short jQuery script:
$('.row').each(function(){
$('.button_solve_it', this).click(function(e){
e.preventDefault();
$.post(
'solve_query.php',
{
id: $('.cell_id', this).html(),
text: $('.cell_text', this).html()
},
function (data) {
$('.cell_solved', this).html('1');
}
);
});
});
In short:
when you click a .button_solve_it button, you make an AJAX request to solve_query.php, where you perform your SQL query.
Then, it sets the content of the row in Solved? column to 1 (or to true or whatever you want).

Use output from MySQL to extract additional info

How can I use the output of a MySQL query as to extract extra info from the database about that output?
For example, if my query generates a list of names and there is extra info about each name existing in the database, when I click on the name on the output page, the system will extract the relevant info from the database and show it on another page.
EDIT: Here's the code:
<?php
// you can delete mysql-assoc
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
?>
<tr align="center">
<td width="5%">
<a href="// I don't know what do put here " >
<?php print("{$row["pid"]}");?>
</a>
</td>
<td><?php print("{$row["place"]}");?></td>
<td><?php print("{$row["date"]}");?></td>
<td><?php print("{$row["ppp"]}");?></td>
<td><input type="button" value="Book"></td>
</tr> <?php } ?>
Set each row of the first output results equal to a variable, and then have an if statement in php that looks for strings in those variables, and then (upon some sort of a page refresh button since php is server-side code) displays the additional content.
Great; here's what you're missing:
<a href="<?php // I don't know what to put here ?>" >
Should be:
<a href="<?php echo 'database.php?action=viewrecord({$row["pid"]})'?>" >
From there, you should be able to extract the relevant info using the pid of the user.
On a side note, unless you're really worried about spacing, you should just dump out the relevant HTML; doing so will save you the task of repeatedly typing <?php ?> every time you want to dump out a variable.
<?php
// you can delete mysql-assoc
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
echo "<tr align='center'>";
echo "<td width='5%'>";
echo "<a href='database.php?action=viewrecord({$row['pid']})'>";
echo "{$row['pid']}";
echo "</a>";
echo "</td>";
echo "<td>{$row['place']}</td>";
echo "<td>{$row['date']}</td>";
echo "<td>{$row['ppp']}</td>";
echo "<td><input type='button' value='Book'></td>";
echo "</tr>";
}
?>
Thanks for the help but this doesn't work :( I'm beginner in php when I press the record the url looks like localhost/ass/database.php?action=viewrecord%28{$row[
This isnt a direct answer to your question perse but since youre new to php im goign to through it out there. The syntax you uisng makes unicorns cry. It give me a headache jsut tryign to decipher it. Heres the same code in an alternative way:
<?php while($row = mysql_fetch_array($rs, MYSQL_ASSOC)): ?>
<tr align="center">
<td><?php printf('%s', url_encode('viewrecord('.$row['pid'].')'), $row['pid']); ?></td>
<td><?php echo $row['place']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['ppp']; ?></td>
<td><input type="button" value="Book"></td>
</tr>
<?php endwhile; ?>
However this database.php?action=viewrecord() worries me. I hope youre not using eval to execute a function passed by URL. Typically this would look something more like: database.php?action=viewrecord&id=2. You would verify that the action parameter is valid, and then invoke a function tied to that parameter which may or may not be the actual function name, and you would pass in the id parameter.
Eaxample database.php:
$action = isset($_POST['action']) ? $_POST['action'] : 'index';
switch($action) {
case 'viewrecord':
if(isset($_POST['id']) {
viewrecord((integer) $_POST['id']);
}
break;
// a whole bunch of other cases here for other actions
default:
// the default thing to do if an action is in valid
}
// the rest of your processing

Categories