Getting PHP to play nice with Javascript

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Getting PHP to play nice with Javascript

Post by phpPete »

Ok, due to PHP and JS naming restrictions I'm finding it exceedingly difficult, or perhaps I'm dense, to post multiple selected options from a select list.

If I name my form: name[] ( effectively declaring an array ) then PHP picks up all the selected options no problem, however, I need to process to do a number of things with JS before i post the form and JS doesn't like element names like: name[].

I tried something like this to no avail also:

<form name="f1" ...>
<select name="f1[choices[]]"...>

When i name like so PHP does get all the posted selections, however, i am unable to do my client side processing in JS with this sort of naming.

Can anyone help me out here?

Thanks,

Pete
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

Why do u have to process form info with JS?! Do it with PHP. It's plausable to use JS for little extra things...but If someone's browser doesn't support JS, they're missing a LOT more than a few nice features...you'r site becomes disfunctional to them. Reconsider your methods...

later on, -Brian
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post by gite_ashish »

hi,

see the code below:

Code: Select all

<SCRIPT type="text/javascript">

    function setTo()
    &#123;
        j = document.frm.elements&#1111;'month&#1111;]'].length;

        for( i = 0; i < j; i++ )
        &#123;
            if ( document.frm.elements&#1111;'month&#1111;]'].options&#1111;i].selected )
            &#123;
                alert( 'Selected: ' + document.frm.elements&#1111;'month&#1111;]'].options&#1111;d
ocument.frm.elements&#1111;'month&#1111;]'].selectedIndex].value );
            &#125;
            else
            &#123;
                alert( 'Unselected: ' + document.frm.elements&#1111;'month&#1111;]'].options
&#1111;i].value );
            &#125;
        &#125;

        return false;
    &#125;

</SCRIPT>

<PRE>
<H3>PHP-JavaScript</H3>
<FORM name=frm method=POST onSubmit="return setTo();">
    Name: <INPUT type=text name=txtName>

    Month: <SELECT name="month&#1111;]" multiple size=6>
        <OPTION VALUE="1">January</OPTION>
        <OPTION VALUE="2">February</OPTION>
        <OPTION VALUE="3">March</OPTION>
        <OPTION VALUE="4">April</OPTION>
        <OPTION VALUE="5">May</OPTION>
        <OPTION VALUE="6">June</OPTION>
        <OPTION VALUE="7">July</OPTION>
        <OPTION VALUE="8">August</OPTION>
        <OPTION VALUE="9">September</OPTION>
        <OPTION VALUE="10">October</OPTION>
        <OPTION VALUE="11">November</OPTION>
        <OPTION VALUE="12">December</OPTION>
    </SELECT>

    <INPUT type=submit value=Submit>
</FORM>
</PRE>

See php man:

:idea: http://www.php.net/manual/en/faq.html.php


Thanks, i also tried it today only ! ;-)
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

I'm using JS in this case because I'm restricting the browser to IE, ( it's for an intranet where I work ).

The JS is perfect here because I'm building numerous select lists from other lists etc...so using JS I avoid a ton of page requests.

If this were for the "real world" I'd obviously go another route.

Regards,

Pete

Also, thanks to gite_ashish, I'll give that script a try.
Post Reply