calling variable in onchange function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • R.G. Vervoort

    #1

    calling variable in onchange function

    I am using a javafunction (onclick in select) in which i am calling a
    function in php (thats why i send this to both php and javascript
    newsgroups).

    in the onclick i call the function "Place_Selected " with the value from the
    select (naam_keuze.val ue)

    in the function the value becomes the $zoek_id and searches in the database
    for the record with the id of $zoek_id

    the naam_keuze.valu e does not give the value to $zoek_id

    When i replace naam_keuze.valu e for a number (15) i works great.

    WHAT AM I DOING WRONG?


    This is my code



    Function Place_Selected( $zoek_id)
    {
    include("data.p hp");
    $link=mysql_con nect($db_host, $username, $password) or die("Database
    error!");
    mysql_select_db ($database , $link)or die("Couldn't open $db:
    ".mysql_error() );
    $sql=mysql_quer y("SELECT * FROM specialismen WHERE id='$zoek_id'") ;

    if ($sql)
    {
    while($blah2 = mysql_fetch_arr ay($sql))
    {
    $newsid = $blah2['id'];
    $sticky = $blah2['naam'];
    }
    }
    return("documen t.form.achterna am.value='$stic ky'");
    }


    <select id=naam_keuze size=1 name=keuze style=visibilit y:hidden
    onchange=".Plac e_Selected('naa m_keuze.value') .";document.for m.voornaam.valu e
    =naam_keuze.val ue;>
    <option value=0>Kies
    $options
    </select>

    Thanks for suggestions

    roy


  • Henk Burgstra

    #2
    Re: calling variable in onchange function

    On Wed, 14 Apr 2004 22:48:53 +0200, R.G. Vervoort wrote:
    [color=blue]
    > I am using a javafunction (onclick in select) in which i am calling a
    > function in php (thats why i send this to both php and javascript
    > newsgroups).
    >
    > in the onclick i call the function "Place_Selected " with the value from the
    > select (naam_keuze.val ue)
    >
    > in the function the value becomes the $zoek_id and searches in the database
    > for the record with the id of $zoek_id
    >
    > the naam_keuze.valu e does not give the value to $zoek_id
    >
    > When i replace naam_keuze.valu e for a number (15) i works great.
    >
    > WHAT AM I DOING WRONG?
    >
    >
    > This is my code
    >
    >
    >
    > Function Place_Selected( $zoek_id)
    > {
    > include("data.p hp");
    > $link=mysql_con nect($db_host, $username, $password) or die("Database
    > error!");
    > mysql_select_db ($database , $link)or die("Couldn't open $db:
    > ".mysql_error() );
    > $sql=mysql_quer y("SELECT * FROM specialismen WHERE id='$zoek_id'") ;
    >
    > if ($sql)
    > {
    > while($blah2 = mysql_fetch_arr ay($sql))
    > {
    > $newsid = $blah2['id'];
    > $sticky = $blah2['naam'];
    > }
    > }
    > return("documen t.form.achterna am.value='$stic ky'");
    > }
    >
    >
    > <select id=naam_keuze size=1 name=keuze style=visibilit y:hidden
    > onchange=".Plac e_Selected('naa m_keuze.value') .";document.for m.voornaam.valu e
    > =naam_keuze.val ue;>
    > <option value=0>Kies
    > $options
    > </select>
    >[/color]
    You have to understand that PHP is executed on the server and Javascript
    on the client. So it isn't possible to call a PHP-function from Javascript
    as you are trying to do.

    So what happens if you substitute 'naam_keuze.val ue' for the number 15 is
    that Place_Selected( 15) will be executed by PHP on the server. The result
    of this function call will be inserted into you HTML and *then* sent to the
    browser (the client) where the Javascript code is executed.
    Let's say that the result of the query wil be 'Jan Jansen', than the
    resulting HTML wil be:
    <select id=naam_keuze size=1 name=keuze style=visibilit y:hidden
    onchange=docume nt.form.achtern aam.value='Jan Jansen';
    document.form.v oornaam.value=n aam_keuze.value ;>

    This is why it sort of works if you substitute 'naam_keuze.val ue' with 15,
    but it will never work otherwise, because Place_Selected will be executed
    on the server, thus $zoek_id will become 'naam_keuze.val ue' which will not
    be found.

    Does this make any sense to you?

    Regards,
    Henk Burgstra

    Comment

    • Reply Via Newsgroup

      #3
      Re: calling variable in onchange function

      R.G. Vervoort wrote:
      [color=blue]
      > I am using a javafunction (onclick in select) in which i am calling a
      > function in php (thats why i send this to both php and javascript
      > newsgroups).
      >
      > in the onclick i call the function "Place_Selected " with the value from the
      > select (naam_keuze.val ue)
      >
      > in the function the value becomes the $zoek_id and searches in the database
      > for the record with the id of $zoek_id
      >
      > the naam_keuze.valu e does not give the value to $zoek_id
      >
      > When i replace naam_keuze.valu e for a number (15) i works great.
      >
      > WHAT AM I DOING WRONG?
      >
      >
      > This is my code
      >
      >
      >
      > Function Place_Selected( $zoek_id)
      > {
      > include("data.p hp");
      > $link=mysql_con nect($db_host, $username, $password) or die("Database
      > error!");
      > mysql_select_db ($database , $link)or die("Couldn't open $db:
      > ".mysql_error() );
      > $sql=mysql_quer y("SELECT * FROM specialismen WHERE id='$zoek_id'") ;
      >
      > if ($sql)
      > {
      > while($blah2 = mysql_fetch_arr ay($sql))
      > {
      > $newsid = $blah2['id'];
      > $sticky = $blah2['naam'];
      > }
      > }
      > return("documen t.form.achterna am.value='$stic ky'");
      > }
      >
      >
      > <select id=naam_keuze size=1 name=keuze style=visibilit y:hidden
      > onchange=".Plac e_Selected('naa m_keuze.value') .";document.for m.voornaam.valu e
      > =naam_keuze.val ue;>
      > <option value=0>Kies
      > $options
      > </select>
      >
      > Thanks for suggestions
      >
      > roy
      >
      >[/color]

      I don't see how it could work, even with a numeric value of 15... What
      version of PHP are you using?

      Here's why I think it shouldn't work...

      Javascript is client side - PHP is server side... thus, you attempt to
      call Place_Selected (PHP code) in your onChange event - This won't
      happen unless you hava a javascript function with that name that submits
      the form contents to the server via a FORM POST or GET...

      Also... The value for your select box should I believe end up in
      $_POST['keuze'] in PHP when the form is submitted...

      And... return("documen t.form.achterna am.value='$stic ky'");
      Again, you've got your knickers in a twist - the return statement will
      not return a value into achternaam like I think you expect... I really
      think you are misunderstandin g how the two technologies fit together...

      If you are sure that your test will work when you enter 15 as a test
      value - try different numbers to confirm that test works - If it does,
      then I think you've not provided the entire code here for me (perhaps
      anyone else) to give a full and proper answer/suggestion.

      Does any of the above help?
      randelld

      Comment

      • R.G. Vervoort

        #4
        Re: calling variable in onchange function

        it works great, as long as the number (15 or ...) is an id in the database.
        i just will not accept the "." (dot) in the line



        "Reply Via Newsgroup" <reply-to-newsgroup@pleas e.com> schreef in bericht
        news:J9kfc.1214 95$Ig.78826@pd7 tw2no...[color=blue]
        > R.G. Vervoort wrote:
        >[color=green]
        > > I am using a javafunction (onclick in select) in which i am calling a
        > > function in php (thats why i send this to both php and javascript
        > > newsgroups).
        > >
        > > in the onclick i call the function "Place_Selected " with the value from[/color][/color]
        the[color=blue][color=green]
        > > select (naam_keuze.val ue)
        > >
        > > in the function the value becomes the $zoek_id and searches in the[/color][/color]
        database[color=blue][color=green]
        > > for the record with the id of $zoek_id
        > >
        > > the naam_keuze.valu e does not give the value to $zoek_id
        > >
        > > When i replace naam_keuze.valu e for a number (15) i works great.
        > >
        > > WHAT AM I DOING WRONG?
        > >
        > >
        > > This is my code
        > >
        > >
        > >
        > > Function Place_Selected( $zoek_id)
        > > {
        > > include("data.p hp");
        > > $link=mysql_con nect($db_host, $username, $password) or die("Database
        > > error!");
        > > mysql_select_db ($database , $link)or die("Couldn't open $db:
        > > ".mysql_error() );
        > > $sql=mysql_quer y("SELECT * FROM specialismen WHERE id='$zoek_id'") ;
        > >
        > > if ($sql)
        > > {
        > > while($blah2 = mysql_fetch_arr ay($sql))
        > > {
        > > $newsid = $blah2['id'];
        > > $sticky = $blah2['naam'];
        > > }
        > > }
        > > return("documen t.form.achterna am.value='$stic ky'");
        > > }
        > >
        > >
        > > <select id=naam_keuze size=1 name=keuze style=visibilit y:hidden
        > >[/color][/color]
        onchange=".Plac e_Selected('naa m_keuze.value') .";document.for m.voornaam.valu e[color=blue][color=green]
        > > =naam_keuze.val ue;>
        > > <option value=0>Kies
        > > $options
        > > </select>
        > >
        > > Thanks for suggestions
        > >
        > > roy
        > >
        > >[/color]
        >
        > I don't see how it could work, even with a numeric value of 15... What
        > version of PHP are you using?
        >
        > Here's why I think it shouldn't work...
        >
        > Javascript is client side - PHP is server side... thus, you attempt to
        > call Place_Selected (PHP code) in your onChange event - This won't
        > happen unless you hava a javascript function with that name that submits
        > the form contents to the server via a FORM POST or GET...
        >
        > Also... The value for your select box should I believe end up in
        > $_POST['keuze'] in PHP when the form is submitted...
        >
        > And... return("documen t.form.achterna am.value='$stic ky'");
        > Again, you've got your knickers in a twist - the return statement will
        > not return a value into achternaam like I think you expect... I really
        > think you are misunderstandin g how the two technologies fit together...
        >
        > If you are sure that your test will work when you enter 15 as a test
        > value - try different numbers to confirm that test works - If it does,
        > then I think you've not provided the entire code here for me (perhaps
        > anyone else) to give a full and proper answer/suggestion.
        >
        > Does any of the above help?
        > randelld[/color]


        Comment

        • Chung Leong

          #5
          Re: calling variable in onchange function

          "R.G. Vervoort" <roy.vervoort@r oyvervoort.nl> wrote in message
          news:407da380$0 $566$e4fe514c@n ews.xs4all.nl.. .[color=blue]
          >
          > WHAT AM I DOING WRONG?[/color]

          You're programming in the wrong language. Use ASP.NET instead.


          Comment

          • Ashmodai

            #6
            Re: calling variable in onchange function

            Chung Leong scribbled something along the lines of:
            [color=blue]
            > "R.G. Vervoort" <roy.vervoort@r oyvervoort.nl> wrote in message
            > news:407da380$0 $566$e4fe514c@n ews.xs4all.nl.. .
            >[color=green]
            >>WHAT AM I DOING WRONG?[/color]
            >
            >
            > You're programming in the wrong language. Use ASP.NET instead.
            >
            >[/color]

            Bad comment, no twinkie.

            --
            Alan Plum, WAD/WD, Mushroom Cloud Productions

            Comment

            • Hello World

              #7
              Re: calling variable in onchange function

              > You're programming in the wrong language. Use ASP.NET instead.

              No no, he will become bald if he does :-)

              Comment

              • Savut

                #8
                Re: calling variable in onchange function

                Place_Selected( 'naam_keuze.val ue')

                you have passed the value of a javascript, this can't work, you have to
                explicitely write the value or a php variable, because you are in a php
                statement (server side, so no javascript value here).

                Savut

                "R.G. Vervoort" <roy.vervoort@r oyvervoort.nl> wrote in message
                news:407dd2d3$0 $570$e4fe514c@n ews.xs4all.nl.. .[color=blue]
                > it works great, as long as the number (15 or ...) is an id in the
                > database.
                > i just will not accept the "." (dot) in the line
                >
                >
                >
                > "Reply Via Newsgroup" <reply-to-newsgroup@pleas e.com> schreef in bericht
                > news:J9kfc.1214 95$Ig.78826@pd7 tw2no...[color=green]
                >> R.G. Vervoort wrote:
                >>[color=darkred]
                >> > I am using a javafunction (onclick in select) in which i am calling a
                >> > function in php (thats why i send this to both php and javascript
                >> > newsgroups).
                >> >
                >> > in the onclick i call the function "Place_Selected " with the value from[/color][/color]
                > the[color=green][color=darkred]
                >> > select (naam_keuze.val ue)
                >> >
                >> > in the function the value becomes the $zoek_id and searches in the[/color][/color]
                > database[color=green][color=darkred]
                >> > for the record with the id of $zoek_id
                >> >
                >> > the naam_keuze.valu e does not give the value to $zoek_id
                >> >
                >> > When i replace naam_keuze.valu e for a number (15) i works great.
                >> >
                >> > WHAT AM I DOING WRONG?
                >> >
                >> >
                >> > This is my code
                >> >
                >> >
                >> >
                >> > Function Place_Selected( $zoek_id)
                >> > {
                >> > include("data.p hp");
                >> > $link=mysql_con nect($db_host, $username, $password) or die("Database
                >> > error!");
                >> > mysql_select_db ($database , $link)or die("Couldn't open $db:
                >> > ".mysql_error() );
                >> > $sql=mysql_quer y("SELECT * FROM specialismen WHERE id='$zoek_id'") ;
                >> >
                >> > if ($sql)
                >> > {
                >> > while($blah2 = mysql_fetch_arr ay($sql))
                >> > {
                >> > $newsid = $blah2['id'];
                >> > $sticky = $blah2['naam'];
                >> > }
                >> > }
                >> > return("documen t.form.achterna am.value='$stic ky'");
                >> > }
                >> >
                >> >
                >> > <select id=naam_keuze size=1 name=keuze style=visibilit y:hidden
                >> >[/color][/color]
                > onchange=".Plac e_Selected('naa m_keuze.value') .";document.for m.voornaam.valu e[color=green][color=darkred]
                >> > =naam_keuze.val ue;>
                >> > <option value=0>Kies
                >> > $options
                >> > </select>
                >> >
                >> > Thanks for suggestions
                >> >
                >> > roy
                >> >
                >> >[/color]
                >>
                >> I don't see how it could work, even with a numeric value of 15... What
                >> version of PHP are you using?
                >>
                >> Here's why I think it shouldn't work...
                >>
                >> Javascript is client side - PHP is server side... thus, you attempt to
                >> call Place_Selected (PHP code) in your onChange event - This won't
                >> happen unless you hava a javascript function with that name that submits
                >> the form contents to the server via a FORM POST or GET...
                >>
                >> Also... The value for your select box should I believe end up in
                >> $_POST['keuze'] in PHP when the form is submitted...
                >>
                >> And... return("documen t.form.achterna am.value='$stic ky'");
                >> Again, you've got your knickers in a twist - the return statement will
                >> not return a value into achternaam like I think you expect... I really
                >> think you are misunderstandin g how the two technologies fit together...
                >>
                >> If you are sure that your test will work when you enter 15 as a test
                >> value - try different numbers to confirm that test works - If it does,
                >> then I think you've not provided the entire code here for me (perhaps
                >> anyone else) to give a full and proper answer/suggestion.
                >>
                >> Does any of the above help?
                >> randelld[/color]
                >
                >[/color]

                Comment

                • Chung Leong

                  #9
                  Re: calling variable in onchange function


                  "Hello World" <helloworld@500 mghosting.com> wrote in message
                  news:35b5a05e.0 404150406.1595b 0a5@posting.goo gle.com...[color=blue][color=green]
                  > > You're programming in the wrong language. Use ASP.NET instead.[/color]
                  >
                  > No no, he will become bald if he does :-)[/color]

                  I'm serious. In ASP.NET the distinction between client and server is
                  blurred. You can attach an onclick handler to a dropdown on the server side,
                  and the handler would get called (on the server side) when the user makes a
                  selection.


                  Comment

                  • R.G. Vervoort

                    #10
                    Re: calling variable in onchange function

                    ok

                    I can see that it is a problem (for me)

                    but....

                    I can call a php function (with the ". ...... .") in the onchange (wich is
                    javascript as i believe), is it not possible to call the javascript into a
                    php function or to decalare a variable and the call it in the function
                    (since it is possible to put a variable in the function wich works).

                    thanks anyway

                    Roy



                    "Savut" <webki@hotmail. com> schreef in bericht
                    news:GjAfc.2704 7$vF3.1605768@n ews20.bellgloba l.com...[color=blue]
                    > Place_Selected( 'naam_keuze.val ue')
                    >
                    > you have passed the value of a javascript, this can't work, you have to
                    > explicitely write the value or a php variable, because you are in a php
                    > statement (server side, so no javascript value here).
                    >
                    > Savut
                    >
                    > "R.G. Vervoort" <roy.vervoort@r oyvervoort.nl> wrote in message
                    > news:407dd2d3$0 $570$e4fe514c@n ews.xs4all.nl.. .[color=green]
                    > > it works great, as long as the number (15 or ...) is an id in the
                    > > database.
                    > > i just will not accept the "." (dot) in the line
                    > >
                    > >
                    > >
                    > > "Reply Via Newsgroup" <reply-to-newsgroup@pleas e.com> schreef in bericht
                    > > news:J9kfc.1214 95$Ig.78826@pd7 tw2no...[color=darkred]
                    > >> R.G. Vervoort wrote:
                    > >>
                    > >> > I am using a javafunction (onclick in select) in which i am calling a
                    > >> > function in php (thats why i send this to both php and javascript
                    > >> > newsgroups).
                    > >> >
                    > >> > in the onclick i call the function "Place_Selected " with the value[/color][/color][/color]
                    from[color=blue][color=green]
                    > > the[color=darkred]
                    > >> > select (naam_keuze.val ue)
                    > >> >
                    > >> > in the function the value becomes the $zoek_id and searches in the[/color]
                    > > database[color=darkred]
                    > >> > for the record with the id of $zoek_id
                    > >> >
                    > >> > the naam_keuze.valu e does not give the value to $zoek_id
                    > >> >
                    > >> > When i replace naam_keuze.valu e for a number (15) i works great.
                    > >> >
                    > >> > WHAT AM I DOING WRONG?
                    > >> >
                    > >> >
                    > >> > This is my code
                    > >> >
                    > >> >
                    > >> >
                    > >> > Function Place_Selected( $zoek_id)
                    > >> > {
                    > >> > include("data.p hp");
                    > >> > $link=mysql_con nect($db_host, $username, $password) or die("Database
                    > >> > error!");
                    > >> > mysql_select_db ($database , $link)or die("Couldn't open $db:
                    > >> > ".mysql_error() );
                    > >> > $sql=mysql_quer y("SELECT * FROM specialismen WHERE id='$zoek_id'") ;
                    > >> >
                    > >> > if ($sql)
                    > >> > {
                    > >> > while($blah2 = mysql_fetch_arr ay($sql))
                    > >> > {
                    > >> > $newsid = $blah2['id'];
                    > >> > $sticky = $blah2['naam'];
                    > >> > }
                    > >> > }
                    > >> > return("documen t.form.achterna am.value='$stic ky'");
                    > >> > }
                    > >> >
                    > >> >
                    > >> > <select id=naam_keuze size=1 name=keuze style=visibilit y:hidden
                    > >> >[/color]
                    > >[/color][/color]
                    onchange=".Plac e_Selected('naa m_keuze.value') .";document.for m.voornaam.valu e[color=blue][color=green][color=darkred]
                    > >> > =naam_keuze.val ue;>
                    > >> > <option value=0>Kies
                    > >> > $options
                    > >> > </select>
                    > >> >
                    > >> > Thanks for suggestions
                    > >> >
                    > >> > roy
                    > >> >
                    > >> >
                    > >>
                    > >> I don't see how it could work, even with a numeric value of 15... What
                    > >> version of PHP are you using?
                    > >>
                    > >> Here's why I think it shouldn't work...
                    > >>
                    > >> Javascript is client side - PHP is server side... thus, you attempt to
                    > >> call Place_Selected (PHP code) in your onChange event - This won't
                    > >> happen unless you hava a javascript function with that name that[/color][/color][/color]
                    submits[color=blue][color=green][color=darkred]
                    > >> the form contents to the server via a FORM POST or GET...
                    > >>
                    > >> Also... The value for your select box should I believe end up in
                    > >> $_POST['keuze'] in PHP when the form is submitted...
                    > >>
                    > >> And... return("documen t.form.achterna am.value='$stic ky'");
                    > >> Again, you've got your knickers in a twist - the return statement will
                    > >> not return a value into achternaam like I think you expect... I really
                    > >> think you are misunderstandin g how the two technologies fit together...
                    > >>
                    > >> If you are sure that your test will work when you enter 15 as a test
                    > >> value - try different numbers to confirm that test works - If it does,
                    > >> then I think you've not provided the entire code here for me (perhaps
                    > >> anyone else) to give a full and proper answer/suggestion.
                    > >>
                    > >> Does any of the above help?
                    > >> randelld[/color]
                    > >
                    > >[/color]
                    >[/color]


                    Comment

                    • Reply Via Newsgroup

                      #11
                      Re: calling variable in onchange function

                      R.G. Vervoort wrote:[color=blue]
                      > ok
                      >
                      > I can see that it is a problem (for me)
                      >
                      > but....
                      >
                      > I can call a php function (with the ". ...... .") in the onchange (wich is
                      > javascript as i believe), is it not possible to call the javascript into a
                      > php function or to decalare a variable and the call it in the function
                      > (since it is possible to put a variable in the function wich works).
                      >
                      > thanks anyway
                      >
                      > Roy
                      >[/color]

                      LET US GET THIS STRAIGHT (and I'm sorry I'm shouting here) BUT YOU
                      CANNOT UNDER NO CIRCUMSTANCES CALL PHP CODE USING AN ONCHANGE EVENT
                      USING THE CODE YOU HAVE IN YOUR ORIGINAL POST (I've copied a line of the
                      offending code below).

                      <select id=naam_keuze size=1 name=keuze style=visibilit y:hidden
                      onchange=".Plac e_Selected('naa m_keuze.value') .";document.for m.voornaam.valu e
                      =naam_keuze.val ue;>
                      <option value=0>Kies
                      $options
                      </select>

                      The code contained in the onChange event is expected to be javascript -
                      NOT PHP.

                      I'll say that again...

                      The code contained in the onChange event is expected to be javascript -
                      NOT PHP.


                      Your web browser does not process PHP - It can use form methods GET and
                      POST to communicate with a server, true, but the methods you have used
                      above WILL NOT WORK.

                      I'm not saying MIGHT - I'm not saying MAYBE - I'm not saying its a grey
                      area... I AM DEFINITE, I HAVE NO DOUBT.... PHP is server side - an
                      onChange event only has access to client side actions. The only link
                      between the two is a POST or a GET method and from the code I see in
                      your original post, I don't see this occuring.

                      Unless you make your code available, either here in a post or via a url,
                      folk are unlikely to be able to help you. You are wasteing your time.

                      You mention that some numeric input works for you and I say that is
                      rubbish... thrash... garbage. If you are adamant that it does work then
                      you are either mistaken, have a bug in your code and getting the result
                      from something other than PHP or you have not provided enough of source
                      code for anyone here to help to show how the data is being sent from
                      your client, to the server, and back, to give you this result you claim.

                      I honestly don't mean to offend, but while you persisted in making a
                      false claim and I needed to clear it up... I'll honestly do my best to
                      help though...

                      regards
                      randelld

                      Comment

                      Working...