Get html button id and store it for next page - php

OK, so is there anyway to get the button id when clicked and store it for use in the next page (i'm trying to use the button id to know which button is clicked and use it for an sql query in the next page) ?
here's my code :
while($donnees=$reponse->fetch())
{
?>
<tr>
<form action="suppression.php" method="post">
<td ><h3 ><a ><?php echo $donnees['Ncheque']; ?></a></h3></td>
<td><?php echo $donnees['Dateremise'];?></td>
<td><a><?php echo $donnees['Client'];?></a></td>
<td><a><?php echo $donnees['Banque'];?></a></td>
<td><a><?php echo $donnees['Motif'];?></a></td>
<td><?php echo $donnees['Dateretour'];?></td>
<td><?php echo $donnees['Datedelivrance'];?></td>
<td><input type="submit" id="<?php echo $donnees['Ncheque'] ?>" class="button" name="delete" value="supprimer" />modifier</td>
</form>
</tr>
<?php
}
$reponse->closeCursor();
?>

You should wrap each button in a <form> element and specify the GET parameter directly in the action then in PHP check which button was clicked using $_GET.
First Page
<form action='otherPage.php?button=Button1'><button>Button1</button><form>
<form action='otherPage.php?button=Button2'><button>Button2</button><form>
Second Page
<?php
if($_GET['button']=="Button1"){
echo "Button 1 was clicked";
} else if($GET['button']=="Button2"){
echo "Button 2 was clicked";
} else {
echo "No button was clicked";
}

You can use HTML5 localstorage.
It stores values in key value pair in your browser.
localStorage.setItem('key', 'value'); // in place of value give id of the clicked button

Related

Using HTML buttons in PHP loops

I have a html table displayed using foreach loop in php. And I do even have buttons to be clicked in multiple rows. And the code goes like this:
<form method="post">
<table>
<tr> <th> Item </th> <th>Click to select</th></tr>
<?php
$query="select items from items_table";
$result=$con->query($query); //$con is connection variable already initialized
$row=mysqli_fetch_assoc($result);
foreach ($row as $index) //loop
{
?>
<tr>
<td><?php echo $index['items']; ?> </td>
<td><input type="button" value="select"> </td> //button here
</tr>
<?php } ?>
</table>
</form>
Now how can I get to know which button was pressed?
I have read some web pages, which says we need to be using AJAX, and I'm a newbie with no knowledge of how it works.. Please help me out!
I tried to have button inside a loop and expected that the buttons works correctly directly. But it gives wrong output.
If I got what u want I'd say you should handle your button or input
Some ways available
$index['items']
Is Not Correct where u used
<input value=<?php echo $row['id'] ?>
<input name=foo[<?php echo $row['id'] ?>] value=<?php echo $row['id'] ?> >
<input name='foo[]' value=<?php echo $row['id'] ?> >
Then handle them :
<?php
foreach($_REQUEST['foo'] as $name =>
$value){
echo $name ."posted and its value
is:". $value;
}
?>
OR
<?php
echo 'value of foo[1] Is '.$_REQUEST['foo[1]'] ;
?>
You can use FOR EXAMPLE $row['name'] Or any field name u have in your table intead of $row['id'] that I gave

Pass Two values when a Submit button is clicked -PHP

This table is displaying details about my workload table per row. And when I click my "pre-View_attend" submit button I want to get the "WorkloadID" and "subjectID" of that workload row where the submit button was clicked.
Currently I have a hidden field containing the subject ID which is "subjectID_hidden", and when I try to get the "WorkloadID" and "subjectID" by using isset.
It seems that I can't accurately get the exact subject ID of the row where I've clicked the "pre-View_attend" submit button.
<tbody style="font-size:20px; text-align:center;">
<tr>
<td><?php echo $row["subjectName"]; ?></td>
<td><?php echo $row["className"]; ?></td>
<td><?php echo $row["RoomNumber"]; ?></td>
<td><?php echo $Sched_Days.'<br>'.$FST.' To '.$FET;?></td>
<td><button style="font-size:15px;" type="button" id="<?php echo $row["WorkloadID"]; ?>" class="btn btn-primary view_StudentList">View Student List</button></td>
<td><button style="font-size:15px;" type="submit" name="gettingAttendance" value="<?php echo $row["WorkloadID"]; ?>" class="btn btn-primary">Take Attendance</button></td>
<td><button style="font-size:15px;" type="submit" name="pre-View_attend" value="<?php echo $row["WorkloadID"]; ?>" class="btn btn-primary">View Attendance</button></td>
<input type="hidden" name="subjectID_hidden" value="<?php echo $row["subjectID"]; ?>">
</tr>
<?php } mysqli_close($connect);?>
</tbody>
/*
I'm getting the workload ID accurately, but the subject ID is incorrect.
I believe I'm getting the last subject ID that my query produced
*/
<?php
if(isset($_POST['pre-View_attend']))
{ $FWID=$_POST['pre-View_attend'];
$FSJID=$_POST['subjectID_hidden'];
echo"Workload ID: $FWID SubjectID: $FSJID";
}
?>
You get the last value because you have a bunch of hidden fields with the same name.
You could name them subjectID_hidden[<?=$row['WorkloadID'];?>] to get an array to then get the subjectID by WorkloadID:
$subjectID = $_POST["subjectID_hidden"][$_POST["pre-View_attend"]];
You need to query the table for something unique to the row you want to retrieve information from. This unqiue data should already be existing on the page, encoded into the button/form.
Example;
<?php
if(isset($_POST['pre-View_attend'])) {
global $db;
$select = "select * from mytable where unique='$unique'";
$connect = mysqli_query($db, $select);
while($row=mysqli_fetch_array($connect)) {
$myfirstVariable = $row['myfirstVariable'];
$mysecondVariable = $row['mysecondVariable'];
$show = "$myfirstVariable $mysecondVariable";
}
}
?>
Then you can call $show when you want in the HTML.
p.s. there are quite a few errors in your html like;
<input type="hidden" name="subjectID_hidden" value="<?php echo $row["subjectID"]; ?>">
should be
<input type="hidden" name="subjectID_hidden" value="<?php echo $row["subjectID"]; ?>" />
self closing at the end with forwards slash.

