0

I have this email = 'abc' and pw = '123' value in PHP MySQL database and PHP code as below.

login2.php

<?php
$connect = mysqli_connect("localhost","","","");
if(isset($_POST['email'])) {
    $email = $_POST['email'];
    $pw    = $_POST['pw'];
    $sql   = "SELECT * FROM table WHERE email='$email' AND pw=md5('$pw')";
    $get   = mysqli_query($connect,$sql);

    if($get && mysqli_num_rows($get) > 0) {
        while($row = mysqli_fetch_assoc($get)) {
            $email_db = $row['email'];
            $pw_db    = $row['pw'];             
            echo '{"success":1}';
            exit();
        }
        mysqli_free_result($get);
    } else {
        echo '{"success":0}';
        echo mysqli_error($connect);
        exit();
    }
}
?>

What I want to achieve in xcode is to bring the success value into other ViewController called HomepageVC and display the value when loginbutton action is clicked.

Right now i can only view the textfield value on the same ViewController page. I don't know how to transfer the input value into another ViewController. Obj c code as below.

ViewController.m

@synthesize displayL,passL,email,pw;

- (IBAction)loginbutton:(id)sender
{    
    @try {

        if([[email text] isEqualToString:@""] || [[pw text] isEqualToString:@""] ) {
            [self alertFailed:@"Please enter both Username and Password" :@"Login Failed!"];
        }
        else {
            NSString *post =[[NSString alloc] initWithFormat:@"email=%@&pw=%@",[email text],[pw text]];
            NSLog(@"PostData: %@",post);
            NSURL *url=[NSURL URLWithString:@"http://localhost/login2.php"];
            NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
            NSString *postLength = [NSString stringWithFormat:@"%d", [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];

            NSError *error = [[NSError alloc] init];
            NSHTTPURLResponse *response = nil;
            NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
            NSLog(@"Response code: %d", [response statusCode]);
            if ([response statusCode] >=200 && [response statusCode] <300)
            {
                NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
                NSLog(@"Response ==> %@", responseData);
                SBJsonParser *jsonParser = [SBJsonParser new];
                NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
                NSLog(@"%@",jsonData);
                NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
                NSLog(@"%d",success);
                if(success == 1)
                {
                    NSLog(@"Login SUCCESS");
                    [self alertStatus:@"Logged in Successfully." :@"Login Success!"];
                    NSString * input  = email.text;
                    NSString * input2 = pw.text;
                    displayL.text = input;
                    passL.text = input2;

                } else {
                    NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
                    [self alertFailed:error_msg :@"Login failed"];
                }
            } else {
                if (error) NSLog(@"Error: %@", error);
                [self alertFailed:@"Connection Failed" :@"Login Failed!"];
            }
        }
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);
        [self alertStatus:@"Login Failed." :@"Login Failed!"];
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
AlotJai
  • 147
  • 4
  • 18

1 Answers1

0

If you want to use values of textfield from currentViewController to nextViewController, you can use NSUserDefaults.

store your textfield value in one string. for example Txtfldstring

Txtfldstring= [NSString stringWithFormat:@"%@",_yourTextField.text];

Then store that textfield value like following way:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString

[prefs setObject:Txtfldstring forKey:@"abc"];

Use this value in nextViewController or any other Controller like following way;

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *s = [prefs stringForKey:@"abc"];
NSLog(@“%@",s);
Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19