title: How to send and receiving data from Xcode (objective c) to php
body: <p>I am trying to send a username and password from xcode to a php file that I have made. As I am new to both xcode and php and for the purposes of the simple application I am making, the username and password is a predefined "123456". I am having a lot of trouble sending the username and password to the php site, and receiving a response. When my code communicates and establishes a connection, it does not read the variable echoed in the php file. Instead it obtains the entire source code of the page. My code is below:</p>
  
  <p>Objective C code:</p>
  
  <p>// I have set an alert view, and set two buttons (continue = buttonIndex==0 and cancel = buttonIndex==1)</p>
  
  <pre><code>    // The user continues from the alert view
  
      if(buttonIndex==0)
  {
      NSString* username = @"123456";
      NSString* pass = @"123456";
  
      // I combine the two strings to send to the php page, and insert a : between the two strings
  
      NSString* combined = [NSString stringWithFormat:@"%@:%@", username, pass];
      NSString *post = combined;
  
      // Now I attempt to post the data to the php page
      NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
  
      NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
  
      NSURL *url = [NSURL URLWithString:@"http://www.sulaimanmajeed.com/login.php"];
      NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
      [theRequest setHTTPMethod:@"POST"];
      [theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
      [theRequest setHTTPBody:postData];
  
      NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  
      if( theConnection )
      {
          webData = [[NSMutableData data] retain];
      }
      else
      {
  
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Now, I also have the other methods which need to be called when doing a post:</p>
  
  <pre><code>    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  {
      [webData setLength: 0];
  }
  
  -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  {
      [webData appendData:data];
  }
  
  -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  {
      [connection release];
      [webData release];
  }
  
  -(void)connectionDidFinishLoading:(NSURLConnection *)connection
  {
  
      NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
      // NSLog(loginStatus);
      dataOutput.text = loginStatus;
      [loginStatus release];
  
      [connection release];
      [webData release];
  }
  </code></pre>
  
  <p>My php code is really simple:</p>
  
  <pre><code>&lt;?php
  
  $usernameEntered = $_GET['user']; $passwordEntered = $_GET['pass']; $full = 'Choose';
  
  $username = array(
      "123456",   "234567",   "345678",   "456789" );
  
  $password = array(
      "123456",   "234567",   "345678",   "456789" );
  
  $accounts = array(
      "Chequings-874576",
      "Savings-793756",
      "Visa-018375",
      "Mastercard-375836" );
  
  $b = 0; $elementsInArray = count($accounts);
  
  $isTrue = "FALSE";
  
  if (($usernameEntered== $username[0]) &amp;&amp; ($passwordEntered == $password[0])) {
      $isTrue = "TRUE"; } elseif ((usernameEntered == $username[1]) &amp;&amp; ($passwordEntered == $password[1])) {
      $isTrue = "TRUE"; } elseif ((usernameEntered == $username[2]) &amp;&amp; ($passwordEntered == $password[2])) {
      $isTrue = "TRUE"; } elseif ((usernameEntered == $username[3]) &amp;&amp; ($passwordEntered == $password[3])) {
      $isTrue = "TRUE"; } else {
      $isTrue = "FALSE"; }
  
  if( $isTrue == "TRUE" ) {   for ($i=0; $i &lt; $elementsInArray; $i++)   {       $b = $i;    $full  = $full . ':' . $accounts[$b] ;   }
       echo $full;
  
  }
  
  if( $isTrue == "FALSE" ) {
            echo "error:error 2:test 1:test 2:test 3";
  
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>So, dataOutput.text should return a "true" value, because the username and password I provided was a default "123456". However dataOutput.text only returns the entire source code of the website. Could someone assist me in finding out why it does not output true, and where my code went wrong? I have been working on this for 2 days, and I am stumped. Thank you in advance.</p>
  
  <p>P.S.</p>
  
  <p>The output I receive in dataOutput is:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;&lt;!-- HTML generated by Network Solutions Website Builder --&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;head&gt;&lt;title&gt;Sulaiman Majeed login&lt;/title&gt;&lt;meta name="robots" content="index, follow" /&gt;&lt;meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" /&gt;&lt;meta name="description" content="Welcome to my website"&gt;&lt;meta http-equiv="cache-control" content="no-cache"/&gt;&lt;meta http-equiv="pragma" content="no-cache"/&gt;&lt;meta http-equiv="expires" content="-1"/&gt;&lt;script type="text/javascript" src="/scripts/user.js"&gt;&lt;/script&gt;&lt;script type="text/javascript" src="/scripts/jquery.js"&gt;&lt;/script&gt;&lt;script type="text/javascript" src="/scripts/simplegallery.js"&gt;&lt;/script&gt;&lt;script type="text/javascript" src="/scripts/photo_album.js"&gt;&lt;/script&gt;&lt;link type="text/css" href="/scripts/PrebuiltBlockStructure.css" media="screen" rel="stylesheet"&gt;&lt;link rel="stylesheet" media="screen" href="/scripts/template.css" id="wb-template.css" /&gt;&lt;link rel="stylesheet" media="all" href="/scripts/layout3.css" id="wb-layout3.css" /&gt;&lt;link rel="stylesheet" media="all" href="/scripts/website.css" id="wb-website.css" /&gt;&lt;link rel="stylesheet" media="print" href="/scripts/print.css" id="wb-print.css" /&gt;&lt;link rel="stylesheet" media="all" href="/scripts/12custom.css" id="wb-custom.css" /&gt;&lt;link rel="stylesheet" media="all" href="/scripts/icwebsiteelement.css" id="wb-icwebsiteelement.css" /&gt;&lt;link rel="stylesheet" media="all" href="/share/scripts/PrebuiltBlockStructure.css" id="wb-/share/scripts/PrebuiltBlockStructure.css" /&gt;&lt;/head&gt;&lt;body onload="initClientMenu();sizeColumns();"&gt;&lt;!-- Start content section --&gt;&lt;div id="wb-container-outer"&gt; &lt;div id="wb-container-inner"&gt; &lt;div id="wb-layout"&gt; &lt;div id="wb-layout-inner"&gt; &lt;div id="wb-layout-top-header"&gt; &lt;div id="wb-navigation-subtop-header"&gt;                                              &lt;/div&gt; &lt;div id="wb-top-subbasenav"&gt;&lt;/div&gt; &lt;/div&gt; &lt;div id="wb-header-nav"&gt; &lt;div id="wb-navigation-top-header"&gt; &lt;div id="wb-navigation-bottom-header"&gt;                                                    &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="wb-layout-header"&gt; &lt;div id="wb-layout-header-mask" class="wb_region"&gt;           &lt;table id="wb_con__42340" class="wb_con" cellpadding="0" cellspacing="0" width="100%"&gt; &lt;tr&gt; &lt;td id="wb_col__45678" class="wb_col" width="50%" valign="top" style=""&gt;                             &lt;/td&gt;&lt;td id="wb_col__5673453" class="wb_col" width="50%" valign="top" style=""&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;        &lt;/div&gt; &lt;/div&gt; &lt;div id="wb-layout-header-base"&gt;&lt;/div&gt; &lt;div id="wb-layout-top-2"&gt; &lt;div id="wb-navigation-subtop"&gt;                         &lt;div&gt;&lt;a href="home.html" class="tertiary" target="_self"&gt;&lt;h3&gt;Home&lt;/h3&gt;&lt;/a&gt;&lt;/div&gt; &lt;div&gt;&lt;a href="resume.html" class="tertiary" target="_self"&gt;&lt;h3&gt;Resume&lt;/h3&gt;&lt;/a&gt;&lt;/div&gt;                      &lt;/div&gt; &lt;div id="wb-top-subbasenav"&gt;&lt;/div&gt; &lt;/div&gt; &lt;div id="wb-container-contentwrapper"&gt; &lt;div id="wb-layout-sidebar"&gt; &lt;div id="wb-navigation"&gt; &lt;div id="wb-navigation-top"&gt; &lt;div id="wb-navigation-bottom"&gt;                                     &lt;div&gt;&lt;a href="home.html" class="tertiary" target="_self"&gt;&lt;h3&gt;Home&lt;/h3&gt;&lt;/a&gt;&lt;/div&gt; &lt;div&gt;&lt;a href="resume.html" class="tertiary" target="_self"&gt;&lt;h3&gt;Resume&lt;/h3&gt;&lt;/a&gt;&lt;/div&gt;                                  &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="wb-basenav"&gt;&lt;/div&gt; &lt;div id="wb-navbasewrap" class="wb_region"&gt; &lt;div class="wb_dropzone_y"&gt; &lt;/div&gt;           &lt;table id="dz-tablewb_element_text__02773140" class="dz-table" cellspacing="0" cellpadding="0" width="100%"&gt; &lt;tr&gt; &lt;td align="left"&gt; &lt;div id="wb_element_text__02773140" class="ic-element"&gt; &lt;div&gt;&lt;/div&gt; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;        &lt;/div&gt; &lt;/div&gt; &lt;div id="wb-layout-main"&gt; &lt;div id="wb-layout-main-inner-1"&gt; &lt;div id="wb-layout-main-inner-2" class="wb_region wb-text-content"&gt;           &lt;table id="wb_cl__12342346" cellpadding="0" cellspacing="0" class="" width="100%"&gt; &lt;tr&gt; &lt;td&gt;              &lt;table id="wb_con__056756794678680" class="wb_con" cellpadding="0" cellspacing="0" width="100%"&gt; &lt;tr&gt; &lt;td id="wb_col__089563564560" class="wb_col" width="100%" valign="top" style=""&gt;                               &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;                       &lt;table id="wb_con__1231213564678" class="wb_con" cellpadding="0" cellspacing="0" width="100%"&gt; &lt;tr&gt; &lt;td id="wb_col__335786456" class="wb_col" width="70%" valign="top" style=""&gt;                                    &lt;table id="dz-tablewb_element_php__1358289049875_15" class="dz-table" cellspacing="0" cellpadding="0" width="100%"&gt; &lt;tr&gt; &lt;td align="left"&gt; &lt;div id="wb_element_php__1358289049875_15" class="ic-element"&gt;&lt;php?  ;  ?&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/td&gt;&lt;td id="wb_col__234456134" class="wb_col" width="30%" valign="top" style=""&gt;                                   &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;              &lt;table id="wb_con__5675679467868" class="wb_con" cellpadding="0" cellspacing="0" width="100%"&gt; &lt;tr&gt; &lt;td id="wb_col__8956356456" class="wb_col" width="100%" valign="top" style=""&gt;                                &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;           &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;        &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div style="clear: both"&gt;&lt;/div&gt; &lt;/div&gt; &lt;div id="wb-layout-footer"&gt; &lt;div id="wb-layout-footer-inner-1" class="wb_region"&gt; &lt;div class="wb_dropzone_y"&gt; &lt;/div&gt;                  &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;&lt;table border="0" width="100%" cellpadding="0" cellspacing="0" id="other_footer"&gt; &lt;tr&gt; &lt;td align="left" valign="middle"&gt;&lt;p style="font-size: 10px; font-style: normal; font-family: arial,verdana,sans-serif;  font-style: normal;"&gt;&lt;a style="text-decoration: none; font-size: 10px; font-style: normal; font-family: arial,verdana,sans-serif;  font-style: normal;" title="Web hosting" href="http://ads.networksolutions.com/landing?code=P61C100S1N0B142A1D255E0000V100"&gt;Web Hosting&lt;/a&gt; powered by Network Solutions®&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;!-- End content section --&gt;&lt;!-- Start nsWebstats code.  Copyright 2010 networksolutions.com More info available at http://networksolutions.com --&gt;&lt;script type="text/javascript"&gt;var pageName="login"; document.write(unescape('%3Cscript src="'+(location.href.split(':')[0] =='https'?'https':'http')+'://stats.netsolads.com/jsconfig?pid=288656091%26mg=all%26lc=1%26pe=1"%3E%3C/script%3E'));&lt;/script&gt;&lt;!--End nsWebStats tracking code. --&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>