I have this delete function in my system but first I need the server needs to know which table he has to delete that is why I am sending a data when the user click the delete button into the server which is the ID of the data I want to delete..first I need to try and get that data being sent by the form but the problem is sending data is not working in my part I tried to echo out the ID just to see if I have a result but it works fine but when I send it to the server it doesn't print anything.. Here is my code where I fetch the scheduleID and the form
if ($strand<>""){
$query1 = mysqli_query($conn,"SELECT * from schedule natural join instructor where day = 'm' and schedule.strand= '$strand' and timeID ='$id' and grade = '$grade' and semester = '$semester'");
}
$row1 = mysqli_fetch_array($query1);
$schedID = $row['scheduleID'];
$id = $row1 ['scheduleID'];
$count=mysqli_num_rows($query1);
if ($count==0)//checking
{
//echo "<td></td>";
}
else
{
//print
echo "<div class='show'>";
echo "<ul>
<li class='options' style='display:inline'>
<span style='float:left;'><a href='sched_edit.php?id=$id1' class='edit' title='Edit'>Edit</a></span>
<span class='action'><a href='#' id='$id1' class='delete' title='Delete'>Remove</a></span>
</li>";
echo "<form class = 'delete' method = 'post' action ='../functions/delete.php'>";
echo "<li class='showme'>";
echo " <input type='hidden' name='delete' value='$id'>";
echo "<button type='submit' name='delete' class='btn btn-danger'>Display Schedule</button>";
echo $row1['subject'];
echo "</li>";
echo "<li class='$displayc'>$row1[strand]";
echo "<li class='$displaym'>$row1[fname], $row1[lname]</li>";
echo "<li class='$displayr'>Room $row1[room]</li>";
echo "</form>";
echo "</ul>";
echo "</div>";
}
?>
</td>
I tried the hidden attribute in some of my forms and it work there but I don't know why it won't in this form, the $id is working also I tried echoing that inside in this page but the data sent is not printing in the server, here's my server
<?php
session_start();
include 'database.php';
if (isset($_POST['delete'])){
$ID = $_POST['delete'];
}
thanks in advance
The problem here is that you have 2 form elements using the same name attribute, so PHP is only keeping the last value, which is not defined.
See both the <input> and the <button> have the same name! The button is last and has no value; that is what is being used by PHP.
<input type='hidden' name='delete' value='$id'>
<button type='submit' name='delete' class='btn btn-danger'>Display Schedule</button>
So you can just remove the name from the button or change it to something other than delete :-)
Related
I am trying to update a php form that holds a few rows of mysql data. I have a button next to each row and when i click on that I want to update the row. The issue im having below is the ID is only set as the last row. How do i get this to push the ID to the button? So basically no matter what button i press i always get the same ID which is the last one to load.
if($result){
while($row = mysqli_fetch_array($result)){
$id = $row["ID"];
$beername = $row["BeerName"];
$beertype = $row["BeerType"];
$beerpercent = $row["BeerPercent"];
$beerdescription = $row["BeerDescription"];
$nowpouring = $row["NowPouring"] =='0' ? '' : 'checked=\"checked\"';
$glutenreduced = $row["GlutenReduced"] =='0' ? '' : 'checked=\"checked\"';
$beertogo = $row["BeerToGo"] =='0' ? '' : 'checked=\"checked\"';
echo "<form action='' method='POST'>";
echo "<tr><td><h6><input type=\"text\" size=\"5\" name=\"id\" value=\"$id\"></h6></td>";
echo "<td><h6><input type=\"text\" size=\"30\" name=\"BeerName\" value=\"$beername\"></h6></td>";
echo "<td><h6><input type=\"text\" size=\"30\" name=\"BeerType\" value=\"$beertype\"></h6></td>";
echo "<td><h6><textarea size=\"90\" style=\"width:250px;height:150px;\" name=\"BeerDescription\" value=\"\">$beerdescription</textarea></h6></td>";
echo "<td><h6><input type=\"text\" size=\"5\" name=\"Percent\" value=\"$beerpercent\"></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"NowPouring\" value=\"true\" $nowpouring></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"GlutenReduced\" value=\"true\" $glutenreduced></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"BeerToGo\" value=\"true\" $beertogo></h6></td>";
#echo "<td><h6> <a href=\". $_SERVER["PHP_SELF"] .?id=".mysql_result($result,$j,'id')."\" onclick=\"\"></h6></td>";
echo "<td><h6> <button name=\"submit\" type=\"submit\" value=\"$id\">Save</button></h6></td>";
echo "</tr>";
echo "</form>";
}
}
if (isset($_POST['submit'])) {
$user = $_POST['submit'];
echo "<p style=\"color:#ffffff\">$id</p>";
#$delet_query = mysqli_query($mysqli, "UPDATE NowPouring SET NowPouring = '1' WHERE ID = '4'") or die(mysql_error());
if ($delet_query) {
echo '<p style="color:#ffffff">Beer with id '.$id.' is updated. To refresh your page, click ' . ' <a href=' . $_SERVER["PHP_SELF"] . ' > here </a></p>';
}
}
?>
The main problem I see here is that the while loop your code has is generating the same name for the inputs...
All of your "<button name=\"submit\" type=\"submit\" value=\"$id\">Save</button>" will have the same name, that's why it always has the last id as value.
Maybe you should try something such as..
<button name=\"$id_submit\" type=\"submit\" value=\"$id\">Save</button>
or if you want you can store it in an array..
<button name=\"submit[]\" type=\"submit\" value=\"$id\">Save</button>
You are seeing this result because the 'name' of each of your inputs is the same, so essentially you have a form with a bunch of elements that have the same names. You need to add a dynamic aspect to each name.
For example, you could update your output to something like this:
echo "<tr><td><h6><input type=\"text\" size=\"5\" name=\"id_$id\" value=\"$id\"></h6></td>";
Where each line adds the current id. Then when you retrieve the form data, you can append the submitted id to the field you want to update.
Have you considered using an AJAX approach so you can submit just the line in question and not have to reload the page and return the whole data set each time?
Make <form> for each submit button. Adding <form> in the while():
if($result){
while($row = mysqli_fetch_array($result)){
echo "<form action='' method='POST'>";
//...
echo "</form>";
}
}
Your form tag is placed at the wrong place.
It should be within:
while($row = mysqli_fetch_array($result)){
$id = $row["ID"];
//....
//....
echo "<form action='' method='POST'>";
echo"<tr>";
echo "<td>" . $id . "</td>";
//....
//....
echo "<td><button type='submit' name='submit'>Save</button></td>";
echo"</tr>";
echo "</form>";
}
i have some problem in here. I have textbox value inside table, and when i clicked link "Valid", i can pass the textbox value to another page.
This is my textbox code in table
echo "<form name='nomor' role='form' method='get'>";
echo "<input type='text' name='nomortempat' class='form-control input-sm'></input>";
echo "</form>";
This is my button
echo "<td class='center'>";
echo "<a class='btn btn-primary btn-sm' href='validasi.php?idorder=" . $row['id_order'] . "&pilih=" . $_GET['pilih'] . "'>
<i class='fa fa-check-square-o'>Valid</i></a>
echo "</td>";
Maybe someone can give me a solution, Thank you and have a nice day!!
In the page validasi.php add this:
$IDorder = $_GET['idorder'];
$Pilih = $_GET['pilih'];
Now you have two variables on validasi.php called IDorder and Pilih from your form
Or is it the nomortempat you want?
I think you can just add
$nomortempat = $_GET['nomortempat'];
to the validasi page and it should work. If I understand your code it should be sent
EDIT I was wrong, you need to add a sumbmit button to your form.
Add session start at the top of your pages: session_start();.
Then add this to your first page:
$_SESSION["idorder"] = $idorder;
$_SESSION["pilih"] = $pilih;
Then this code to your form:
echo "<form name='nomor' role='form' method='get'>";
echo "<input type='text' name='nomortempat' class='form-control input-sm'></input>";
echo "<input type='submit' name='submit' value='Submit'></input>";
echo "</form>";
Then on validasi page add this:
$nomortempat = $_GET['nomortempat']; // and:
$IDorder = $_SESSION["idorder"];
$Pilih = $_SESSION["pilih"];
Now you have all three values on validasi.php
with GET form, you could try this:
echo "<form name='nomor' action='action.php' role='form' method='get'>";
echo "<input type='text' name='nomortempat' class='form-control input-sm'></input>";
echo "<input type='submit' value='valid'>";
echo "</form>";
and in action.php page, to catch value in textbox name='nomortempat', code is below:
if (isset($_GET['nomortempat'])){
echo $_GET['nomortempat'];
}
Of course, you could add more input fields whenever you want to inside form with different names.
I have a form with 2 selects, when you send the first, the second select charges the values that are called on my oracle bd with a query, then when i send the second select, it generates a table with checkboxes:
if(isset($idTActi)){
$stallTableTarifas=oci_parse($conn, "SELECT TARIFAS.ID, TARIFAS.ID_TIPO_ACTIVIDAD, TARIFAS.TIPO, TIPO_ACTIVIDAD.TEMPS_KM, TARIFAS.PRECIO
FROM TARIFAS, TIPO_ACTIVIDAD
WHERE TARIFAS.ID_TIPO_ACTIVIDAD = TIPO_ACTIVIDAD.ID
AND TARIFAS.ID_TIPO_ACTIVIDAD = $idTActi");
oci_execute($stallTableTarifas);
echo "<div class='divPrecios'>";
echo "<table>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Tipus Tarifa</td>
<td>Temps/Km</td>
<td>Preu</td>
<td><input type='submit' class='carrito' value=''></td>";
echo "</tr>";
while (($row=oci_fetch_array($stallTableTarifas,OCI_BOTH))!=false){
echo "<tr>";
echo "<td>".$row['TIPO']."</td>";
echo "<td>".$row['TEMPS_KM']."</td>";
echo "<td>".$row['PRECIO']."</td>";
echo "<td><input type='checkbox' name='checkbox[]' value='".$row['ID']."'/></td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
}
echo "</form>";
The variable $idTActi it's the id that i return from the second select, so when i click on the checkboxes and i send it on the button named class='carrito', that's an sprite that i generate on css, i see on the bottom another table with the information that i selected on the previous table:
echo "<div class='divPrecios'>";
echo "<table>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Nom Activitat</td>
<td>Nom Tipus Activitat</td>
<td>Tipus Tarifa</td>
<td>Temps/km</td>
<td>Preu</td>";
echo "</tr>";
foreach($_POST['checkbox'] as $item){
$stallTableCarrito=oci_parse($conn, "SELECT ACTIVIDAD.NOM AS NOM_ACTIVIDAD, TIPO_ACTIVIDAD.NOM AS NOM_TACTIVIDAD, TARIFAS.TIPO, TIPO_ACTIVIDAD.TEMPS_KM, TARIFAS.PRECIO
FROM TARIFAS, ACTIVIDAD, TIPO_ACTIVIDAD
WHERE TARIFAS.ID = $item
AND TARIFAS.ID_TIPO_ACTIVIDAD = TIPO_ACTIVIDAD.ID
AND TIPO_ACTIVIDAD.ID_ACTIVIDAD = ACTIVIDAD.ID");
oci_execute($stallTableCarrito);
$array=array(
0=>array(),
1=>array(),
2=>array(),
3=>array(),
4=>array()
);
while (($row=oci_fetch_array($stallTableCarrito,OCI_BOTH))!=false){
array_push($array[0],$row['NOM_ACTIVIDAD']);
array_push($array[1],$row['NOM_TACTIVIDAD']);
array_push($array[2],$row['TIPO']);
array_push($array[3],$row['TEMPS_KM']);
array_push($array[4],$row['PRECIO']);
}
for ($x=0;$x<count($array[0]);$x++){
echo "<tr>";
echo " <td>".$array[0][$x]."</td>";
echo " <td>".$array[1][$x]."</td>";
echo " <td>".$array[2][$x]."</td>";
echo " <td>".$array[3][$x]."</td>";
echo " <td>".$array[4][$x]."</td>";
echo " <td><input type='submit' class='carritoElim' value=''></td>";
echo "</tr>";
}
}
echo "</table>";
echo "</div>";
Basically that's a shopping form.
And where is the problem? When i send the pushed checkboxes with the button class='carrito', the form by default refresh the page and clears my array, what can i do?
In your first part of code, is your form tag open ? (I guess it is if this one works)
In the second part, is your <input type='submit' class='carritoElim' value=''> tag in a form ?
Because if it's not, you gonna have a bad time ;-)
Maybe in the last form you should generate hidden input with same names as your first form and same values.
If you don't I guess your variable $idTActi won't be set anymore and it won't succeed the first test if(isset($idTActi)). That could be why you get a cleared page.
If you have a multi step form in the same php page, for this kind of html code :
<form method=POST url="myURL">
<select name="select1">[...]</select>
<select name="select2">[...]</select>
<!-- VARIOUS PART : may not be displayed -->
<div id="checkboxes">
<input type="hidden" name="boxStep" value="1"/>
<input type="checkbox" name="cb1" value="1"/>
[...]
</div>
<!-- END OF VARIOUS PART -->
</form>
Then you need php tests in this order :
// if post request
if (isset($_POST)) {
if (isset($_POST['boxStep'])) {
// behavior when checkboxes values are sent
} else {
if (isset($_POST['select2'])) {
// behavior when second select is filled
// display "VARIOUS PART"
} else {
// behavior when only first select is filled
// Do not display "VARIOUS PART"
}
}
} else {
// default behavior (no select filled)
// Do not display "VARIOUS PART"
}
Apolo
I have on my page a dataTable with tons of rows. Each row has an id, name, surname and an action column. In the action column there is a textarea where you can add a comment and a button that submits that comment. When I submit the comment I want the page to position itself where it was, but I cannot figure it out how. Any ideas?
Here is the snippet of the code:
$result = mysql_query($query, $connection);
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<form method='post' action='saveComment.php#position_".$row['id']."'>";
echo "<td hidden><input hidden name='id' readonly value=" . $row['id'] . " /></td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td><input type='submit' name='submit' value='Comment'/></td>";
echo "<a id='position_".$row['id']."'></a>";
echo "</tr>";
}
I tried passing the id of the current row to the script that saves the comment and it does pass the value, after which it should position it back where it was, but it doesn't seem to work :/
use Ajax to submit the form, and on success:
window.location = 'saveComment.php#position_<? echo $row['id']; ?>';
I found out how it works with anchors! The problem was that I didn't pass the id in the saveComment.php script when redirecting back to the original page.
Code in the page where the form is:
if ($_POST['submit']){
$id=$_POST["id"];
header('Refresh: 0.5; URL=originalPage.php#position_' . $id);
//...rest of code goes here...
}
That's it :)
You need to use
<a name='position_".$row['id']."'></a>
instead of
<a id='position_".$row['id']."'></a>
I want to build a feature that enables the user to moderate questions and is similar to wordpress comment or typical email moderation that uses a checkbox to approve/delete many questions at once.
How would I modify the following code to check which the questions are checked and react to the delete buttons when pressed? You may notice from the below code that I can currently do this with individual questions but I want to give the user more power.
<h2>Moderate Questions</h2>
<div>
<button type="button" class="btn" name="delete" id="delete">Delete</button>
<button type="button" class="btn" name="approve" id="approve">Approve</button>
<?php
// Select all unapproved questions in db
$sql = "SELECT question_id, question, DATE_FORMAT(question_date, '%e %b %Y at %H:%i') AS dateattime FROM questions WHERE question_approved='0' ORDER BY question_date DESC";
$result = mysql_query($sql);
$myquestions = mysql_fetch_array($result);
if($myquestions) {
do {
$question_id = $myquestions["question_id"];
$question = $myquestions["question"];
$dateattime = $myquestions["dateattime"];
echo "<div class='question'>";
echo "<span value='$question_id'>";
echo "<input name='checkbox' type='checkbox' id='checkbox' value='$question_id'>";
echo "$question - <span class='date'>Asked $dateattime </span>";
echo "</span>\n";
echo "<div class='question-controls'><a href='".$_SERVER["PHP_SELF"]."?delete=$question_id' title='Delete this question' class='delete' onclick='return confirm(\"Are you sure you want to delete this question?\")'>Delete</a> | <a href='#'>Edit</a> |";
echo " <a href='".$_SERVER["PHP_SELF"]."?approve=$question_id' title='Publish this question'>Approve</a></div>";
echo "</div>";
} while ($myquestions = mysql_fetch_array($result));
} else {
echo "<p>No one has asked a question recently.</p>";
}
?>
</div>
You need to modify 'checkbox' element in order to make an array of values. Code:
echo "<input name='checkbox[]' type='checkbox' id='checkbox' value='$question_id'>";
After that your can call this variable from php code(using $_REQUEST['checkbox'] for example) and it will be array. So your can iterate through it and delete these rows from database.