我有3个UITextfield
和1个按钮,当我按下按钮时,点击使用表单发送到数据库的3 UITextfield
个数据。
我的代码是将数据发送到数据库,但它在数据库中以空值显示。
<form action="//http://192.168.3.171:8090/RestWebService/rest/person" id="suggestions" method="post">
<input id="name" name="name" type="text" >
<input id="suggestion" name="suggestion" type="text">
<input id="submitsuggestion" name="submitsuggestion" type="text">
</form>
Viewcontroller.M
#import "ViewController.h"
@interface ViewController ()
{
NSMutableData *recievedData;
NSMutableData *webData;
NSURLConnection *connection;
NSMutableArray *array;
NSMutableString *first;
}
@end
@implementation ViewController
@synthesize webview;
@synthesize firstName;
@synthesize lastName;
@synthesize email;
- (void)viewDidLoad
{}
- (IBAction)send:(id)sender
{
NSString *name = firstName.text;
NSLog(@" name is %@ ",name);
NSString *lastname = lastName.text;
NSLog(@" name is %@ ",lastname);
NSString *emailname = email.text;
NSLog(@" name is %@ ",emailname);
if (name.length == 0 || lastname.length == 0 || email.text==0) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Message!" message:@"plz enter 3 fields " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}else{
webData=[NSMutableData data];
NSURL *url = [NSURL URLWithString:@"http://192.168.3.128:8050/RestWebService/rest/person"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSData *requestData = [@"name=firstName&suggestion=lastName&submitsuggestion=email" dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"requestData%@",requestData);
[request setHTTPMethod:@"POST"];
[request setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//[request setValue:requestData forHTTPHeaderField:@"Content-Length"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
NSLog(@"requestData*******:%@",requestData);
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn)
{
NSLog(@"Connection successfull");
NSLog(@"GOOD Day My data %@",webData);
}
else
{
NSLog(@"connection could not be made");
}
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength:0];
NSLog(@"DidReceiveResponse");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
NSLog(@"DidReceiveData");
NSLog(@"DATA %@",data);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Error is");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Succeeded! Received %d bytes of data",[webData length]);
NSLog(@"Data is %@",webData);
// NSLog(@"receivedData%@",_receivedData);
NSString *responseText = [[NSString alloc] initWithData:webData encoding: NSASCIIStringEncoding];
NSLog(@"Response: %@", responseText);//holds textfield entered value
NSLog(@"");
NSString *newLineStr = @"\n";
responseText = [responseText stringByReplacingOccurrencesOfString:@"<br />" withString:newLineStr];
NSLog(@"ResponesText %@",responseText);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我的UITextfield
数据将存储在数据库中,但它为空。
答案 0 :(得分:1)
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.appListData = [NSMutableData data]; // start off with new data
}
或
如何传递网络服务
NSString *post = [NSString stringWithFormat:@"first_name=%@&last_name=%@",firstName.text,lastName.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[post length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost/promos/index.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self];
if( theConnection ){
// indicator.hidden = NO;
mutableData = [[NSMutableData alloc]init];
}
您的PHP代码
<?php
$first name = $_POST['first_name'];
$last name=$_POST['last_name'];
echo $username;
?>
答案 1 :(得分:0)
上述问题的确切答案[用于在您的URL(服务器)中发布数据]
//Here YOUR URL
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://192.168.3.128:8050/RestWebService/rest/person"]];
//create the Method "GET" or "POST"
[request setHTTPMethod:@"POST"];
//Pass The String to server(YOU SHOULD GIVE YOUR PARAMETERS INSTEAD OF MY PARAMETERS)
NSString *userUpdate =[NSString strin gWithFormat:@"user_email=%@&user_login=%@&user_pass=%@& last_upd_by=%@&user_registered=%@&",txtemail.text,txtuser1.text,txtpass1.text,txtuser1.text,datestr,nil];
//Check The Value what we passed
NSLog(@"the data Details is =%@", userUpdate);
//Convert the String to Data
NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];
//Apply the data to the body
[request setHTTPBody:data1];
//Create the response and Error
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
//This is for Response
NSLog(@"got response==%@", resSrt);
if(resSrt)
{
NSLog(@"got response");
/* ViewController *view =[[ViewController alloc]initWithNibName:@"ViewController" bundle:NULL];
[self presentViewController:view animated:YES completion:nil];*/
}
else
{
NSLog(@"faield to connect");
}
答案 2 :(得分:0)
让自己轻松生活并使用AFNetworking。有关如何发布表单数据的说明,请访问:https://github.com/AFNetworking/AFNetworking