Need javascript confirm box dialogue & delete images when press ok - php

<?php
require "../db/dbconfig.php";
$gal=mysql_query("select * from gallery");
$numrows=mysql_num_rows($gal);
if($numrows>=1)
{
echo "<form action='delete.php' method='post' name='f2' id='f2'>";
echo '<table id="rqst" style="display:block;" border="0" cellpadding="12" cellspacing="3" width="500px">';
echo "<tr><th>Tick to select</th><th>Images in Gallery</th></tr>";
while($row=mysql_fetch_array($gal)
{
$imgfile=$row['ImgName'];
$Image="<img src=gallery/".$imgfile." width='230px' height='150px'/>";
$img_name=$imgfile;
echo "<tr>";
echo "<td><input type='checkbox' name='imgs[]' value='$img_name'></td><td>".$Image."</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan='3' align='right'>";
echo "<input type='submit' value='Delete' name='del'></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
?>
This is my code....This will display images from gallery and checkboxes associated with them. When I click delete button with unchecked checkboxes an alert should come like this "Please check at least one checkbox"..How to do that??
My next problem is,,when I click delete button after checked checkbox, alert should come like this=" Do you want to delete?? "...If clicked Ok,the image must be deleted else do nothing...Please help ...Thanks in advance....

check this below link for validation using jquery:
http://jsfiddle.net/susheel61/U3Unk/2/
<form id="form">
First name: <input type="checkbox" name="firstname" class="check"><br>
<button>test</button>
</form>
$("button").on("click",function(e){
var status = $(".check").is(":checked");
e.preventDefault();
if(!status){
alert("this is not checked");
}
});

Yes, you can do it by javascript or jquery to validate whether your atleast one checkbox is select or not. So, for that you need to give a common class for all checkbox as example
<input type="checkbox" name="firstname" class="addchk">
Now in your submit button call a javascript function which validate the matter.
<input type='button' value='Delete' name='del' onclick='delete_checked()' />
Now write a function to validate whether any checkbox is selected.
function delete_checked()
{
var status = $(".addchk").is(":checked");
e.preventDefault();
if(!status){
alert("this is not checked");
}
else
{
// SUbmit yout form
}
}

Related

Differentiate between multiple buttons on a table. html php

I am creating an edit edit/delete user table and have created an 'edit' button for each record populated in the table. I want to do several things.
1. when an edit button is pressed for a specific user, open a new page called "EDIT."
2. populate form controls in the "EDIT" page with the corresponding user information for the specific 'edit' button that was pressed.
My question is, how do I differentiate between which button is pressed on the users table?
this is what my table looks like:
And this is the code for generating the table and buttons.
if (!$_REQUEST['search']) {
$sql = "SELECT * FROM users_table ORDER BY lname ASC";
$retvalues = mysqli_query($conn, $sql);
$counter = 1;
while ($row = mysqli_fetch_array($retvalues, MYSQL_ASSOC)) {
$lname = capword($row['lname']);
$fname = capword($row['fname']);
if ($row['admin'] != 1) {
$stringAdmin = "No";
$admincolor = "<td>";
} else {
$stringAdmin = "Yes";
$admincolor = "<td style='color:red;'>";
}
echo "<tr>";
echo "<td>".$counter.".</td>";
echo "<td>".$lname." , ".$fname."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['password']."</td>";
echo $admincolor.$stringAdmin."</td>";
echo "<td><input type='checkbox' name='user[]' value='{$row['id']}'></td>";
echo "<td><input type='submit' name='edit' value='edit'></td>";
echo "</tr>";
$counter++;
}
}
You can differentiate the each edit button by using unique id
<button class='edit' data-id="<?php echo $_GET["id"];?>"> EDIT</button>.
While click on the edit button, read the data-id by using the following code
$(document).on('click', '.edit', function(){
var id = $(this).attr('data-id');
//code - you need to do
})
Better to show the data on next page, use bootstrap model box and AJAX request which will interact lot of the users.
$(document).on('click', '.edit', function(){
var id = $(this).attr('data-id');
if(id) {
$.ajax({
url : your url("profile/edit/"+id)
type : 'post',
success: function(response){
if (response.success) {
$('#modal').modal('show');
}
}
}):
}
});
If you change the value attribute of "Edit" submit button to the row id you can use this value to know what record id will be edited and populate it.
"<input type='submit' name='edit' value='edit'>"
change to
"<button name='edit' value='{$row['id']}'>Edit</button>"
Note that if the whole table is the form container, all inputs in table will be posted, not only the current row.
I would make the edit button into a hyperlink instead. Then there's no need to have form code. I'm assuming you don't need to pass the "delete" parameter to your edit page (as that's a different operation).
Instead of
echo "<td><input type='submit' name='edit' value='edit'></td>";
write
echo "<td><a href='edit.php?id=".$row['id']."'>Edit</a></td>";
Then in edit.php look for the variable
$_GET["id"]
and use that to search the database and display the appropriate record for editing.
P.S. If you still want your "Edit" hyperlink to look like a button it's quite easy to do that with CSS.
I did this by creating and calling a javaScript function that passes the id of the row as an argument.
echo"<tr>";
echo"<td>".$counter. ".</td>";
echo"<td>".$lname. " , " .$fname."</td>";
echo"<td>".$row['email']."</td>";
echo"<td>".$row['password']."</td>";
echo $admincolor . $stringAdmin ."</td>";
echo"<td><input type='checkbox' name='user[]' value='{$row['id']}'></td>";
echo"<td><input type='button' name='edit' value='edit' onclick='javascript:editUser(". $row['id'].");'></td>";
echo"</tr>";
the function redirects the user to an 'edit' page while passing the value of the id in the url.
function editUser(id){
window.location = "edituser.php?id="+id;
}
once on the edit page, i used $_GET to retrieve the id value and edit my entry.
You can change and add a form to your table like this:
echo "<tr>";
echo "<td>".$counter.".</td>";
echo "<td>".$lname." , ".$fname."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['password']."</td>";
echo $admincolor.$stringAdmin."</td>";
<form action="edit.php" method="post">
echo "<input type="text" name="id" value="$row['id']" style="visibilty: hidden;">";
echo "<td><input type="checkbox" name="delete"></td>";
echo "<td><input type="submit" name="edit" value="edit"></td>";
</form>
echo "</tr>"
First we have defined a form with POST method: <form action="edit.php" method="post"> and let's say edit.php will handle the request.
Then we have added a hidden form element to store and pass the user's ID: <input type="text" name="id" value="$row['id']" style="visibilty: hidden;"> It was possible to use value="..." of "edit" button but if you are dealing with passing more than one variables with POST or GET method, the trick is using an extra form element with visibility: hidden; CSS property.
Eventually, it will pass the value of $row['id'] to edit.php when the form has been submitted.
and we can handle the request in edit.php like this:
<html>
<body>
User with this ID number: <?php echo $_POST["id"]; ?> will be edited.
</body>
</html>
Official guide: http://php.net/manual/en/tutorial.forms.php
You can either use POST or GET method to pass the variables. For a detailed comparison: https://stackoverflow.com/a/504993/2104879

AJAX return, update table on page

I have the following that is generate for every record in my database
$allbills = mysql_query("SELECT * FROM outgoings WHERE outgoings.user_id = '$uid'") or die(mysql_error());
echo "<table>";
while($info = mysql_fetch_array( $allbills ))
{
echo "<tr>";
echo "<th>bill id:</th> <td>".$info['id'] . "</td> ";
echo "<th>total:</th> <td>".$info['bill'] . "</td> ";
echo "<th>bill name:</th> <td>".$info['bill_name'] . "</td> ";
echo "<th>bill deposit:</th> <td>".$info['bill_description'] . "</td> ";
echo "<th>colour:</th> <td>".$info['bill_colour'] . " </td>";
echo "<th>edit:</th> <td>
<form class='bill-upd'>
<input type='hidden' value='".$info['rand']."' name='rand2' id='rand2'>
<input type='hidden' value='".$info['id']."' name='billid' id='billid'>
Total <input type='text' id='total' name='total' /><br />
Bill name<input type='text' id='bill-name' name='bill-name' /><br />
bill descriptiion <input type='text' id='bill-description' name='bill-description' /><br />
bill colour<input type='text' id='bill-colour' name='bill-colour' />
<input type='button' value='submit' class='bill-upd-submit' />
</form>
</td>";
echo "</tr>";
}
echo "</table>";
This updates my users record in a table using AJAX
$(document).ready(function(){
$(".bill-upd-submit").click(function() {
var elem = $(this);
$.post('update_bill.php', elem.parent('.bill-upd').serialize(), function(data) {
elem.append(data);
});
});
});
Once this is done however, the user needs to refresh the page to see the results, is there a way I can populate the table the user edits, with the latest data after the update query takes place?
I would try a different approach:
1) Add an invisible div right after your button. You can do this dynamically:
$(document).ready(function() {
$(".bill-upd-submit").after("<div style='display:none'></div>");
// ...
});
2) Now your post event would be as follows:
$(".bill-upd-submit").click(function() {
var elem = $(this);
$.post("update_bill.php", elem.parent(".bill-upd").serialize(), function(data) {
elem.next("div").html(data);
});
});
BTW, I'm assuming that there are other buttons in the same page, and that's the reason for using class (bill-upd-submit) instead of id. If there is just one button, id is faster. Also, I think that bill-upd-submit is of type button. If it is type submit, I would change-it to button or added the following to the click event:
function() {
// ... the code shown above
return false;
});
This will prevent the form's submit to happen.
Looks to me like your current javascript is trying to append the form, not the table. If you're trying to add a row to the table at the bottom and keep all the other rows, then this might work. Give your table an ID in the html markup (here I use "table_id"):
$(".bill-upd-submit").click(function() {
$.post("update_bill.php", elem.parent(".bill-upd").serialize(), function(data){
$("#table_id tr:last").after(data);
});
});
And just make sure your php file (update_bill.php) has the appropriate html built into it to echo a proper tr for you.

