473,378 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to use / combine 2 jquery scripts that use two different datatypes

348 100+
I have one jquery function that submits data via a datastring to my server. This works great. On success, I currently have a little alert box that lets the user know that the data was stored.

I now have a second function that gets back a json response for a success or failure from the server and will echo to the user whether the data was saved successfully or not. This function should replace the alert box in the bottom most function.

I don't quite know how these two functions should fit together. Independantly, they work great but when I try and combine them, nothing works.

I'd sure appriciate any help on this. Thanks.

Here are the functions that I want to combine.

Expand|Select|Wrap|Line Numbers
  1. $('#Cust').submit(function() {
  2.     var data = $(this).serialize();
  3.     var url = $(this).attr('action');
  4.     var method = $(this).attr('method');
  5.     $.ajax({
  6.         url: url,
  7.         type: method,
  8.         data: data,
  9.         dataType: 'json',
  10.         success: function(data) {
  11.             var $div = $('<div>').attr('id', 'message').html(data.message);
  12.             if(data.success == 0) {
  13.                 $div.addClass('error');
  14.             } else {
  15.                 $div.addClass('success');
  16.             }
  17.             $('body').append($div);
  18.         }
  19.     });
  20.     return false;
  21. });
  22.  
  23.  
  24.  
  25. $(function() {
  26.   $('.error').hide();
  27.   $(".Btn").click(function() {
  28.     $('.error').hide();
  29.       var Phone = $("input#Phone").val();
  30.         var dataString = '&Summ='+ Summ + '&Dtl='+ Dtl;
  31.         $.ajax({
  32.       type: "POST",
  33.       url: "index.php",
  34.       data: dataString,
  35.       success: function(){
  36.         $('#Cust input[type=text]').val('');
  37.         alert( "Success! Data Saved");
  38.       }
  39.      });
  40.     return false;
  41.     });
  42. });
May 22 '09 #1
3 3666
acoder
16,027 Expert Mod 8TB
You mean you want to make another Ajax request when the bottom function is successful?
May 23 '09 #2
fjm
348 100+
@acoder
Hi Acoder. Yes, exactly.

If you look, you will see an alert() in the success part of the lower function. What I really want to do is replace that alert with a nice dynamic css styled div to tell the user that their data was successfully received. That's really all I'm doing. :)

I just don't know how to tie these functions together. Both the upper and lower functions use different datatypes. One uses json and the other uses datastring. :/

Thanks for any help you can provide.

EDIT:

I think I need to just put this out there so there isn't any confusion. The url in the lower function (url: "index.php") is only for getting the database values and importing them into the textboxes for display. I can't use this same url for the json server callback. It would have to be a new url.
May 23 '09 #3
acoder
16,027 Expert Mod 8TB
If you want to simply replace the alert with a styled div, then replace the alert with the following lines:
Expand|Select|Wrap|Line Numbers
  1. var $div = $('<div>').attr('id', 'message').html("Success! Data saved");
  2.     $div.addClass('success');
  3. }
  4. $('body').append($div);
If you want to replicate the Ajax request triggered on submit, take the following three lines:
Expand|Select|Wrap|Line Numbers
  1. var data = $(this).serialize();
  2. var url = $(this).attr('action');
  3. var method = $(this).attr('method');
and replace 'this' with the form, e.g. "#formid".
May 25 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Prodip Saha | last post by:
Dear ASP.NET Gurus, I have a TextBox control with AutoPostBack set to true to execute the server scripts. I also, added some client script for validation.What I want is--execute the client script...
1
by: mikeh3275 | last post by:
I'm new to developing in jQuery, so I'm probably doing this wrong. I'm loading a form into a modal dialog box and I'm trying to assign a click event to the button in the form, but I can't seem to...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
3
by: NetWave | last post by:
Hi, For my next project I'm going to need Ajax, so I'm in about to read some books on ASP.NET Ajax. While reading Rick Strahl's blog I stumbled upon jQuery. I've been to the website and seen...
6
by: lorlarz | last post by:
Although I believe your criticisms of jQuery are without merit, I have tried to see the fuss in a positive light. I, thusly, have decided that perhaps there is a need for YET further transparency...
20
by: Aaron Gray | last post by:
There does not seem too be anyway to test if two jQuery references are the same element. Given :- ... <div id="1"></div .... Then :- alert( $("#1") == $("#1"))
26
by: RobG | last post by:
Do some of the regulars here need to re-think their (sometimes strident) opposition to libraries? Both Microsoft and Nokia have announced support for jQuery. It seems to have gained quite a bit...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: metaphysics | last post by:
I am using a jQuery script called LocalScroll, and I have a quick problem that needs fixing. The demo for the script is here: jQuery.LocalScroll - Regular Demo, and the documentation is here: Ariel...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.