#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    2
    Rep Power
    0

    javascript inside php tags


    Hello

    I need some help with my php script. I try to make a javascript work inside my php tags. This script is an advanced combo script that uses two boxes, one as the main categories, the other as the links associated with each category. I don't get any error message on my page. I have tested the javascript with just statical values (without the connection to my database and php coding) and that worked. I don't think it's any problem with my php code and the resultat from DB. But I'm not sure if I have enclosed the javascript right inside my php-tags. Because I don't get the any action when I select or click at any of the values in the form. sorry, for my bad english.

    The javascript is from http://javascriptkit.com/script/cut183.shtml

    I have made a demo

    <form name="doublecombo"> <p><select name="example" size="7" onChange="redirect(this.options.selectedIndex)">
    <? ....DB connection...
    do {
    print ('<option value="'.$DB->GetField("id").'">'.$DB->GetField("name").'</option>'); }
    while($DB->move_next()); ?>

    <select name="stage2" size="4">
    <option value="http://...">....</option> </select>
    <input type="button" name="test" value="go!" onClick="go()"> </p>

    <? echo("<SCRIPT>");
    echo("var groups=document.doublecombo.example.options.length");
    echo("var group=new Array(groups)");
    echo("for (i=0; i<groups; i++)");
    echo("group[i]=new Array()");

    $strSQL = ".......";
    $DB->Query($strSQL);
    $a=0;
    if ($DB->Num_rows()>0)
    do {
    $id=($DB->GetField("id"));
    $DB2 = new DB_Sql;
    $DB2->connect();
    $strSQL2 = "SELECT .....";
    $DB2->Query($strSQL2);
    $b=0;
    if ($DB2->Num_rows()>0)
    do {
    $nameprod=($DB2->GetField("name")); $idprod=($DB2->GetField("id"));
    print("group[$a]");
    print("[$b]");
    print("=new Option(\"$nameprod\",\"http://....=$idprod\")"); $b++;
    }while($DB2->move_next());
    $a++;
    }while($DB->move_next());
    $DB2->close();

    echo("var temp=document.doublecombo.stage2");
    echo("function redirect(x){");
    echo("for (m=temp.options.length-1;m>0;m--)");
    echo("temp.options[m]=null");
    echo("for (i=0;i<group[x].length;i++){");
    echo("temp.options[i]=new Option(group[x][i].text,group[x][i].value)");
    echo("}");
    echo("temp.options[0].selected=true");
    echo("}");
    echo("function go(){");
    echo("location=temp.options[temp.selectedIndex].value"); echo("}");
    echo("</script>");
    ?> </form>
    Last edited by andy123; February 5th, 2004 at 08:29 AM.
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    796
    could you make this any harder to read?
  4. #3
  5. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Sep 2002
    Location
    667 - neighbour of the beast!
    Posts
    1,113
    Rep Power
    138
    He could've used a tiny grey font!

  6. #4
  7. Why so angry?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2004
    Posts
    1,939
    Rep Power
    901
    i don't know the class you use for the database, so i can't really tell you if that stuff is working, but why don't you just break out of php to print the javascript? would be a lot easier.

    PHP Code:
    <?
    }while($DB->move_next()); 
    $DB2->close(); 
    ?>
    var temp=document.doublecombo.stage2
    function redirect(x){
    for (m=temp.options.length-1;m>0;m--)
    temp.options[m]=null
    for (i=0;i<group[x].length;i++){
    temp.options[i]=new Option(group[x][i].text,group[x][i].value)
    }
    temp.options[0].selected=true

    function go(){
    location=temp.options[temp.selectedIndex].value; 
    }
    </script>
    </form>
    i did not check to see if i made any errors, but you get the idea..
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    2
    Rep Power
    0
    I do see your point and I have tried to do that. But I thought that PHP is a server-side language used to output my page, when the page is displayed it no longer exist; Javascript is the client-side language that reside on the page for user interaction. By that reason I think that I have to enclose the whole javascript inside php-tags.

    I provide som links if you want to see the difference:

    without php inside javascript

    with php inside javascript
  10. #6
  11. Why so angry?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2004
    Posts
    1,939
    Rep Power
    901
    i'm not exactly sure what you mean when you display the page it no longer exists.

    it doesn't matter that php is serverside and javascript is clientside. they cannot communicate at all, but think of breaking out of php as a long echo statement.

    <?
    $x = 5;
    echo "<html><head>"
    echo "Head";
    echo "</head><body>";
    echo "Body";
    echo "</body></html>";
    $x = 10;
    ?>

    is the same as

    <?
    $x = 5;
    ?>
    <html><head>
    Head
    </head><body>
    Body
    </body></html>
    <?
    $x = 10;
    ?>

    if you look at the source of both pages, there is this function:


    function redirect(x){
    for (m=temp.options.length-1;m>0;m--)
    temp.options[m]=null
    for (i=0;i<group[x].length;i++){
    temp.options[i]=new Option(group[x][i].text,group[x][i].value)
    }
    temp.options[0].selected=true
    }

    function go(){
    location=temp.options[temp.selectedIndex].value
    }

    i think that is the function you were saying didn't exist. i get a javascript error when loading the page though. i think there are problems with the javascript.

IMN logo majestic logo threadwatch logo seochat tools logo