can't delete from specific row from generate table from mySQL

Hye, i'm working on a small project of Inventory system. Everything is okay until this last part. I have included a delete button at the end of each row for user to delete any item of choice, but when user press the delete button, it deleted the last row of the table, instead of the row/item of choices, where is my mistake in my coding?
Thank you!
<?php
$result= mysql_query("SELECT * from Staff ORDER BY status");
$count=1;
while($row=mysql_fetch_array($result)){
?>
<tr>
<td align="left"><?php echo $count; ?></td>
<td align="left"><?php echo $row['staffId']; ?></td>
<td align="left"><?php echo $row['name']; ?></td>
<td align="left"><?php echo $row['address'];?></td>
<td align="left"><?php echo $row['pNum'];?></td>
<td align="left"><?php echo $row['status'];?></td>
<td align="left"><?php echo $row['type'];?></td>
<td><input type="submit" name="deleteStaff" value="Delete" onClick="displayMessage()">
<input type="hidden" name="staffId1" value="<?PHP echo $row['staffId']; ?>">
</td>
</tr>
</tbody>
<?php
$count++; }
if(isset($_POST['deleteStaff'])){
$id=$_POST['staffId1'];
$result=mysql_query("DELETE from Staff WHERE staffId='$id' ");
if($result){
echo '<script> location.replace("viewStaff.php"); </script>';
}
else{
?>
<script>
alert ("Fail to delete data")
window.location.href='viewStaff.php'
</script>
<?PHP
}
}
?>
I've encountered this error before as well. I think the error is happening since you're calling the same "onclick" function for each array result.
Instead, create a link to a page where you can run the php deletion script and pass the staffID into the URI and Get that in the php deletion script that you created.
example
<form action="deletion_script.php?staffid=<?php echo $staffid; ?>">
Put that inside of the td tag
Then in your deletion_script.php file, put something like this
<?php
//get staffid from URI
$staffid = $_GET['staffid'];
$sql="DELETE FROM $table_name WHERE staffid = '$staffid'"
$result = mysqli_query($connect, $sql);
if($result) {
//send back to the page you want
header("Location: ../inventory.php");
}
?>
This worked for me, and how I handle all situations like this from now on. You'll need to adjust the code a little to fit your set up.
since the name for the hidden field is the same for all rows being generated the post request is pulling the value of the last row( also "staffId1" ).
I recommend either using a individual form for each row in which case your code doesn't change as much or use js to get the required staffID using selectors
If you include the same hidden field (StaffId1) for every record it will contain more then one value.
You are now listening to the pressed SUBMIT so you don't need the hidden field. You can just get the value if the pressed SUBMIT.
Button
type="submit" name="staffId" value="<?php echo $id ?>"
Php
$id=$_POST['staffId'];

