2 Replies - 2400 Views - Last Post: 09 September 2011 - 08:10 AM

#1 Boos199  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 18-June 10

problem making app Post to php

Posted 08 September 2011 - 10:15 PM

So i'm writing a app to control a php script when a button is pressed. All that the php script contains is two radio buttons. My problem is figuring out what string to actually post to the url.

My button action:
                         NSString* content = @"LED0=1";
                         NSURL* url = [NSURL URLWithString:@"*****************************************************"];
                         NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
                         [urlRequest setHTTPMethod:@"post"];
                         [urlRequest setHTTPBody:[content dataUsingEncoding:NSASCIIStringEncoding]];



my php form script:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=320; initial-scale=2.0; maximum-scale=2.0; user-scalable=0;"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>LED Control </title>
</head>

<body>

<?php 
$control = fopen("control.txt", "r");
$data = fgetcsv($control, 10 , ',') or die ("can't open file");
$LED0status= $data[1];
fclose($control);
echo("LED0 Status is: "); 
if ($LED0status ==1) {
	echo("<font color='#00FF33'>ON</font>");
} else {
	echo("<font color='red'>OFF</font>");
}
?>

<form action="control_submit.php" method="post" target="_self">

  <table width="200">
    <tr>
      <td><label>
        <input <?php if (!(strcmp("$LED0status","1"))) {echo "checked=\"checked\"";} ?> type="radio" name="LED0" value="1" id="LED0_0" />
        ON</label></td>
    </tr>
    <tr>
      <td><label>
        <input <?php if (!(strcmp("$LED0status","0"))) {echo "checked=\"checked\"";} ?> type="radio" name="LED0" value="0" id="LED0_1" />
        OFF</label></td>
    </tr>
  </table>
    
  <input name="Submit" type="submit" />
  
</form>

</body>
</html>


now this is the php script that writes the value to a text file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta name="viewport" content="width=320; initial-scale=2.0; maximum-scale=2.0; user-scalable=0;"/>
<meta http-equiv="REFRESH" content="1;url=http://www.auburn.edu/~cjb0001/arduino/control_test.php">
<title>Data Test</title>
</head>

<body>

Saving Command...<br />

<?php $write = fopen("control.txt", 'w');

fwrite($write, "0");
fwrite($write, ",");
fwrite($write, $LED0);

fclose($write);

?>



</body>

</html>



Thanks in advance,
Andrew

This post has been edited by Boos199: 08 September 2011 - 10:16 PM


Is This A Good Question/Topic? 0
  • +

Replies To: problem making app Post to php

#2 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: problem making app Post to php

Posted 09 September 2011 - 08:04 AM

The string you want in the URL is the server's address. You need to run a webserver with PHP installed for this program to work. If you're running the server on the same machine as the Obj-C program then the address will be localhost.
Was This Post Helpful? 0
  • +
  • -

#3 Boos199  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 18-June 10

Re: problem making app Post to php

Posted 09 September 2011 - 08:10 AM

View PostGWatt, on 09 September 2011 - 10:04 AM, said:

The string you want in the URL is the server's address. You need to run a webserver with PHP installed for this program to work. If you're running the server on the same machine as the Obj-C program then the address will be localhost.


sorry, I should have been more specific. I know what to put for the url, I just didn't want to post it for everybody to see. My question is what string I should send to post the data. like do I just leave it as "LED0=1"? or "LED0=0"? Does that make sense? I can't figure out the actual string to post to the form.

NSString* content = @"LED0=1";



Andrew

This post has been edited by Boos199: 09 September 2011 - 08:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1