javascript - jQuery - Ajax POST seems to does not work -


here html code:

<div class="form-horizontal row-border">         <div class="form-group">             <label class="col-md-2 control-label">title:</label>             <div class="col-md-10"><input class="form-control" id="title" name="title" type="text"></div>         </div>         <div class="form-group">             <label class="col-md-2 control-label">article:</label>             <div class="col-md-10">                 <textarea name="editor1" id="editor" rows="10" cols="80">                                 let's go...                             </textarea>                  </div>         </div>                                           <div class="form-group">             <label class="col-md-2 control-label">tags:</label>             <div class="col-md-10"><input class="tags" id="tags" type="text" value="">             </div>         </div>         <div class="row" style="margin-left:92%;">         <input type="button" id="postarticle" class="btn btn-success" value="post article"></input>         </div> </div> 

here javascript code:

<script type="text/javascript">              $("#postarticle").click(function() {                                 var title = $("#title").val();             var text = $("#editor").text();             var tags = $("#tags").val();                  $.ajax({                     url: 'post_new_article.php',                     type: 'post',                     data: {                         title: title,                         tags: tags,                         text: text                     },                     datatype: 'json',                     success: function (data) {                                                           noty({text: 'mysite.com:' + data.title + ' created!', type: 'success'});                      },                       error: function(xmlhttprequest, textstatus, errorthrown) {                         noty({text: 'failure - article not created! error is: ' + errorthrown + '', type: 'error'});                       }                 });          });  </script> 

here code from: post_new_article.php:

<?php  require "include/config.php"; require "include/functions.php";  connectwithmysqldatabase();  error_reporting(e_all); ini_set('display_errors', '1');  $title = $_post['title']; $text = $_post['text']; $tags = $_post['tags']; $month = date('f'); $year = date('y'); $day = date('d');  if(isset($_post['title']))   {     mysql_query("insert `blog` (`id`, `title`, `article`, `autor`, `date`, `month`,`year`, `tags`, `image`) values ('', '$title', '$text', 'venelin vasilev', '$day','$month', '$year', '$tags', '')"); }  $result['title'][0] = $title;  echo json_encode($result); 

from things seems mysql insert function not working. can confirm connectwithmysqldatabase(); function working intended , function establishing connection mysql.

somehow seems can not insert mysql query after hit post article button. can confirm receive response post_new_article.php because receive notification title of article response. json seems read title of article.

so can me out resolve problem , make insert query mysql database ?

thanks in advance!

try this

in form submit button should be

<input type="submit" id="submit" class="btn btn-success" value="post article"></input> 

and ajax should be

<script>   $(function(){       $( "#submit" ).click(function(event)       {          event.preventdefault();          var title = $("#title").val();          var text = $("#editor").text();          var tags = $("#tags").val();            $.ajax(              {                type:"post",                datatype: 'json',                url: "./post_new_article.php",                data:{ title:title, text:text,tags:tags},                success:function(data)                {                 }             });       });   }); </script> 

edit 01

change this

mysql_query("insert blog (id, title, article, autor, date, month,year, tags, image) values ('', '$title', '$text', 'venelin vasilev', '$day','$month', '$year', '$tags', '')"); 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -