AJAX enters data twice in database - javascript

I have a function where a user can a report a post, the user clicks the report button, and then promptly enters the information for the report. This works, the problem is if the page hasn't reloaded and the user decided to report a second post the data for that report enters the database twice. Why is this?
Here's the code:
$(document).ready(function() {
$(".report_post").click(function(e) {
var nid = $(this).attr("id");
$("#report_reason").dialog({
resizable: false,
height: 300,
width: 400,
modal: true,
});
$('.submit_report_post').click(function() {
var content = $("#report_content").val();
var type = "Post";
if ($('input[name="report"]:checked').length > 0 && (content != null &&
content != "")) {
var complaint = document.querySelector('input[name="report"]:checked').value;
alert('Reported!');
$.ajax({
type: 'POST',
url: 'php/report_post.php',
data: {
type: type,
nid: nid,
reason: complaint,
content: content,
},
cache: false,
success: function(data) {
$("#report_content").val("");
$("input[name='report']").prop('checked', false);
//$("#report_reason").dialog('close');
}
});
} else {
alert('Fill all of the information!');
}
});
e.preventDefault();
});
});

You're submitting your form twice, once the normal way and once via AJAX. You have e.preventDefault(); in your code which would normally stop the typical non-AJAX submission, however you never created the e argument.
Change:
$('.submit_report_post').click(function() {
to
$('.submit_report_post').click(function(e) {
and this will make the form only submit through the AJAX code.

You are binding click on $('.submit_report_post') every time you click on $(".report_post"), you need to do it outside of first bind
$(document).ready(function() {
$(".report_post").click(function(e) {
var nid = $(this).attr("id");
$("#report_reason").dialog({
resizable: false,
height: 300,
width: 400,
modal: true,
});
e.preventDefault();
});
$('.submit_report_post').click(function() {
var content = $("#report_content").val();
var type = "Post";
if ($('input[name="report"]:checked').length > 0 && (content != null &&
content != "")) {
var complaint = document.querySelector('input[name="report"]:checked').value;
alert('Reported!');
$.ajax({
type: 'POST',
url: 'php/report_post.php',
data: {
type: type,
nid: nid,
reason: complaint,
content: content,
},
cache: false,
success: function(data) {
$("#report_content").val("");
$("input[name='report']").prop('checked', false);
//$("#report_reason").dialog('close');
}
});
} else {
alert('Fill all of the information!');
}
});
});

Related

How could i access the id of element on whose class emojionearea is being append

I am using emojionearea in my input type based on class name. For submitting the data with AJAX I need to send the id of the input type. When I tried to use $(this).attr('id'); to get the id it says undefined.
Please explain how can access the id with help of $(this), because there are more than 1 fields and the id is being generated dynamically .
I have also tried to access the id with $(this).prev().attr('id');, $(this).parent().attr('id'); etc.
Thanks in advance
$(".emojionearea").emojioneArea({
pickerPosition: "right",
tonesStyle: "bullet",
hidePickerOnBlur: true,
events: {
keyup: function(editor, event) {
this.hidePicker()
if (event.which == 13) {
var reciver_id = id;
var message = editor.html();
if (message != '' || message != 'undefined') {
request = $.ajax({
url: "/send-message",
type: "POST",
'async': false,
data: {
'message': message,
'reciver_id': reciver_id,
'_token': CSRF_TOKEN
},
});
request.done(function(res) {
if (res) {
editor.html('');
var text = 'text';
// ths.parent().parent().children().find('.chat-scroll').append(element);
$(this).find('.classname').append(text);
}
});
request.fail(function(jqXHR, textStatus) {
console.log("Request failed: " + textStatus);
});
}
}
}
}
});

Validation DropDownList outside grid

I have kendo dropdownlist and button submit. I want if the user not select anything in dropdown(position), there will be validation that inform the user must select one position at least at dropdown. Then, if the user has click the position, so the user can submit the data. I have used some method like "required" but not working.
HTML
<input id="dropdown" style="width:200px;" />
JavaScript for kendoDropDownList (position)
$("#dropdown").kendoDropDownList({
optionLabel: "- Select Position -",
dataTextField: "functionName",
dataValueField: "hrsPositionID",
dataSource: {
transport:{
read: {
url: "./testjson.php",
type: "POST",
data: function() {
return {
method: "getDropdown",
}
}
},
},
},
change: function(e){
console.log(this.value());
// $('#AccountingTree').data('kendoTreeView').homogeneous.read();
homogeneous1.read();
homogeneous2.read();
homogeneous3.read();
homogeneous4.read();
homogeneous5.read();
homogeneous6.read();
homogeneous7.read();
homogeneous8.read();
homogeneous9.read();
homogeneous10.read();
homogeneous11.read();
homogeneous12.read();
homogeneous13.read();
homogeneous14.read();
}
}).data('kendoDropDownList');
dropdownlist = $("#dropdown").data("kendoDropDownList");
For dropdownlist above, i"m using homogeneous data (treeview).
Anyone have any idea or reference on this question?
JavaScript AJAX call for submit button
//AJAX call for button
$("#primaryTextButton").click(function(){
if($("#dropdown").data("kendoDropDownList").value() == ""){
kendo.alert("Please select position.");
}
});
$("#primaryTextButton").kendoButton();
var button = $("#primaryTextButton").data("kendoButton");
button.bind("click", function(e) {
var test = $("#dropdown").val()
$.ajax({
url: "../DesignationProgramTemplate/testjson.php",
type: "POST",
data: {
method: "addTemplate" ,
id: test,
progid: array
},
success: function (response) {
if(response === "SUCCESS")
{
kendo.alert("Data saved");
}else
{
kendo.confirm("Update the data?")
.done(function(){
$.ajax({
type: "POST",
url: "../DesignationProgramTemplate/testjson.php",
data: {
method: "deleteTemplate" ,
id: test,
progid: array
},
success: function(){
kendo.alert("Data updated");
}
});
});
}
},
});
});
When you click the button, just get the value of the kendoDropDownList and do a
conditional statement. Hope this helps. Happy Coding ;D
//AJAX call for button
$("#primaryTextButton").kendoButton();
var button = $("#primaryTextButton").data("kendoButton");
button.bind("click", function(e) {
var test = $("#dropdown").data("kendoDropDownList").value();
if(test == ""){
kendo.alert("Please select position.");
}
else{
$.ajax({
url: "../DesignationProgramTemplate/testjson.php",
type: "POST",
data: {
method: "addTemplate" ,
id: test,
progid: array
},
success: function (response) {
if(response === "SUCCESS"){
kendo.alert("Data saved");
}
else{
kendo.confirm("Update the data?")
.done(function(){
$.ajax({
type: "POST",
url: "../DesignationProgramTemplate/testjson.php",
data: {
method: "deleteTemplate" ,
id: test,
progid: array
},
success: function(){
kendo.alert("Data updated");
}
});
});
}
},
});
}
});

event.preventDefault(); only works some of the time with submit form

I am using the Mailchimp API to submit a form. The goal is to prevent the default callback provided by Mailchimp. The majority of the time event.preventDefault() is behaving as it should. Then randomly it will not work:
$(function () {
var $form = $('#mc-embedded-subscribe-form');
$('#mc-embedded-subscribe').on('click', function(event) {
if(event) event.preventDefault();
register($form);
});
});
function register($form) {
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
cache : false,
dataType : 'json',
contentType: "application/json; charset=utf-8",
error : function(err) { alert("Could not connect to the registration server. Please try again later."); },
success : function(data) {
if (data.result != "success") {
// Something went wrong, do something to notify the user. maybe alert(data.msg);
var message = data.msg
var messageSh = data.msg.substring(4);
if (data.msg == '0 - Please enter a value' || data.msg == '0 - An email address must contain a single #') {
$('#notification_container').html('<span class="alert">'+messageSh+'</span>');
} else {
$('#notification_container').html('<span class="alert">'+message+'</span>');
}
} else {
// It worked, carry on...
var message = data.msg;
$('.popup-promo-container').addClass('thanks');
$('.checkboxes, #mc_embed_signup_scroll').addClass('hidden');
$('.complete-promo').html(message).removeClass('hidden');
setTimeout(function() {
document.querySelector('.popup-promo').style.display = "none";
},20000);
}
}
});
}
Try
take off ready function.
remove if on event
Code:
var $form = $('#mc-embedded-subscribe-form');
$('#mc-embedded-subscribe').on('click', function(event) {
event.preventDefault();
register($form);
});

How to refresh jTable after adding record w/o page refreshing

Here is my add driver button
$( "#addDriver" ).button().click(function( event ) {
event.preventDefault();
var dn = $("input[name=driver_name]").val();
var ds = $("input[name=driver_status]").val();
var dataString = 'driver_name='+ dn + '&driver_status='+ ds;
//Here is where im trying to load it but it's not working
$('#driversTable').jtable('load');
if(dn == '' || ds == '' )
{
alert("Fill up all Fields with \'*\'");
}
else
{
$.ajax({
type: "POST",
url: "processDriver.php",
data: dataString,
dataType: "json",
success: function(data){
if(!data.error && data.success) {
alert(data.successMsg);
$("input[name=driver_name]").val('');
$("input[name=driver_status]").val('');
} else {
alert(data.errorMsg);
}
}
});
}
});
And here is my jTable init
$("#driversTable").jtable({
title: 'Drivers',
actions: {
listAction: "getAllDrivers.php",
},
fields: {
did: {
key: true,
list: false
},
dID: {
title: 'Driver ID',
width: '7%'
},
dName: {
title: 'Name',
width: '7%'
},
dStatus: {
title: 'Status',
width: '12%'
}
}
});
$('#driversTable').jtable('load');
I want it to be right after adding a new record i want the table to refresh without refreshing the page. I tried the load and reload functions of jtable is not working or am i just putting them on the wrong place of my code?
Insert Data First Then Reload.
$.ajax({
type: "POST",
url: "processDriver.php",
data: dataString,
dataType: "json",
success: function(data){
if(!data.error && data.success) {
alert(data.successMsg);
$('#driversTable').jtable('reload');//<== Reload after Success
$("input[name=driver_name]").val('');
$("input[name=driver_status]").val('');
} else {
alert(data.errorMsg);
}
}
});

How can I assign an HTML5 data value to an element?

I have the following code:
$.modal({
title: title,
closeButton: true,
content: content,
complete: function () {
applyTemplateSetup();
$('#main-form').updateTabs();
$('#main-form').data('action',action);
// updated to line below but still does not work
$('#main-form').data('action','Edit');
},
width: 900,
resizeOnLoad: true,
buttons: {
'Submit': function (win) {
formSubmitHandler($('#main-form'));
},
}
Once my data is loaded I am trying to set the data attribute action. I then have more code that reads it in the submit handler:
var formSubmitHandler = function (form) {
//e.preventDefault();
var $form = form;
var val = $form.valid();
if (!$form.valid || $form.valid()) {
var submitBt = $(this).find('button[type=submit]');
submitBt.disableBt();
var sendTimer = new Date().getTime();
$.ajax({
url: $form.attr('action'),
dataType: 'json',
type: 'POST',
data: $form.serializeArray(),
success: function (json, textStatus, XMLHttpRequest) {
json = json || {};
if (json.success) {
if ($form.data('action') == "Edit") {
$('#modal').removeBlockMessages()
submitBt.enableBt();
} else {
However it seems the value is not being set correctly as when I step through the code this is not getting a true value: $form.data('action') == "Edit". Am I doing something wrong?
This might be the issue.
First you do - $('#main-form').data('action',action);
I'm not entirely sure what the actionvariable holds, but from this line of your code - url: $form.attr('action') I'm going to assume you're giving the form action value. Why you do that is your concern, but I do believe it does not == "Edit".
What value do you get in your $form.data('action') ?

Categories

Resources