|
|
|
iOS - ทำหน้า login php mysql แต่มันไม่ส่งค่า json มาให้ค่ะ |
|
|
|
|
|
|
|
Code (Objective-C)
//
// ViewController.m
// LoginScreenTutorial
//
// Created by Dipin Krishna on 26/12/13.
// Copyright (c) 2013 Dipin Krishna. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)sigininClicked:(id)sender {
NSInteger success = 0;
@try {
if([[self.Username text] isEqualToString:@""] || [[self.Password text] isEqualToString:@""] ) {
[self alertStatus:@"Please enter Email and Password" :@"Sign in Failed!" :0];
} else {
NSString *post =[[NSString alloc] initWithFormat:@"Idname=%@&password=%@",[self.Username text],[self.Password text]];
NSLog(@"PostData: %@",post);
NSURL *url=[NSURL URLWithString:@"http://localhost:8888/loginfile.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code: %ld", (long)[response statusCode]);
if ([response statusCode] >= 200 && [response statusCode] < 300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
NSError *error = nil;
NSDictionary *jsonData = [NSJSONSerialization
JSONObjectWithData:urlData
options:NSJSONReadingMutableContainers
error:&error];
success = [jsonData[@"success"] integerValue];
NSLog(@"Success: %ld",(long)success);
if(success == 1)
{
NSLog(@"Login SUCCESS");
} else {
NSString *error_msg = (NSString *) jsonData[@"error_message"];
[self alertStatus:error_msg :@"Sign in Failed!" :0];
}
} else {
//if (error) NSLog(@"Error: %@", error);
[self alertStatus:@"Connection Failed" :@"Sign in Failed!" :0];
}
}
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
[self alertStatus:@"Sign in Failed." :@"Error!" :0];
}
if (success) {
[self performSegueWithIdentifier:@"login_success" sender:self];
}
}
- (void) alertStatus:(NSString *)msg :(NSString *)title :(int) tag
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
alertView.tag = tag;
[alertView show];
}
- (IBAction)backgroundTap:(id)sender {
[self.view endEditing:YES];
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
@end
php file
Code (PHP)
<?php
$host = "localhost";
$user = "root";
$pass = "root";
$db="student2";
$r = mysql_connect($host, $user, $pass);
if (!$r) {
echo "Could not connect to server\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Connection established\n";
}
echo mysql_get_server_info() . "\n";
$r2 = mysql_select_db($db);
if (!$r2) {
echo "Cannot select database\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "database selected\n";
}
$u=$_POST['Username'];
$pw=$_POST['Password'];
$result=mysql_query("select sIdname,password from student where Idname='$u' and password='$pw'");
$row=mysql_fetch_array($result);
if($row["Idname"]==$u && $row["password"]==$pw)
{
echo '{"success":1}';
}
else
{
'{"success":0,"error_message":"Username and/or password is invalid."}';
echo json_encode(array("key" => $result));
}
mysql_close();
?>
Tag : Mobile, MySQL, iOS, iPhone, Objective-C, Mac
|
|
|
|
|
|
Date :
2014-03-19 13:49:02 |
By :
fern_105 |
View :
1364 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ทดสอบเรียกไฟล์ php โดยตรง โดยสมมุติพวกค่าตัวแปร แล้ว php มันส่งค่าอะไรกลับมาหรือไม่
|
|
|
|
|
Date :
2014-03-19 17:33:00 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ปิด echo 'database select'; ครับ และให้ทำการปิด echo ให้หมด เว้นที่ echo json ครับ ไม่งั้น string ที่ส่งมาจะ error เพราะข้อความที่รับมาจะไม่เป็น json จากตัวอย่างของน้องข้อความที่รับมาจะเป็น
database select { 'success':1}
ให้ลอง nslog error ดูครับ น่าจะเห็นชัดเจนกว่า
|
|
|
|
|
Date :
2014-03-20 23:36:52 |
By :
xbeginner01 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|