how to send a text box value without submitting form in php?

I want to refresh the same page and display the entered value in text box on the same page after clicking the link or button.
I have the following code:
<?php
echo '<h3>Fee Payment</h3>';
echo "<form id='myFormId' name='myFormName'>";
echo " <input type='text' name='myTextField'>";
echo "<a href='{$_SERVER['PHP_SELF']}?student_no=myTextField'> Search Student</a>";
if (!(isset($_GET[student_no])))
{
echo "No Student is available.";
}
else
{
$student_no = $_GET['student_no'];
echo "Student NO:".$studen_no;
}
?>
Please guide me how to achieve the goal and whats the error my code.
<?php
echo '<h3>Fee Payment</h3>';
if(isset($_POST['myTextField']))
$value=$_POST['myTextField'];
else
$value='';
echo "<form id='myFormId' name='myFormName' method='post'>";
echo " <input type='text' name='myTextField' value='$value'>";
echo "<a href='{$_SERVER['PHP_SELF']}?student_no=myTextField'> Search Student</a>";
You also need a submit button in the form, and a form close tag.
What you want is AJAX. I am just providing you an example of how you can proceed by using jQuery, but you can use any other library / framework or any other way also. You can check this article also for more usability details.
In the main page:-
<?php
echo '<h3>Fee Payment</h3>';
echo "<form id='myFormId' name='myFormName'>";
echo "<input type='text' name='myTextField' id='myTextField' />";
echo 'Search Student';
echo "</form>";
?>
<script type="text/javascript">
$('#inline_submit_a').click(function(evt){
$.ajax({
type: "GET",
url: "handler.php",
data: {text:$('#myTextField').val()}
});
evt.preventDefault();
return false;
});
</script>
In the "handler.php" page:-
<?php
if (!(isset($_GET[student_no])))
{
echo "No Student is available.";
}
else
{
$student_no = $_GET['student_no'];
echo "Student NO:".$student_no;
}
?>
You can write all the logic related to the database in the "handler.php" page.
Hope it helps.
You're using a link to submit the form, but you should be using a submit button.
<input type="submit" value="Search student" />
You didn't close the <form> tag.
You're using $_GET[student_no] which will make PHP look for a definition of student_no. Since it's a string, express it as one. $_GET['student_no'] is better.
Why can't you submit the form? Do you want to have a link for searching instead of a button?
You could configure the link to submit the form via javascript and change the form action to GET like this:
<?php
echo '<h3>Fee Payment</h3>';
echo "<form id='myFormId' name='myFormName'
action='{$_SERVER['PHP_SELF']}' method='GET'>";
echo " <input type='text' name='student_no'>";
echo "<a href='javscript:document.forms.myFormName.submit()'>
Search Student</a>";
...

