#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Posts
    37
    Rep Power
    14

    PHP and Javascript Combined


    Hi,

    I'm working on a Javascript to update 2 listboxes with values, here's where PHP come in. I use PHP to get the values from a Database, but how can I use Javascript variables in PHP code?
    What I'm trying to do looks something like this simplified:

    <SCRIPT LANGUAGE="Javascript">
    function getPerioder(selection)
    {
    var ftgID = selection.options[selection.selectedIndex].value;
    <?php $result2=mysql_query("SELECT * FROM FtgPerioder WHERE ftgID=ftgID");
    $object=mysql_fetch_object($result2);
    $name=$object->name;
    ?>
    document.form1.perioder.options[0].text=name;
    }
    </SCRIPT>


    The thing I want to do is:
    replace ftgID in
    mysql_query("SELECT * FROM FtgPerioder WHERE ID=ftgID");
    with the javascript variable

    Help Greatly needed!
    Many Thanks!
    Jonas VersÚn
    Last edited by ds99jove; May 20th, 2001 at 05:42 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Posts
    37
    Rep Power
    14

    Question Help needed bad!


    I guess what I really want is to be able to access the Document Object Model from within PHP. If this is not possible, then someone please tell me how to:

    Have 2 listboxes
    Listbox nr 1 gets its values from a Database
    Listbox nr 2 is dependent on Listbox nr 1 and gets its values from a database.
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    You can not pass the javascript variable to PHP clientside, ie without a refresh.

    the other option is to load all possible results into javascript arays, but this could be a hefty file depending on the number of possible options, but that is your only other choice if you want to do this clientside.
    resistance is no longer futile :: 423
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    actually - you could use forms in hidden frames etc. Need to know your javascript for that one though.
    resistance is no longer futile :: 423
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Posts
    37
    Rep Power
    14
    Hi, thanks for the reply!

    Before I went to sleep last night I too reached the conclusion that storing everything in a javascript array right away was a possible solution. The number of objects should not pass ~150. That should not drop performance too much should it?

    About submitting the values, is there anyway of submitting a form without a submit button? Cause if the users have to click a submit button then I'm at the point that I'm specifically try to avoid

    (ie. The user selects value 2 from Listbox 1, then clicks the submit button and selects value C from lisbox 2... then he goes back and changes listbox 1 to value 1 and saves. Problem here is that value 1 do not have the value C and voila! A bug is created!)

    A simple solution would be something like:
    ..... onChange="refresh();">

    but would that make the form values global values as submitting the form does? And is there a refresh() function?

    Many Thanks Again!
    Jonas VersÚn
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    You could can submit via (something like ) onchange="do_submit();"

    function do_submit(){
    var yak=document.formname.fieldname.selectedIndex;
    document.formname.action=this.php?id=yak;
    document.formname.submit();
    }

    my javcascript is not that hot but something like that should work.
    resistance is no longer futile :: 423
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Posts
    37
    Rep Power
    14

    Great!!


    Thanks!

    Actually I only needed the:
    document.formname.submit();
    statement, since all the values I need are inside the form and the form has "METHOD='Post'" they are made global HTML variables I can access with PHP when the form is submitted, my problem was how to submit the form without the use of a button Now I've learned you can do that 2 using the DOM =)
    I'll check back later if (when there are any more troubles!

IMN logo majestic logo threadwatch logo seochat tools logo