i'm a beginner at angularJS 2 and i'm trying to create a login form using PHP as a back-end tech. I have written the following code in app.component.ts
import {Http, Response, HTTP_PROVIDERS} from 'angular2/http';
import {Injectable, Component } from 'angular2/core';
import 'rxjs/add/operator/map'
@Component({
selector: 'my-app',
templateUrl: 'app/loginForm.component.html',
providers:[HTTP_PROVIDERS]
})
export class AppComponent {
private data;
constructor(private http:Http){
}
submit(){
this.getData();
}
getData(){
this.http.get('http://localhost/AngularWithPhpTest/').
map(data => data.json()).subscribe(data =>console.log(data));
}
}
and here's my loginForm.component.html
<form (ngSubmit)="submit()" action="index.php" method="post">
<div class="control-group">
<label for="username">Username </label>
<input type="text" id="username" class="form-control" name="username"
value="xxx">
</div>
<div class="control-group">
<label for="username">password </label>
<input type="password" id="password" name="password"class="form-control"
value="xxx">
</div>
<input type="submit" class="btn btn-primary" name="submit" value="Log-In">
</form>
and finally this is the php code :
header("Access-Control-Allow-Origin: *");
$con=mysql_connect('localhost','root','') or die("Failed to connect to
MySQL:
" . mysql_error());
$db=mysql_select_db('test',$con) or die("Failed to connect to MySQL: " .
mysql_error());
function SignIn() {
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
echo $username." ".$password;
if(!empty($_POST['username']))
{
$query = mysql_query("SELECT * FROM user where username='$username'
AND password = '$password'") or die(mysql_error());
$row = mysql_fetch_array($query) or die(mysql_error());
if(!empty($row['username']) AND !empty($row['password']))
{
$_SESSION['username'] = $row['password'];
echo "SUCCESSFULLY LOGIN TO USER PROFILE PAGE...";
$uname=$row['username'];
$pass=$row['password'];
$data = array('username'=>$uname ,'password' => $pass);
echo json_encode($data);
}
else {
echo "SORRY... YOU ENTERD WRONG ID AND PASSWORD... PLEASE RETRY...";
}
}
}
if(isset($_POST['submit'])) {
SignIn();
}
These are the errors i get :
angular2.dev.js:23083 STACKTRACE:
BrowserDomAdapter.logError @ angular2.dev.js:23083
ExceptionHandler.call @ angular2.dev.js:1187
(anonymous) @ angular2.dev.js:12591
NgZone._notifyOnError @ angular2.dev.js:13635
onError @ angular2.dev.js:13539
Zone.run @ angular2-polyfills.js:1247
(anonymous) @ angular2.dev.js:13558
zoneBoundFn @ angular2-polyfills.js:1220
angular2.dev.js:23083 SyntaxError: Unexpected token ? in JSON at position 8
at JSON.parse (<anonymous>)
at Function.Json.parse (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:359:27)
at Response.json (http://localhost:3000/node_modules/angular2/bundles/http.dev.js:290:36)
at MapSubscriber.eval [as project] (http://localhost:3000/app/app.component.js:33:109)
at MapSubscriber._next (http://localhost:3000/node_modules/rxjs/bundles/Rx.js:5039:31)
at MapSubscriber.Subscriber.next (http://localhost:3000/node_modules/rxjs/bundles/Rx.js:10667:14)
at XMLHttpRequest.onLoad (http://localhost:3000/node_modules/angular2/bundles/http.dev.js:660:30)
at Zone.run (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:1243:24)
at Zone.run (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:13558:32)
at XMLHttpRequest.zoneBoundFn (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:1220:26)
-----async gap-----
Error
at _getStacktraceWithUncaughtError (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:2244:29)
at Zone.fork (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:2293:47)
at Zone.bind (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:1218:53)
at XMLHttpRequest.obj.addEventListener (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:1503:95)
at Observable._subscribe (http://localhost:3000/node_modules/angular2/bundles/http.dev.js:681:14)
at Observable.subscribe (http://localhost:3000/node_modules/rxjs/bundles/Rx.js:11006:29)
at Observable._subscribe (http://localhost:3000/node_modules/rxjs/bundles/Rx.js:11038:26)
at Observable.subscribe (http://localhost:3000/node_modules/rxjs/bundles/Rx.js:11004:29)
at AppComponent.getData (http://localhost:3000/app/app.component.js:33:131)
at AppComponent.submit (http://localhost:3000/app/app.component.js:30:26)
-----async gap-----
Error
at _getStacktraceWithUncaughtError (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:2244:29)
at Zone.fork (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:2293:47)
at arguments.(anonymous function) (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:1671:82)
-----async gap-----
Error
at _getStacktraceWithUncaughtError (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:2244:29)
at Zone.fork (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:2293:47)
at NgZone._createInnerZone (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:13546:39)
at new NgZone (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:13412:32)
at createNgZone (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:12475:12)
at PlatformRef_.application (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:12550:31)
at Object.bootstrap (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:24805:64)
at execute (http://localhost:3000/app/boot.js:14:23)
at ensureEvaluated (http://localhost:3000/node_modules/systemjs/dist/system.src.js:3186:26)
at Object.execute (http://localhost:3000/node_modules/systemjs/dist/system.src.js:3304:13)
BrowserDomAdapter.logError @ angular2.dev.js:23083
ExceptionHandler.call @ angular2.dev.js:1188
(anonymous) @ angular2.dev.js:12591
NgZone._notifyOnError @ angular2.dev.js:13635
onError @ angular2.dev.js:13539
Zone.run @ angular2-polyfills.js:1247
(anonymous) @ angular2.dev.js:13558
zoneBoundFn @ angular2-polyfills.js:1220
angular2.dev.js:359 Uncaught SyntaxError: Unexpected token ? in JSON at position
8
at JSON.parse (<anonymous>)
at Function.Json.parse (angular2.dev.js:359)
at Response.json (http.dev.js:290)
at MapSubscriber.eval [as project] (app.component.ts:22)
at MapSubscriber._next (Rx.js:5039)
at MapSubscriber.Subscriber.next (Rx.js:10667)
at XMLHttpRequest.onLoad (http.dev.js:660)
at Zone.run (angular2-polyfills.js:1243)
at Zone.run (angular2.dev.js:13558)
at XMLHttpRequest.zoneBoundFn (angular2-polyfills.js:1220)