javascript - Can't able to insert data into database using jQuery & AJAX in php -
i can insert data database using pure php , mysql. can't able insert data database using jquery & ajax in php. 1 error showing ajax error: i.e please check network connection. please me solve problem.
my jquery ajax code, php code, html code given below.
html --- news.php
<div id="error"></div> <form method="post" class="form-horizontal" role="form" name="newsform"> <div class="form-group"> <label class="control-label col-sm-2">news heading</label> <div class="col-sm-10"> <input type="text" class="form-control" id="newsheading" name="newsheading" placeholder="enter news heading"> </div> </div> <!--news heading--> <div class="form-group"> <label class="control-label col-sm-2">news source url</label> <div class="col-sm-10"> <input type="text" class="form-control" id="newssource" name="newssource" placeholder="enter news source url"> </div> </div> <!--news source--> <div class="form-group"> <label class="control-label col-sm-2">news content</label> <div class="col-sm-10"> <textarea class="form-control" rows="3" id="newscontent" name="newscontent" placeholder="news content"></textarea> </div> </div><!--news data--> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" id="postnews" name="postnews" class="btn bg-red">submit</button> </div> </div> <!--submit button--> </form>
if write simple php codes in news.php works & inserts data database.
<?php if(isset($_post['postnews'])===true) { $heading = $_post['newsheading']; $source = $_post['newssource']; $content = $_post['newscontent']; $query = mysql_query("insert `news`(`news_heading`,`news_source`,`news_content`) values('$heading','$source','$content')"); } ?>
the below scripts show ajax network connection error. can't insert data database.
jquery & ajax
<script> $(document).ready(function(){ $("#postnews").click(function(){ var newsheading = $("#newsheading").val(); var newssource = $("#newssource").val(); var newscontent = $("#newscontent").val(); var datastring = 'newsheading='+newsheading+'&newssource='+newssource+'&newscontent='+newscontent; alert(datastring); // if($.trim(newsheading).length>0 && $.trim(newssource).length>0 && $.trim(newscontent).length>0) { $.ajax({ type: "post", url: "core/news-post-process.php", data: datastring, cache: false, beforesend: function(){ $("#error").html("<div class='alert alert-primary bg-primary'><a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a><strong>please wait, processing.</strong> </div>"); $("#postnews").html("<i class='fa fa-spinner fa-pulse'></i><span class='sr-only'>loading...</span> posting...."); }, success: function(data){ if(data=="success"){ $("#error").html("<div class='alert alert-success bg-green'><a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a><strong>successfully inserted</strong> </div>"); } else{ $("#error").html("<div class='alert bg-red'><a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a><strong>can't insert</strong> </div>"); } }, error: function(xmlhttprequest, textstatus, errorthrown) { if (xmlhttprequest.readystate == 4) { // http error (can checked xmlhttprequest.status , xmlhttprequest.statustext) $("#error").html("<div class='alert bg-yellow'><a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a><strong>http request error</strong> </div>"); } else if (xmlhttprequest.readystate == 0) { // network error (i.e. connection refused, access denied due cors, etc.) $("#error").html("<div class='alert bg-yellow'><a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a><strong>please check network connection</strong> </div> "); } else { // weird happening $("#error").html("<div class='alert bg-yellow'><a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a><strong>some error occured</strong> </div>"); } } }); } }); }); </script>
core/dbconnect.php
<?php error_reporting( e_all & ~e_deprecated & ~e_notice ); if(!mysql_connect("localhost","root","")) { die('oops connection problem ! --> '.mysql_error()); } if(!mysql_select_db("admin")) { die('oops database selection problem ! --> '.mysql_error()); } ?>
core/news-post-process.php
<?php session_start(); include_once 'dbconnect.php'; if(isset($_post['newsheading']) && isset($_post['newssource']) && isset($_post['newscontent'])) { $newsheading = mysql_real_escape_string($_post['newsheading']); $newssource = mysql_real_escape_string($_post['newssource']); $newscontent = mysql_real_escape_string($_post['newscontent']); /*$newsheading = trim($newsheading); $newssource = trim($newssource); $newscontent = trim($newscontent);*/ $query = mysql_query("insert `news`(`news_heading`,`news_source`,`news_content`) values('".$newsheading."','".$newssource."','".$newscontent."')"); if ($query) { echo "sucess"; } else{ echo "failed"; } } ?>
why jquery , ajax won't work ??
change form tag this....
<form class="form-horizontal" role="form" name="newsform"> </form>
when comes using ajax post data , u notice writing method in ajax call like:
$.ajax({ method:"post", });
so don't need in form tag mention post method
Comments
Post a Comment