show div on checkbox checked php

i have a list of records from a recordset :
<?php while($row = mysqli_fetch_array($result_imprint))
{ ?>
<tr><td><input type="checkbox" id="location_<?php echo $row['location_id']; ?>" name="location_[<?php echo $row['location_id']; ?>]" value="location_<?php echo $row['imprint_location']; ?>"/> </td>
<td><?php echo $row['imprint_location']; ?></td>
<td><?php echo $row['max_size']; ?></td>
<td><?php echo $row['imprint_type']; ?> </td>
<td> <?php echo $row['max_colours']; ?></td>
<td><?php echo $row['setup_fee']; ?></td></tr>
<?php } ?>
I can't figure out how to show a div if someone checks a box from one of these records. there are a max of six records, I have the div's already done out :
<div class="location_1" id="location_1" style="display: none;">
<br /><hr class="style-seven">
Content Here
</div>
This is the code i used but it doesn't seem to work :
$(document).ready(function(){
$('#location').change(function(){
if(this.checked)
$('#location_[<?php echo $row['location_id']; ?>]').fadeIn('slow');
else
$('#location_[<?php echo $row['location_id']; ?>]').fadeOut('slow');
});
});
any help much appreciated.
thanks
For starters... IDs have to be unique on your page. So you cannot have a checkbox with id="location_1" and then have a div with also an id of "location_1"
So have checkboxes with id "location_X" and your divs with "div_location_X"
I would also stop using php on javascript, unless you do a ajax call to a php script or you do a loop to write all the javascript for each of the checkboxes, but it would be simpler to:
when checked (use a class to group all the checkboxes together), you can get javascipt to get the id attribute of the checkbox
<input type="checkbox" id="location_1" name="location_1" class="check"/> </td>
$(document).ready(function(){
$('.check').change(function(){
if((.check).is(":checked"))
var div_name = $(this).attr('id'); //this should come back with location_1 for the first checkbox
$('#div_'+div_name+'').fadeIn('slow'); // prepend div_ for the corrent unique id name
else
$('#div_'+div_name+'').fadeOut('slow');
});
});
Use chrome tools console for debugging
currently your php code will get interpreted as a string.
try changing your code to:
$('#location_['+<?php echo $row["location_id"]; ?>+']').fadein('slow')
and respectively for fade out.
<tr><td>
<input type="checkbox" id="location_<?php echo $row['location_id']; ?>" name="location_[<?php echo $row['location_id']; ?>]" class="check"/> </td>
$(document).ready(function(){
$('#location').change(function(){
if((.check).is(":checked"))
$('#location_[<?php echo $row['location_id']; ?>]').fadeIn('slow');
else
$('#location_[<?php echo $row['location_id']; ?>]').fadeOut('slow');
});
});
1.You can't have same id for div and check box,so i am changing div id to location_div
$(document).ready(function(){
$(":checkbox").click(function () {
if($("input[id^='location_']:checkbox:checked").length>0)
{
$("#location_div").removeAttr('style');
}
else
{
$("#location_div").css({"display":"none"});
}
});
});

