I am developing a Phonegap Cordova application and I want to POST data to server using AJAX but am not able to, am getting an error.
My example code is:
<script>
$(document).ready(function()
{
$('#frm').submit(function()
{
var username = $('#textinput').val();
var username = $.trim(username);
var password = $('#passwordinput').val();
var password = $.trim(password);
{
alert('Please enter username');
return false;
}
else if(password =='')
{
alert('Please enter password');
return false;
}
else
{
var user = $('[name=username]').val();
var pass = $('[name=password]').val();
$.ajax({
type: 'POST',
url: 'http://eqfree***p.com/log_sb.php',
rossDomain: true,
beforeSend : function() {$.mobile.loading('show')},
complete : function() {$.mobile.loading('hide')},
data: { username:'user', password:'pass'},
dataType: 'json',
success: function(data){
alert(data.success);
alert('success');
},
error: function(){
alert('error!');
}
});
return false;
}
});
});
</script>
try this one
<script>
$(document).ready(function()
{
$('#frm').submit(function()
{
var username = $('#textinput').val();
var username = $.trim(username);
var password = $('#passwordinput').val();
var password = $.trim(password);
else if(username =='')//check condition
{
alert('Please enter username');
return false;
}
else if(password =='')
{
alert('Please enter password');
return false;
}
else
{
//no need to re initialize
//var user = $('[name=username]').val();
//var pass = $('[name=password]').val();
$.ajax({
type: 'POST',
url: 'http://eqfree***p.com/log_sb.php',
crossDomain: true,
beforeSend : function() {$.mobile.loading('show')},
complete : function() {$.mobile.loading('hide')},
data: { 'username':username, 'password':password},
dataType: 'json',
success: function(data){
alert(data.success);
alert('success');
},
error: function(){
alert('error!');
}
});
return false;
}
});
});
</script>
Related
I know it has been answered but since Im ignorant with javascript and ajax, need help. I had found a code for crud operations. My problem is that html verification is not working. When the modal submits the form the required fields in html cannot block the form operation not to be submited. Can anyone help with the script? Here is the code for all operations:
$(document).on('click','#btn-add',function(e) {
var data = $("#user_form").serialize();
$.ajax({
data: data,
type: "post",
url: "save.php",
success: function(dataResult){
var dataResult = JSON.parse(dataResult);
if(dataResult.statusCode==200){
$('#modal_task').modal('hide');
alert('Η εργασία καταχωρήθηκε με επιτυχία!');
window.location.reload(true);
}
else if(dataResult.statusCode==201){
alert(dataResult);
}
}
});
});
$(document).on('click','.update',function(e) {
var id=$(this).attr("data-id");
var name=$(this).attr("data-name");
var email=$(this).attr("data-email");
var phone=$(this).attr("data-phone");
var city=$(this).attr("data-city");
$('#id_u').val(id);
$('#name_u').val(name);
$('#email_u').val(email);
$('#phone_u').val(phone);
$('#city_u').val(city);
});
$(document).on('click','#update',function(e) {
var data = $("#update_form").serialize();
$.ajax({
data: data,
type: "post",
url: "backend/save.php",
success: function(dataResult){
var dataResult = JSON.parse(dataResult);
if(dataResult.statusCode==200){
$('#editEmployeeModal').modal('hide');
alert('Data updated successfully !');
location.reload();
}
else if(dataResult.statusCode==201){
alert(dataResult);
}
}
});
});
$(document).on("click", ".delete", function() {
var id=$(this).attr("data-id");
$('#id_d').val(id);
});
$(document).on("click", "#delete", function() {
$.ajax({
url: "backend/save.php",
type: "POST",
cache: false,
data:{
type:3,
id: $("#id_d").val()
},
success: function(dataResult){
$('#deleteEmployeeModal').modal('hide');
$("#"+dataResult).remove();
}
});
});
$(document).on("click", "#delete_multiple", function() {
var user = [];
$(".user_checkbox:checked").each(function() {
user.push($(this).data('user-id'));
});
if(user.length <=0) {
alert("Please select records.");
}
else {
WRN_PROFILE_DELETE = "Are you sure you want to delete "+(user.length>1?"these":"this")+" row?";
var checked = confirm(WRN_PROFILE_DELETE);
if(checked == true) {
var selected_values = user.join(",");
console.log(selected_values);
$.ajax({
type: "POST",
url: "backend/save.php",
cache:false,
data:{
type: 4,
id : selected_values
},
success: function(response) {
var ids = response.split(",");
for (var i=0; i < ids.length; i++ ) {
$("#"+ids[i]).remove();
}
}
});
}
}
});
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
var checkbox = $('table tbody input[type="checkbox"]');
$("#selectAll").click(function(){
if(this.checked){
checkbox.each(function(){
this.checked = true;
});
} else{
checkbox.each(function(){
this.checked = false;
});
}
});
checkbox.click(function(){
if(!this.checked){
$("#selectAll").prop("checked", false);
}
});
});
This my code for send realtime message.
After send message, it is not display unless i refresh the page.
I want this chat box message to show the new message entered.
This is to show the user List In the left side of the page:
$(".user-w").click(function() {
var user_id = $(this).attr("id");
// alert(user_id);
$.ajax({
type: "POST",
url: "<?php echo base_url('Message/getMessageByLists/')?>",
data: {
'user_id': user_id
},
datatype: 'html',
success: function(data) {
$("#loadMessages").html(data);
}
}); });
This is Send function:
$("#sendMessage").click(function(e)
{e.preventDefault();// alert("test");
var msg = $("#message_text").val();
var touser = $("#touser").val();
//var reservation_id = $("#reservation_id").val();
if (!msg || msg.length == 0) {
alert("enter a message");
} else {
$.ajax({
type: "POST",
url: "<?php echo base_url('Message/addMessage')?>",
// data:{'msg' : msg, 'touser' : touser, 'reservation_id' : reservation_id},
data: {
'msg': msg,
'touser': touser
},
datatype: 'text',
// Page.Server.ScriptTimeout = 300;
success: function(data) {
if (data == 1) {
//$("#loadMessages").load();
$("#message_text").val("");
} else {
alert("noo eroor chat message");
}
},
});
//return false;
} }); // End Send Function
if I am not wrong then, their might be an element with class as "user-w", also that element have "id" attribute too. So you can just execute following line, after sending the message to user.
$(".user-w[id='"+ touser +"']").trigger("click");
above line needs to placed in "success" method, like as below:
if (data == 1) {
//$("#loadMessages").load();
$("#message_text").val("");
$(".user-w[id='"+ touser +"']").trigger("click");
} else {
alert("noo eroor chat message");
}
There was a problem connecting Google Recaptcha v2. Or rather with the form. The check works fine, the error message displays, but, the message is still sent.
How can I fix this?
This is a form of feedback:
$(document).ready(function() {
$("#fast-call_submit").bind("click", function(e) {
e.preventDefault();
var form = $('#feedback_form');
var fields = form.serialize();
$.ajax({
url: form.attr('action') + '.json',
type: 'post',
data: fields,
dataType: 'json',
complete: function() {},
success: function(response) {
var v = grecaptcha.getResponse();
if (v.length == 0) {
$('.w-form-re-fail').show();
return false;
} else {
if (response.status == 'ok') {
$('.w-form-done').show();
$('.w-form-fail').hide();
form.hide();
} else {
$('.w-form-fail').show();
}
$('.w-form-re-fail').hide();
return true;
}
}
});
});
});
edit-txt,updatePassword are two buttons in a single form superAdminDetailsForm . i want to valide this form in both button click. I wrote form validation on each button click .but it doesn't work. edit-txt button is type of text and update password is type of button
$().ready(function() {$('#edit-txt').on('click', function() {
$("#superAdminDetailsForm").validate({
rules: {
superadminEmail:{
required:true,
email:true
},
},
messages: {
superadminEmail:"Please enter your Email id"
},
submitHandler: function(){
var formData =$("#superAdminDetailsForm").serialize();
$.ajax({
url:'/accounts',
type:'post',
datatype: 'json',
data: formData,
success : function(response){
if(response == "true"){
window.location='/accounts';
} else {
alert("false")
}
},
error: function (request,status, error) {
}
});
return false;
}
});
});
$('#updatePassword').on('click', function() {$("#superAdminDetailsForm").validate({
rules: {
oldPassword:"required",
newPassword:"required",
confirmpassword:{
require :"true",
equalTo : "#oldPassword"
}
},
messages: {
oldPassword:"Please enter Old Password ",
newPassword: "Please enter New Password",
confirmpassword:"Retype password is incorrect"
},
submitHandler: function(){//to pass all data of a form serial
var formData =$("#superAdminDetailsForm").serialize();
$.ajax({
url:'/changePassword',
type:'post',
datatype: 'json',
data: formData,
success : function(response){
if(response == "true"){
window.location = '/accounts';
} else {
alert("password incorrect");
}
},
error: function (request,status, error) {
}
});
return false;
}
});
});
});
I am using this code:
function submitForm() {
$.ajax({type:'POST',
url: 'index.php',
data:$('#ContactForm').serialize(),
success: function(response) {
$('#ContactForm').find('.form_result').html(response);
}}
);
return false;
}
It works well but how do I go about adding a validation for example ... to alert if username or password is empty?
I know there's a lot of these similar scripts around but want to add it to this one in particular.
Use beforeSend()
function submitForm() {
$.ajax({type:'POST',
url: 'index.php',
data:$('#ContactForm').serialize(),
success: function(response) {
$('#ContactForm').find('.form_result').html(response);
}},
beforeSend: function(){
if(!$("#name").val()){
alert("name field is empty.");
return false;
}
}
);
return false;
}
function submitForm() {
if(!$("#name").val()){
alert("name field is empty.");
return false;
}
$.ajax({type:'POST',
url: 'index.php',
data:$('#ContactForm').serialize(), success: function(response) {
$('#ContactForm').find('.form_result').html(response);
}});
return false;
}
you have to perform validation before the ajax calling statement
for example
function submitForm() {
var username=$('#id').value;
if(username!='')
{
$.ajax({type:'POST',
url: 'index.php',
data:$('#ContactForm').serialize(), success: function(response) {
$('#ContactForm').find('.form_result').html(response);
}});
}
else
{
alert("Please Enter Username");
}
return false;
}
You might want to do something like this
function submitForm() {
if (inputIsValid()) {
sendAjaxRequest();
} else {
ShowMessage();
}
}
function sendAjaxRequest(parameters) {
$.ajax({
type: 'POST',
url: 'index.php',
data: $('#ContactForm').serialize(),
success: function (response) {
$('#ContactForm').find('.form_result').html(response);
}
});
}
function inputIsValid() {
return $("#username").val() && $("#password").val();
}
function ShowMessage() {
alert("Please provide username and password");
}
Use this link for reference http://jsfiddle.net/RH8Dy/ to view the example or below given Script
function submit()
{
var username = document.getElementById("txtusername").value;
var password = document.getElementById("txtpassword").value;
if(username == "")
{
alert("enter username");
}
if(username == "")
{
alert("enter username");
}
else if(password == "")
{
alert("Enter Password");
}
else
{
//ajax call
}
}