prevent postBack - jquery/javascript - javascript

I have a gridview with 2 template buttons: (1) Approve and (2) Rejected..When one of the buttons is clicked the status of the row will change to either Approve or Rejected and update the 'Status' field in the database. I am using this in money transactions.
What i want to happen is to prevent the page to postback when either of the button is clicked. How can i do this in jquery? How can i get the status of the row from the database then pass it to jquery so that when i click the button it will not post back( so that the payment of a customer will only be added once to the currenct balance of the merchant ).
Please help, I have no idea on how to do this...

You can do it with Ajax. To post some data without postback.

When render a table row, you add some id to row like this
<table>
<tr data-id="1">
<td>Name</td>
<td><button class="approve">Aprove</button><button class="reject">Reject</button></td>
</tr>
</table>
Then use jquery, ajax to catch button press
$('.approve').click(function(){
var id = $(this).parents('tr').data('id');/// Here is your id
$.post('/approve/',{id:id}, function(data){
})
});

Related

Submitting data without clicking a submit button

I want to create a table with rows full of inputs. The idea is that each row of inputs ties to one object on the backend. I want a row to be "submitted" once the user is no longer entering data into it.
I'm mostly a backend engineer bumbling with the front end, so I'm wondering what are some approaches to get the following effect:
I want to call a backend "action" (in the MVC sense) when a user stops inputting in a current row and is then on the next row.
I've read about onblur and onfocusout, but neither seem to do what I want. Onblur is on a per-input basis, and onfocusout is triggered each time an input is left even if a user is tabbing through the same row. Further, onfocusout isn't supported by firefox yet.
My app is grails, but I'm open to circumventing the standard g:form (grails form) practice and using some sort of javascript solution. I just don't know what that would be.
Any discussion would be helpful, as I'm trying to learn more about frontend solutions in general. If there are any javascript frameworks that make this easier, let me know about them.
you can use javascript to submit a form when ever you want. You can attach the submit form function to any event (like user ideal, blur any other event you want). Then if any of the event happens, you can call that function which will submit the form. see the example below
function idealUser(){//you call that function according to requirement
setTimeout(function(){
submitForm();
},5000);
}
function submitForm(){
document.getElementByID('formId').submit();
}
You could store the starting values of each row in javascript. Add a function for the onfocus event to the rows to check if the focused row changes.
Then when the focused row changes, the js can check if any other row has different values than the saved values. If a row that is out of focus does have different values than what was previously saved, then submit the changed row info to the backend.
Also update the stored row values in the js after the change is sent to the server.
Validate and submit when moving out of the table row.
Here's some code and a demo. I triggered a button click when you leave the row but you may use ajax to call your server side code.
$("table").on("blur", ".row", function(e){
//check if all input fields are filled
var cols = $(this).find(".col");
var filled = true;
cols.each(function(i, v){
if($(v).val().length==0){
filled = false;
}
});
//if not moving out of the last input field then don't submit
if(e.target !== $(this).find("input").last()[0]){
return;
}
//If filled trigger click for submission
if(filled){
//in reality, you may use ajax here to call your backend code
$("#submit-btn").trigger("click");
}
});
$("#submit-btn").on("click", function(){
alert("Submit Clicked!");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<tr id="row1" class="row">
<td><input class="col" value=""></td>
<td><input class="col" value=""></td>
<td><input class="col" value=""></td>
</tr>
<tr id="row2" class="row">
<td><input class="col" value=""></td>
<td><input class="col" value=""></td>
<td><input class="col3"value=""></td>
</tr>
</table>
<input type=button value=submit id=submit-btn>

How to prevent PHP Post Max Size being exceeded on a Form submission

I have a FORM on a PHP web page that accepts a table of data (editable-grid) which the end-user is in control of in that they can add as many rows as they like. One of the fields is also a TEXTAREA which means each row has a variable size.
If the post is too large however, it will be rejected by the server and all the target page will see is an empty $_POST. Worse, if the user navigates back to the input page, it will show its initial empty state, losing all data entered and annoying the end-user to say the least.
I could increase the POST_max_size setting in PHP, but all that will do is push the boundary at which it will fail.
I could also check in JavaScript what the size of the post will be, PRIOR to them submitting the form, ideally as they add each row or change the textarea content, but I'm not sure how accurate or slow that will be. It also means they would have to remove some data or rows.
The only other option I can think of is to submit each row individually in a hidden FORM, through AJAX, once they click the Submit button. I'm not sure it is a good idea to replace one post with hundreds of posts to the server.
Any other ideas?
Instead of sending the entire datagrid you can just send the edited row as if it were a single form using javascript. If you send each row in a loop with ajax even if it is not edited you can choke the server in the same way as if you were sending the entire datagrid. Instead of sending 20k rows of data with just one column modified, just send one row when the user leaves the textarea.
I don't know how you manage the editing events but with jQuery (for the sake of simplicity) could be something like this.
$(document).ready(function() {
$('textarea, input').on('blur', function(e) {
var $row = $(this).closest('tr')
var data = {
id: $row.data('id'),
input: $row.find('td input').val(),
textarea: $row.find('td textarea').val()
};
// e.g. foo.php/1
$.post("foo.php/" + data.id, data)
.success(function() { /* silent success */ })
.fail(function() { alert('Error') })
});
});
with html
<table>
<tbody>
<tr data-id="1">
<td><input /></td>
<td><textarea></textarea></td>
</tr>
<tr data-id="2">
<td><input /></td>
<td><textarea></textarea></td>
</tr>
<tr data-id="3">
<td><input /></td>
<td><textarea></textarea></td>
</tr>
</tbody>
</table>
example jsfiddle https://jsfiddle.net/vt0a0udx
ps: sorry for the bad english.
try to change this value in php.ini
max_input_vars = 1000 to 10000

Create a popup window based that displays records based on the value supplied form parent window.

I am working with a form that shows treatment history of a patient in a tabular format and it displays date,time,treatment amount. one td contains a view button.
<td align='left' width='90px'><input type='button' class='btn_class' id=\"".$row['ipd_pat_trt_id']."\" value='view'/></td>
the view button contains treatment_id (field from treatment table) value as its id .
Now what i want that when a user clicks on view button it should open a popup window and pass the treatment_id value to the popup window. And the popup window should display all the record from treatment table('select * from' is ok), based on the treatment_id value.
Any one can help me please..
Thanks in advance.
A simple jQuery script would work, but you could just use a regular <form> post:
<table>
<tr>
<td>
<button class="btn_class" id="300">View</button>
</td>
</tr>
</table>
<script>
// On click
$(".btn_class").click(function() {
// Assign the button's id value
var Id = $(this).attr('id');
// Open a new window with a $_GET query into blank page
window.open('?id='+Id,'_blank');
});
</script>

Show/hide table rows based on button click

I want to use a toggle button next to a table to hide or show table rows based on the whether or not a checkbox in the last column of the table is checked.
Here is the code for the table:
<table>
<tr>
<td>1<td>
<td>Text<td>
<td><input type="checkbox" class="form-checkbox" checked="checked"><td>
</tr>
<tr>
<td>2<td>
<td>Text<td>
<td><input type="checkbox" class="form-checkbox"><td>
</tr>
</table>
In the sidebar I have this button:
<a id="comtoggle" class="btn" href="#">Toggle</a>
In the above example, I would like for row 1 to disappear if the Toggle button is clicked, but have it reappear if the toggle button is clicked again. The state of the checkbox is stored in a database and is saved via an AJAX function (which works fine).
Here is the jQuery I have. I think my problem is using the .is(':checked') in the 3rd line.
$("#comtoggle").click(function() {
if ($('.form-checkbox').is(':checked')) {
$('.form-checkbox').is(':checked').closest('tr').toggle();
}
});
With this snippet, all of my rows disappear, and the console of Firebug shows this error:
TypeError: $target.offset(...) is null
Not sure if it is related.
I suppose this is what you mean. Catch the fiddle aswell: http://jsfiddle.net/7C423/1
All you need to do is just grab that what you are interested in and play the ball.
$("#comtoggle").click(function() {
$(".form-checkbox:checked").closest("tr").toggle();
});
Since you are selecting elements by class, you need to loop trough them and in case they are checked hide a row.
$("#comtoggle").click(function() {
$('.form-checkbox').each(function(chk){
if($(this).is(':checked'))
$(this).closest("tr").toggle();
});
});

JavaScript - confirmation box. | Yes? > delete data from MySQL / No - do nothing

I don't have much experience with JavaScript. However I wanted to do a small thing with JavaScript and MySQL. And I could use some help.
I have a page in PHP which search for something and it's gives the results based on the search query.
For each result it adds 3 images, one which as a URL where you can view the content. Other where you can edit that content.
And the third one you can delete.
For that I wanted to do something nice.
Like, the user clicks the image, a confirmation dialog appears. In that box it asks if you sure you want to delete the data.
If yes, it would delete the data. where ID =
The ID is printed in the onclick action, inside the JavaScript function in the image using PHP echo.
If not, we would close the dialog and continue.
OK, so let's assume the following (forgive me for re-clarifying the question):
You have a number of rows of some form, with delete links, and you want to confirm that the user actually wants to delete it?
Let's assume the following HTML:
<tr>
<td>Some Item 1</td>
<td>Delete</td>
</tr>
<tr>
<td>Some Item 2</td>
<td>Delete</td>
</tr>
<tr>
<td>Some Item 3</td>
<td>Delete</td>
</tr>
So I'm assuming the same PHP script can run the delete, picking up on the mode parameter:
<?php
if($_GET['mode'] == 'delete') {
//Check if there is something in $_GET['id'].
if($_GET['id']) {
//Prevent SQL injection, just to be safe.
$query = "DELETE FROM sometable WHERE id='" . mysql_real_escape_string($_GET['id']) . "'";
mysql_query($query);
}
}
I'm going to give two solutions to this on the JavaScript side - the first with an inline, slightly ugly solution, the second using jQuery (http://jquery.com/), and unobtrusive JavaScript.
Ok, so for the first, I would bind on the onclick event of each link.
<tr>
<td>Some Item 3</td>
<td>Delete</td>
</tr>
Then create a JavaScript function:
//This will get called when the link is clicked.
function checkDeleteItem() {
//show the confirmation box
return confirm('Are you sure you want to delete this?');
}
As I said, I don't like that solution, because it is horribly obtrusive, and also not particularly robust.
Now, the jQuery solution:
//Do all this when the DOM is loaded
$(function() {
//get all delete links (Note the class I gave them in the HTML)
$("a.delete-link").click(function() {
//Basically, if confirm is true (OK button is pressed), then
//the click event is permitted to continue, and the link will
//be followed - however, if the cancel is pressed, the click event will be stopped here.
return confirm("Are you sure you want to delete this?");
});
});
I heartily recommend this solution over the previous one, since it is much more elegant and nice, and is generally best practice.
Can't help you with the php part, but you can use JavaScript's Confirm:
var ok = confirm('Are you sure you want to delete this row');
//ok is true or false
You can bind a function to the click event on your delete buttons. Returning false will cause them to ignore the click.
If you have a button that deletes the row you want, then call the function below to confirm user if he wants to delete :
function confirmDelete()
{
if (confirm('Do you want to delete ?'))
{
return true;
}
else
{
return false;
}
}
call it like that :
<input type="button" value="Delete Record" onclick="confirmDelete()" />
What you need is a basic HTML form that will submit and do the deletion. Get the HTML right for this first - I would suggest each image has its own form with a hidden field for the ID value and the image uses an image button: <input type="image" />
Once you have this working you can add the JavaScript warning dialogue. You will need to replace the form's submit event with your own function that prevents the form from submitting (by returning false) and then show your dialogue. If the user clicks yes, then you'll need to trigger the forms onsubmit() event with JavaScript and if the user clicks no then just hide the dialogue.
Also, have a read about unobtrusive JavaScript - DOM Scripting by Jeremy Keith is a fanastic book that will simply explain how to do this stuff.

Categories

Resources