PDA

View Full Version : HTTPService Explicit Parametre Passing


kalex
05-19-2008, 06:55 PM
Hi, thanks for any responses.

Ok, this seems pretty straight forward but I do not understand how the object is being passed:

<![CDATA[
public function advancedSearch():void {
//cancel previous pending calls
srv_search.cancel();

var all:Object = new Object();
all.first_name = search_first_name.text;
all.last_name = search_last_name.text;
all.full_name = search_full_name.text;
all.contact_title = search_contact_title.text;
all.organization = search_organization.text;
srv_search.send(all); <!-- is all passed as POST data? -->
}
]]>
</mx:Script>
<mx:HTTPService url="http://whatever.com/contacts_flex.php?search_db=1" id="srv_search" method="POST" />

So since I have the HTTPService set to POST, will the all object be sent as POST data? So, in PHP, could I get something like this?

$_POST['all']['first_name']

That's all I really want to do. I want to be able to loop through 'all'
Any ideas?

Text from the Help documentation:

"When you use explicit parameter passing, you can specify an object that contains name-value pairs as a send() method parameter. A send() method parameter must be a simple base type; you cannot use complex nested objects because there is no generic way to convert them to name-value pairs.

If you do not specify a parameter to the send() method, the HTTPService component uses any query parameters specified in an <mx:request> tag.

The following examples show two ways to call an HTTP service by using the send() method with a parameter. The second example also shows how to call the cancel() method to cancel an HTTP service call."

tsj4
05-19-2008, 07:20 PM
Your HTTPService object was created in mxml but never called until the advancedSearch() function is invoked. You would need to call srv_search.send();. Though I am not sure if the vars after the initial url value will make it (?search_db=1). I believe this will be stripped. That is the purpose of the params(I might be wrong about this though, but I am positive the everything after the & will be stripped from the url value). Everything looks good on how it was set up and should look like this when executed.

http://whatever.com/contacts_flex.php?search_db=1&first_name=someValue&last_name=someValue&full_name=someValue&cont.......

kalex
05-19-2008, 07:36 PM
Ah, sorry, I forgot to include my button that calls advancedSearch(). Still, thanks very much, you have pretty much answered my question. I'm not sure about ?search_db=1 being stripped either. Could I do something like this?

<mx:Script>
<![CDATA[

public function advancedSearch():void {
//cancel previous pending calls
srv_search.cancel();

var all:Object = new Object();
all.first_name = search_first_name.text;
all.last_name = search_last_name.text;
all.full_name = search_full_name.text;
all.contact_title = search_contact_title.text;
all.organization = search_organization.text;
srv_search.send(all);
}
]]>
</mx:Script>
<mx:HTTPService url="http://whatever.com/contacts_flex.php?" id="srv_search" method="POST">
<mx:request>
<search_db>1</search_db>
</mx:request>
</mx:HTTPService>


And send search_db through as a post along with the all object? I mean obviously search_db is just a trigger and I could trigger with all instead, but I am curious about using the <mx:request> tag along with explicit parameter passing anyway.

Also, would the URL be outputted the way you've shown even with the HTTPService method set to POST? I thought it would only look like that if it was a GET.

Anyway, thanks again, I have been searching hard for answers on this!

tsj4
05-19-2008, 08:25 PM
Instead of guessing if that value will be stripped or the format of the request I recommend getting a plugin for your test browser which captures all http request . Then you can see whats actually going on.


FireFox: Live HTTP Headers
IE: Http Watch Basic

let me know how it goes


also if you do need to add search_db as a separate param you will not need to include the ? at the end of the url. It will be included automatically. This is making me think that "http://whatever.com/contacts_flex.php?search_db=1" is valid. You will see though once you install the plugin

kalex
05-19-2008, 09:17 PM
Thanks again...

I don't think I've properly explained what I am trying to do. If I am passing a GET, this is what I really need to pass to the URL:

http://whatever.com/flex_contacts.php?search_db=1&all[first_name]=someValue&all[last_name]=someValue&all[contact_title]=someValue&all[contact_title]=someValue&all[Borganization]=someValue

Let's forget about the search_db param - it is the rest that I am having so much trouble with. This is very simple to do in HTML and I have not found any clue on how to do it with Flex. As you can see, the URL would pass something like this to php:

$_GET['all']['first_name']

The reason is that I can then loop through $_GET['all'] which makes the following PHP function work very well ($k matches the actual mySQL field name):


function search_contacts($all, $type) {
$reports = array();
$find_report = new Ext_contacts();
$q = "SELECT * FROM ext_contacts ";
if ($all) {
$q .= "WHERE ";
foreach ($all as $k => $v) {
if ($v) {
if ($prev) {
$q .= "AND ";
}
if ($type == "exact") {
$q .= "$k = '$v' ";
} elseif ($type == "fuzzy") {
$q .= "$k LIKE '%$v%' ";
}
$prev = 1;
}
}
}


See? Otherwise I have to check each field and there are many more than I have used in my examples (around 40!).. This is my problem in its entirety, at the best of my ability to explain it simply (too late).. ANYTHING! Any ideas, comments, questions, or especially answers to this would help so much. Thank you!

tsj4
05-19-2008, 11:52 PM
The url will not include the object "all". What will be passed to your script is the url as I formatted it earlier. I don't know php so I can't answer those questions. Sorry

kalex
05-20-2008, 12:32 AM
Thanks for all your help. I don't think it can be done. Oh well, there goes my interest in Flex ;)

anoopasok
05-24-2008, 08:19 AM
Hi...

i'm a bit new to this flex and actionscript. but while i read your post, i think i've found something in the things i read that can help u...
regarding the communication between a flex application and and a php file, we can use a httpservice. and ofcourse u can choose the method to POST or GET...now for sending the arguements for the php file,we have to send it using the <mx:request></mx:request>. the arguements are send in a name value format and it looks like xml.


for example if i want to send my name to a php file, say sample.php, i'll define the http service like this

<mx:httpservice id="some_id" url="sample.php" method="POST" >
<mx:request>
<usename>Anoop</username>
<mx:request>
</mx:httpservice>

so when the php program recieves this thing, it can get my name using $_POST["username"] which will return Anoop.

If i want to send some other details like my sex also with the name,

<mx:httpservice id="some_id" url="sample.php" method="POST" >
<mx:request>
<details>
<usename>Anoop</username>
<sex>Male</sex>
</details>
<mx:request>
</mx:httpservice>


this will send the php file the arguements and the php file will be able to access them in the $_POST["details"]["username"] and $_POST["details"]["sex"] format.


please note that i'ven't tried this before and as it jus comes out of my mind i wrote this. the code is also not taken directly from the application, so if u r going to copy paste, it will show some format errors, so check before u do it.

if it works, then i think this is wat u wanted...

Anoop