javascript - AJAX function behaving weird -


i have ajax function supposed pass on data php code supposed enter data database happening ajax not working way should.

             <?php               session_start();     include_once("../engine/database.php");     if (isset($_post["name"],$_post["password"]) ) {             $uname  = htmlspecialchars(mysql_real_escape_string($_post["name"]) );       $passwd = htmlspecialchars(mysql_real_escape_string($_post["password"]) );       $query = "select * users username ='".$uname."' , password='".$passwd."' limit 1";       $query= mysql_query($query);       if(mysql_num_rows($query) == 1) {                 $_session['admin'] = "welcome admin panel {$_post['name']} ";         $_session['onoff'] = 'on';        ?>              <!doctype html>       <html>       <head>           <meta charset="utf-8">           <meta http-equiv="x-ua-compatible" content="ie=edge">           <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.3.0/pure-min.css">           <script type="text/javascript" src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script>            <script>           tinymce.init({               selector: "textarea#postcont",               plugins: [                   "advlist autolink lists link image charmap print preview anchor",                   "searchreplace visualblocks code fullscreen",                   "insertdatetime media table contextmenu paste"               ],               toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"           });         </script>         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript" charset="utf-8" async defer></script>            <title>admin panel</title>            </head>       <body>                 <div class="pure-g-r" style="letter-spacing:0em; padding-left:2em; padding-right:2em;">               <div class="pure-u-5-10">                                               <form class="pure-form pure-form-stacked" action='../engine/engine.php' method='post'>                 <fieldset>                            <legend><?php echo  $_session['admin']; ?></legend>                   <div style="margin:0 auto;">                                             <input type='text' name='posttitle' class="pure-input-1" placeholder='article title'/>                                                   </div>                   <textarea  class="pure-input-1" name='postdes' name='postdes' cols='102' placeholder='article summary'></textarea>                                     <textarea  id="postcont" name='postcont' cols='60' rows='10'></textarea>                                     <br />                   <button type="submit" name="blogsubmit" class="pure-button pure-input-1 pure-button-primary">submit</button>                                   </fieldset>                               </form>           </div>           <div class="pure-u-1-10">            </div>           <div class="pure-u-4-10">                                                <h2 style="margin:0 auto;">posted articles</h2>                   <dl>                       <dt>how create text editor</dt>                       <dd><a href=""><small><a href="">edit</a></small> | <small><a href=""> delete</a></small></a>                       <dt>how create text editor</dt>                       <dd><a href=""><small><a href="">edit</a></small> | <small><a href="">delete</a></small></a>                                                    </dl>                                           </div>                           </div>         <script type="text/javascript">                 function submit() {                       var xhr = new xmlhttprequest();                       try {                         xhr = new new activexobject("msxml2.xmlhttp");                       }                       catch (e) {                         try {                           xhr = new activexobject("microsoft.xmlhttp");                         }                         catch (e) {                           alert(" please upgrade broswer");                             return false;                         }                       }                       xhr.onreadystatechange = function () {                           if (xhr.readystate === 4) {                                 if (xhr.status === 200) {                                      alert(xhr.responsetext);                                                                                    } else {                                             alert("dang!!!");                                             return false;                                       }                           } else {                                 alert("dang!!!! 2 buahahaha");                                 return false;                           }                         var title = document.getelementbyid('posttitle').value;                        var postdes = document.getelementbyid('postdes').value;                                                                var postcont = document.getelementbyid('postcont').value;                                                                   var data = "title=" + title + "&summary=" + postdes + "&content=" + postcont;                                                                    xhr.open("post", "../engine/engine.php",true );                        xhr.setrequestheader("content-type","application/x-www-form-urlencoded");                        xhr.send(data);                                          }                                        }            </script>         </body>     </html>           <?php   }       else {           echo "username or password incorrect";           die(mysql_error());        }     }    ?> 

it supposed pass on code

                 <?php include_once('database.php'); if (isset($_post['blogsubmit'])  ) {     $title   =  mysql_real_escape_string($_post['posttitle']);     $summary =  mysql_real_escape_string($_post['postdes']);     $content =  mysql_real_escape_string($_post['postcont']);     insertblogpost($title,$summary,$content); }  function insertblogpost($title,$summary,$content) {      $query = "insert `blogposts` set             `title`     = '$title',             `summary`   = '$summary',                            `content`   = '$content' ";                                          mysql_query($query) or die(mysql_error());     echo "yaaaay";       } ?> 

the problem page prints yaaay part instead of alert. have tried whatever know of fix problem, appreciate help. have tried switching between true , false post didn't help.

ps: sorry using mysql_* , i'm still not comfortable using pdo yet. many in advance.

from can see never bind submit-function submit-event of form. function never called, , form doing regular post ../engine/engine.php page, why see results on new page.

you should add following attribute form tag:

onsubmit="return submit()" 

and in order override regular form post function can work instead must return false function.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

debian - 500 Error upon login into Plesk Admin - auth.php3? -