Saturday, 15 September 2012

javascript - Success in $.ajax is not get executed -


I'm a newbie for Ajax. The first example is, I wanted to implement an add operation. For the purpose, I have written the following code:

HTML:

  & lt ;! Doctype html & gt; & Lt; Html & gt; & Lt; Top & gt; & Lt; Title & gt; Add two numbers & lt; / Title & gt; & Lt; Meta content = "text / html; charset = UTF-8" http-equiv = "content-type" & gt; & Lt; Meta content = "utf-8" http-equiv = "encoding" & gt; & Lt; Script src = "jquery.js" & gt; & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Form id = "addForm" method = "post" & gt; & Lt; Input type = "text" name = "first" & gt; & Lt; Input type = "text" name = "second" & gt; & Lt; Input type = "submit" name = "submit btn" & gt; & Lt; / Form & gt; & Lt; Script src = "global.js" & gt; & Lt; / Script & gt; & Lt; / Body & gt; & Lt; / Html & gt;  

PHP:

  & lt ;? Php header ('content-type: text / html; charset = UTF-8'); $ Json = array ('success' = & gt; wrong, 'result' => 1); If (isset ($ _ POST ['first']) and release ($ _ POST ['second'])) {$ json ['success'] = "true"; $ First = $ _POST ['first']; $ Sec = $ _POST ['second']; $ Jason ['Result'] = $ First + $ Second; Echo json_encode ($ json); ? & Gt; ('Submit', function () {// alert ("submit hello")  

global.js

  $ ('# addForm ').); Var content = $ (this) .serialize (); $ Ajax ({url: 'add.php', datatype: 'jason', type: 'post', data: content, success: work (data) {if (data.success) {warning ("result" + data. result);}}}); // Alert ("Wfah");});  

The problem is that when I // warnings ("Wfah"); , then I get results in Ajax's success and then redirect to add.php. When I do not cancel it, I am redirected directly to add.php It seems that success has not been found. Please help me. Apart from this, Ajax suggests being a good source for learning. ('Submit', function).

  $ ('# addForm'). (E) {e.preventDefault (); // & lt; ----- You need to stop sending the form // alert ("Hello submit"); Var content = $ (this). Serialize (); $ .ajax ({url: 'add.php', datatype: 'jason', type: 'post', data: content, success: function (data) {if (data.success) {warning ("result is" Data .result);}}}); // Alert ("Wfah");});  

No comments:

Post a Comment