javascript - why it doesn't display "you had wrong" message? -
often use ajax jquery according below code why doesn't display "you had wrong" message when "if"is true? , display "faile" message?
file login.php
<!doctype html> <html lang="en" class="no-js"> <head> <title>just test</title> </head> <div id="response" ></div> <h3>login</h3> <form id="login" class="form-login"> <input type="text" class="form-control" name="yourname" id="yourname" placeholder="yourname"> <button type="submit" class="btn btn-bricky pull-left"> ورود <i class="fa fa-arrow-circle-right"></i> </button> </form> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <script> $(document).ready(function (e){ $("#login").on('submit',(function(e){ e.preventdefault(); $.ajax({ url: "include/php/check_login.php", type: "post", data: new formdata(this), contenttype: false, cache: false, processdata:false, success: function(data) { if(data=='faile'){ $("#response").html('you had wrong...'); }else{ $("#response").html(data); } return false; } }); })); }); </script> </body><!-- end: body --> </html>
file:check_login.php
<?php if (isset($_post['yourname']) && !empty($_post['yourname'])) { $yourname=$_post['yourname']; echo $yourname; } else echo "faile"; ?>
here method receiving json based data
<!doctype html> <html lang="en" class="no-js"> <head> <title>just test</title> </head> <body> <div id="response" ></div> <h3>login</h3> <form id="login" class="form-login"> <input type="text" class="form-control" name="yourname" id="yourname" placeholder="yourname"> <button type="submit" class="btn btn-bricky pull-left"> ورود <i class="fa fa-arrow-circle-right"></i> </button> </form> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <script type="text/javascript"> $(function (e) { $("#login").submit(function(e){ e.preventdefault(); $.ajax({ url: "check_login.php", data: new formdata(this), type: "post", contenttype: false, cache: false, processdata:false, // data type should set json response data json value datatype: 'json', success: function(json) { if( json.data == 'faile') { $("#response").html('you had wrong...'); } else { $("#response").html(json.data); } return false; } }); }); }); </script> </body> </html>
check_login.php
<?php session_start(); if (isset($_post['yourname']) && !empty($_post['yourname'])) { $response = array( "data" => $_post['yourname']); } else {$response = array( "data" => "faile");} echo json_encode($response); ?>
using json, there should no error regarding \n
:: new line after echo
Comments
Post a Comment