2009/8/24 ColinFine <[email protected]>:
>
>
>
> On Aug 22, 5:09 pm, desbest <[email protected]> wrote:
>> How do I use AjaxRequest to POST checkboxes which is an array, not a
>> variable?
>> [code]
>> =====================================
>> <script type="text/javascript">
>>
>> function addEvent(day, month, year, body, involved) {
>> if(day && month && year && body && involved) {
>>
>> //alert('Add Event \n Day: '+day+'\n Month:
>> '+month+'\n Year:
>> '+year+'\n Involved: '+involved+'\n Body: '+body);
>>
>> new Ajax.Request('rpc.php', {method: 'post',
>> postBody:
>> 'action=addEvent&d='+day+'&m='+month+'&y='+year+'&involved='+involved
>> +'&body='+body+'', onSuccess: highlightEvent(day)});
>>
>> $('evtBody').value = '';
>> } else {
>> alert('There was an unexpected script error.\nPlease
>> ensure that
>> you have not altered parts of it.');
>> }
>> </script>
>> ========================================
>> [/code]
>>
>> It posts information using ajax but the rpc.php thinks that $involved
>> equals 1.
>> What it doesn't know is that $involved is an array, which when echoed
>> reads Array because it has $involved[0] and $involved[1]. Below is
>> rpc.php for a calendar script.
>>
>> [code]
>> ===========================================
>> $day = $_POST['d'];
>> $month = $_POST['m'];
>> $year = $_POST['y'];
>> $body = $_POST['body'];
>> $involved = $_POST["involved"];
>>
>> $howmany = count($involved);
>> $timeStamp = mktime(0,0,0, $month, $day, $year);
>> $bodyF = addslashes(trim($body));
>>
>> $addEvent = mysql_query("INSERT INTO event (body, timestamp,
>> userid,
>> involved) VALUES ('$body', '$timeStamp', '$logged[id]', '$howmany')",
>> $conn);
>>
>> $daidting = mysql_insert_id();
>> $addInvolved = mysql_query("INSERT INTO involved (eventid,
>> userid)
>> VALUES ('$daidting', '$howmany')", $conn);
>>
>> foreach ($involved as $f) {
>> //echo $f."<br />";
>> $addInvolved = mysql_query("INSERT INTO involved (eventid,
>> userid)
>> VALUES ('$daidting', '$f')", $conn);
>> }
>>
>> =======================
>> [/code]
>
> CGI has no concept of an array, even in POST. You either need to
> serialise the array, or generate multiple instances of
> "involved=whatever".
>
> PHP has a nifty wrinkle whereby if a control in your page has a name
> ending in '[]' then it will give you back an array.
>
> BUT, where checkboxes are involved, I don't think an array is useful,
> because the wonderful design of HTML returns absolutely nothing for an
> unchecked box, so if you simply try to pass all the checkboxes back to
> your CGI program you will get only the checked ones with no indication
> of which boxes on the page they were.
> >
>
Extending upon what ColinFine said about the use of [], this can be
useful when using checkboxes when the [] contains, say, an id or when
the input tag's value is set.
e.g.
<input type="checkbox" name="involved[Monday]" />
or
<input type="checkbox" name="involved[]" value="Monday" />
The first would produce and array ...
Array
(
[Monday] => 1
)
The second would produce an array ...
Array
(
[0] => Monday
)
--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---