Jump to content

alidayan

New Members
  • Posts

    8
  • Joined

  • Last visited

alidayan's Achievements

Member

Member (2/5)

0

Reputation

  1. Thank you I will try to do like that.
  2. How to do this inside javascript? Thank you but I have problem while sending json data to PHP i think. This is what oResponse = JSON.parse(xhr.responseText); gives me: [object Object]. And php sends error while trying to insert data to mysql <?php class Subtasks{ // database connection and table name private $conn; private $table_name = "myExmpTable"; public $t_id; public $name; // constructor with $db as database connection public function __construct($db){ $this->conn = $db; } function create(){ $query = "INSERT INTO ". $this->table_name ." (id, name) VALUES ('".$this->t_id."', '".$this->name."')"; // prepare query $stmt = $this->conn->prepare($query); // execute query if($stmt->execute()){ $this->id = $this->conn->lastInsertId(); return true; } return false; } } ?>
  3. Hi, I am trying to send input Don't, can't or anything else which contains '. But Ajax sends it like: t_name=Don%27t&_=1589636831048 and then I got an error while trying to insert to mysql. I only have problem with '. I tried lots of thing but something is missing. I couldn't figure it out. Could somebody help me? My Ajax: var t_name = document.getElementById(mydiv).value; $.ajax( { type: "GET", url: './api/objects/add.php', async:false, cache:false, contentType: "application/x-www-form-urlencoded;charset=ISO-8859-1", dataType: 'json', data: { t_tsk: id, t_name: t_name }, success: function (result) { }, error: function(xhr, status, error) { document.getElementById("errorDiv").hidden = false; if (xhr.responseText) { } else { } } }); My url when I write don't to input area: /add.php?t_tsk=1&t_name=Don%27t&_=1589636831048 My php code: <?php // include database and object files include_once '../config/database.php'; include_once '../objects/myObject.php'; // get database connection $database = new Database(); $db = $database->getConnection(); $myObject = new MyObject($db); $myObject->t_id = isset($_GET['t_tsk']) ? $_GET['t_tsk'] : die(); $myObject->name = isset($_GET['t_name']) ? utf8_decode($_GET['t_name']) : die(); if($myObject->create()){ http_response_code(200); $myObject_arr=array( "status" => true, "message" => "Successfully created!", "id" => $myObject->id, "name" => $myObject->name ); } else{ http_response_code(401); $myObject_arr=array( "status" => false, "message" => "Error!" ); } print_r(json_encode($myObject_arr)); ?> Thank you all.
  4. Thank you I had changed Post to Get from php that didn't worked but now I changed POST from AJAX to GET that worked. Thank you so much.
  5. When I run login.php?username=alidayan93@gmail.com&password=12345678 I got: {"status":true,"message":"Successfully Login!","id":"6","username":"alidayan","email":"alidayan93@gmail.com"} as response If I call api/user/login.php from another page called login.php as well I got error which is attached
  6. Sorry for late response. Yes I did. I got: array(5) { ["status"]=> bool(true) ["message"]=> string(19) "Successfully Login!" ["id"]=> string(1) "6" ["username"]=> string(8) "alidayan" ["email"]=> string(20) "ali***@gmail.com" } And if send wrong password/username I got: array(2) { ["status"]=> bool(false) ["message"]=> string(29) "Invalid Username or Password!" }
  7. Hi, Ajax POST requests returns 200 OK, but fires error. I couldn't figure out why. Could anybody help me? Javascript: function login(){ $.ajax( { type: "POST", url: './api/login.php', dataType: 'json', data: { username: $("#username").val(), password: $("#pass").val() }, success: function (result) { if (result['status'] == true) { window.location.href = './welcome.php'; } else { alert(result['message']); } }, error: function(xhr, status, error) { if (xhr.responseText) { oResponse = JSON.parse(xhr.responseText); alert(oResponse.error.message) } else { alert("Status: "+status+"\nXhr: "+JSON.stringify(xhr)+"\n\nError: " + JSON.stringify(error)); } } }); } PHP: <?php include_once '../config/database.php'; include_once '../object/acc.php'; $database = new Database(); $db = $database->getConnection(); $user = new acc($db); $user->username = isset($_GET['name']) ? $_GET['name'] : die(); $user->password = isset($_GET['pass']) ? $_GET['pass'] : die(); $stmt = $user->login(); if($stmt->rowCount() > 0){ $row = $stmt->fetch(PDO::FETCH_ASSOC); if(password_verify($user->password, $row['pass'])){ http_response_code(200); // create array $arr = array( "status" => true, "message" => "Successfully Login!", "id" => $row['id'], "username" => $row['name'], "email" => $row['email'] ); } else{ http_response_code(401); $arr = array( "status" => false, "message" => "Invalid Username or Password!" ); } } else{ http_response_code(401); $arr = array( "status" => false, "message" => "Invalid Username or Password!" ); } echo json_encode($arr); ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.