Multiple Checkboxes with same name validator

I have this form with multiple checkboxes with same name and all of checkboxes are read from a database:
echo "<FORM action='check.php' method=POST>"; ?>
<div style='color:#414141; font-size:15px;'>
<?php
while($rows=mysql_fetch_array($result)){
?>
<input name="check[]" type="checkbox" id="checkk[]" value="<? echo $rows['ID']; ?>"><? echo $rows['checkboxname']; ?>
<BR>
<?php
}
echo "</div>";
echo "<input name='' type='submit' id='get' value='Next'>";
echo "</FORM>";
I need a form validation to the next file.When I click on Submit Button(Next),show me the next page only if I checked a checkbox.I preffer a javascript validation method.
You can add a click event listener for the submit button and check if atleast one checkbox is checked and submit if true.
Something like below should work,
Try below,
echo "<input name='' type='submit' id='get' value='Next' onclick='return validateCheckbox()'>";
<script>
function validateCheckbox () {
var checkboxes = document.getElementsByName('check[]'); //selected by name since OP wanted to select by name
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
return true; // found 1 checked checkbox
}
}
return false; //none checked, so cancel submit
}
</script>

Detect click on submit button in PHP

I have a php file that contains a form (which contains 2 input boxes and a submit button) for updating a contact. I managed to fill the fields with the contact's data, but I can't detect if the submit button is clicked
form looks like this
echo "<form action=Contact.php><table>".
"<tr><td>First Name</td><td><input type=text size=75% name=FirstName value='".$row['FirstName']."'></td></tr>".
"<tr><td>Last Name</td><td><input type=text size=75% name=LastName value='".$row['LastName']."'></td></tr>".
"<tr><td colspan=2><input type=submit name=UpdateContact value=Update></td></tr>".
"</table></form>";
this code should output a "clicked" message if the button is clicked
if (isset($_POST['UpdateContact']))
{
echo "<p>clicked";
}
else
{
echo "<p>not clicked";
}
can anyone help me out or tell me what i've done wrong
(I want from the same php file to fill the contact's data in a from and to update the database)
The default method for a form is GET, so either set the form's method attribute to "post", or change your $_POST in PHP to $_GET.
echo "<form method=post action=Contact.php><table>".
"<tr><td>First Name</td><td><input type=text size=75% name=FirstName value='".$row['FirstName']."'></td></tr>".
"<tr><td>Last Name</td><td><input type=text size=75% name=LastName value='".$row['LastName']."'></td></tr>".
"<tr><td colspan=2><input type=submit name=UpdateContact value=Update></td></tr>".
"</table></form>";
or
if (isset($_GET['UpdateContact']))
{
echo "<p>clicked";
}
else
{
echo "<p>not clicked";
}
are you sure your code is reaching the php file. If yes, then the first think to check is the request type.. using $_SERVER['REQUEST_METHOD']
it will give POST in case the button was click and GET in case it the page was accessed using URL like.. google.com/Contact.php.
also, u might ned to add METHOD ="POST" in the first line of form action "form action=Contact.php

Categories