Hello all. I have built an iOS app that sends data to a php script in key/value pairs using a stringWithFormat. I would like to build the same in an Android app. I'm seeing several examples on how to send data to a web form or to an MySQL database but my php script just grabs the values it is sent via post method and sends them to me in an html formatted email. Does anyone have an example of how to send a few vars to a php script via post much like an html web form would?
I have successfully sent a single key/ value using
Dim m As Map = CreateMap("lastname": "Fuzzy", "key2": "value2", ...)
Dim sb As StringBuilder
sb.Initialize
For Each k As String In m.Keys
sb.Append(k).Append("=").Append(m.Get(k))
Next
jobPost.PostString("...", sb.ToString)
I gave that a shot and it works but....It seems to be appending everything into the first value after the first key.
So when my PHP script grabs the first key....$firstname = $_POST["firstname"]; It returns all the keys and all the values crammed into firstname. I'm suspicious it has something to do with
sb.Append(k).Append("=").Append(m.Get(k)) but I'm not sure where the "K" is coming from and what it's job is.
Also I had to initalize jobpost. If you can get me over the hump on that mysterious "K" I think I can figure it out.
I think you're trying to send multiple insertions. In this case, I would go for sending a string containing all rows separated by a special character, for e.g. a crlf or maybe something else (I usually put "///" but this is not a standard). Example:
B4X:
Dim yourInsertions as string="lastname=Fuzzy///lastname=SomethingElse///lastname=AndSoOn"
After posting this string, you should explode this to your php script using the separation character or combination of characters. Example:
B4X:
$yourRows=explode("///",$yourInsertions);
$rowsCount=count($yourRows);
if($rowsCount>0){
for($i=0;$i<$rowsCount;$i++){
$currentRow=$yourRows[$i];
$yourMap=explode(",",$currentRow);//array containing the fields of the current row
//get your fields from here
$i++;}}
else {
//no rows sent, handle this
}
This is just an example, and there are better coding practices for this in php, but I chose this one as a bit simpler to follow.
Great Suggestions. I'll Give that a try this weekend. This is so much easier and more straight forward than Objective C. It's just taking me a while to wrap my head around it. Thanks everyone!!
Okay That got it. Adding the .append("&") was the missing link. I can now add as many keys/values as needed to the map and the php script sorts them out perfectly.
I still feel like I'm cheating though because even though it works I don't fully understand it.
Thanks to all who chipped in to help!!