Is POST information from jquery $.post sent in parent page

I have a parent page which has a drop down list in it. using the onchange event, data is posted to a second page using $.post(). This page uses the posted variables to output mysql data with a checkbox for each line. This page output is then inserted into the parent page using jquery $('#DIV name').html(output).show();
The user can then see the mysql table rows with corresponding checkboxes. They then select the checkboxes they want and say delete. Delete is a form submit button.
My question is, when they click delete how do I take the form data from the second page and post it with that of the parent page so that I can then use $_POST[] to get the checkbox info and delete the selected table rows?
example of the parent page code is:
javascript/jquery
<script type="text/javascript">
function get(row){ //row being processed, defined in onchange="get(x)"
('getpeopleinjobs.php',{ //data posted to external page
postvarposition: form["position"+row].value, //variable equal to input box, sent to external page
postvarjob: form["job"+row].value, //variable equal to input box, sent to external page
postvarperson: form["person"+row].value, //variable equal to drop down list, sent to external page
postrow: row}, //variable equal row being processed, sent to external page
function(output){
$('#training'+row).html(output).show(); //display external results in row div
//popupWindow = window.open('t4.php?variable1Name=Vicki&variable2Name=Maulline','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
});
}
</script>
Form data is
<tr>
<td>
<?PHP echo $i; ?>
</td>
<td>
<input type=text NAME="position<?PHP echo $i; ?>" id="position<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" value="<? echo mysql_result($resultpositionjob,$r,0);?>">
</td>
<td>
<input type=text NAME="job<?PHP echo $i; ?>" id="job<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" value="<? echo mysql_result($resultpositionjob,$r,1);?>">
</td>
<td>
<SELECT NAME="person<?PHP echo $i; ?>" id="person<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" onchange="get(<? echo $i; ?>);">
<OPTION VALUE=0 >
<?=$optionpeople?>
</SELECT>
</td>
<td onclick="train(<? echo $i; ?>);" style="color:grey; cursor: pointer;">
<div id="training<?PHP echo $i; ?>"><font color=grey size=2></div>
</td>
<td>
</td>
</tr>
<?PHP
$i++;
$r++;
}
?>
The second page or page called by jquery, the output is:
echo
"
<table border=0 width=400>
<tr>
<td width=20>
</td>
<td width=150>
<b>Position<b>
</td>
<td>
<b>Job<b>
</td>
</tr>
";
while($line = mysql_fetch_array($result))
{
echo "
<tr>
<td>
";
?>
<input type=checkbox name="delete[]" id="delete[]" value="<?php echo $rows['id']; ?>">
<?PHP
echo "
</td>
<td>
";
echo $line['position'];
echo "
</td>
<td>
";
echo $line['job'];
echo "
</td>
</tr>
";
}
?>
<tr>
<td>
<input type=submit name="update" id="update" value="Update">
</td>
</tr>
</table>
<?PHP
}
To repeat I want the table checkbox element from the second page posted with the form data of the parent page.
Is this possible as my testing hasnt given me any luck.
Am I doing something wrong or do I need to modify the jquery code?
Thanks as always for the assistance.
There is no second page. Really - you're loading HTML content from the second page, but it's being inserted into the parent page. All content, from your browsers perspective, is in the same page. The fields should be included as long as they're inside the DOM inside the element. Use the developer tools for your browser or Firebug for Firefox to make sure that the content is placed within the form element in the DOM. The developer tools should also be able to show you exactly which variables are submitted to the server when the form is submitted.
The 'action' parameter of 'form' will indicate where the content gets submitted (and it seems you've left that part out of your HTML, so it's impossible to say if you've left out or just not included it in the paste.

Categories