This is a discussion on Does JavaScript not work in php web file? within the PHP General forums, part of the PHP Programming Forums category; Dear my friends... I am confused to implement JavaScript code in php web file. I want my JavaScript change the &...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Dear my friends...
I am confused to implement JavaScript code in php web file. I want my JavaScript change the "selected" item of <select> tag based on the "selected" item of previous <selected> in same php page. Please help me.... tell my where is my mistake. === Here is my code pelangganbaru.php === <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <META name="generator" content="HTML Tidy for Linux/x86 (vers 31 October 2006), see www.w3.org"> <TITLE>guru.com - Menu for new customer</TITLE> </HEAD> <Script Language="JavaScript"> function opsi(){ if (document.formulir.opsinegara.value!="Virgina Islands (USA)";){ document.formulir.opsistate.options[0].selected=true; } } </Script> <BODY> <?php if (isset($pid)){ $idproduk=$_GET['pid']; echo "Produk ID: $idproduk<br>"; } ?> <table border=0 align="center" cellpadding=0 cellspacing=0 width=800> <tr><td></td><td><table border=0 align="center" cellpadding=0 cellspacing=0 width=400> <form method="post" action="cgi/cgipelangganbaru.php?pid=$idproduk" name="formulir"> <tr> <td align="right" bgcolor="#F5D78B"> 3. Country: </td> <td align="center"> </td> <td align="left"> <select name="negara" onchange="jsopsi()" name="opsinegara"> <?php include_once "koneksi.php"; $sqlnya="select country from countries"; $kelas=new koneksi(); $klas=$kelas->getkoneksi("survey",$sqlnya); if ( mysql_num_rows($klas) > 0 ) { while( list($negara) = mysql_fetch_row($klas) ) { echo "<option id=\"opsi"."$negara\" value=\"$negara\">$negara</option>"; } } else { echo 'No results found'; } ?> </select> </td> </tr> <tr> <td align="right" bgcolor="#F5D78B"> 4. State: </td> <td align="center"> </td> <td align="left"> <select name="opsistate"> <?php include_once "koneksi.php"; $sqlnya="select state,statename from states"; $kelas=new koneksi(); $klas=$kelas->getkoneksi("survey",$sqlnya); if ( mysql_num_rows($klas) > 0 ) { echo "<option name=\"opsi".$state."\" value=\"\"></option>"; if ($negara=="Virgin Islands (US)"){ echo "<option name=\"nonusa\" value=\"00\" selected>Non USA</option>"; }else{ echo "<option name=\"nonusa\" value=\"00\">Non USA</option>"; } while( list($state,$statename) = mysql_fetch_row($klas) ) { echo "<option name=\"state\" value=\"$state\">$statename ($state)</option>"; } } else { echo 'No results found'; } ?> </select> </td> </tr> <tr> <td align="right"> <input type="submit"> </td> <td align="center"> </td> <td align="left"> <input type="reset"> </td> </tr> </form> </table> </td><td></td></tr></table> </BODY> </HTML> -- Patrik Hasibuan <patrikh@penguin-teknologi.com> Junior Programmer |
|
|||
|
Hi,
JavaScript codes works in php files. Yours code is also fine. But check your calling function and defined functions. Calling function is jsopsi and defined function is opsi. Please check. Other than this everything is ok Warm Regards, Sanjeev http://www.sanchanworld.com/ http://webdirectory.sanchanworld.com - Submit your website URL http://webhosting.sanchanworld.com - Choose your best web hosting plan -----Original Message----- From: Patrik Hasibuan [mailto:patrikh@penguin-teknologi.com] Sent: Saturday, August 25, 2007 9:17 PM To: php-general@lists.php.net Subject: [php] Does JavaScript not work in php web file? Dear my friends... I am confused to implement JavaScript code in php web file. I want my JavaScript change the "selected" item of <select> tag based on the "selected" item of previous <selected> in same php page. Please help me.... tell my where is my mistake. === Here is my code pelangganbaru.php === <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <META name="generator" content="HTML Tidy for Linux/x86 (vers 31 October 2006), see www.w3.org"> <TITLE>guru.com - Menu for new customer</TITLE> </HEAD> <Script Language="JavaScript"> function opsi(){ if (document.formulir.opsinegara.value!="Virgina Islands (USA)";){ document.formulir.opsistate.options[0].selected=true; } } </Script> <BODY> <?php if (isset($pid)){ $idproduk=$_GET['pid']; echo "Produk ID: $idproduk<br>"; } ?> <table border=0 align="center" cellpadding=0 cellspacing=0 width=800> <tr><td></td><td><table border=0 align="center" cellpadding=0 cellspacing=0 width=400> <form method="post" action="cgi/cgipelangganbaru.php?pid=$idproduk" name="formulir"> <tr> <td align="right" bgcolor="#F5D78B"> 3. Country: </td> <td align="center"> </td> <td align="left"> <select name="negara" onchange="jsopsi()" name="opsinegara"> <?php include_once "koneksi.php"; $sqlnya="select country from countries"; $kelas=new koneksi(); $klas=$kelas->getkoneksi("survey",$sqlnya); if ( mysql_num_rows($klas) > 0 ) { while( list($negara) = mysql_fetch_row($klas) ) { echo "<option id=\"opsi"."$negara\" value=\"$negara\">$negara</option>"; } } else { echo 'No results found'; } ?> </select> </td> </tr> <tr> <td align="right" bgcolor="#F5D78B"> 4. State: </td> <td align="center"> </td> <td align="left"> <select name="opsistate"> <?php include_once "koneksi.php"; $sqlnya="select state,statename from states"; $kelas=new koneksi(); $klas=$kelas->getkoneksi("survey",$sqlnya); if ( mysql_num_rows($klas) > 0 ) { echo "<option name=\"opsi".$state."\" value=\"\"></option>"; if ($negara=="Virgin Islands (US)"){ echo "<option name=\"nonusa\" value=\"00\" selected>Non USA</option>"; }else{ echo "<option name=\"nonusa\" value=\"00\">Non USA</option>"; } while( list($state,$statename) = mysql_fetch_row($klas) ) { echo "<option name=\"state\" value=\"$state\">$statename ($state)</option>"; } } else { echo 'No results found'; } ?> </select> </td> </tr> <tr> <td align="right"> <input type="submit"> </td> <td align="center"> </td> <td align="left"> <input type="reset"> </td> </tr> </form> </table> </td><td></td></tr></table> </BODY> </HTML> -- Patrik Hasibuan <patrikh@penguin-teknologi.com> Junior Programmer -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|||
|
Dear Sanjeev,
you're right. I am sorry for my carelessness. I corrected my code and than try it again. But again, it does not work as I expect. I even put << prompt("this function is being called"); >> in the jsopsi() but again the prompt does not come out. Pffuih... I am desperate only because of this small thing. Please keep telling me what's wrong with my code. Do I have to write the JavaScript in another way if the javascript is placed in php file? == //This is my current code: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <META name="generator" content="HTML Tidy for Linux/x86 (vers 31 October 2006), see www.w3.org"> <TITLE>guru.com - Menu for new customer</TITLE> <Script Language="JavaScript"> function jsopsi(){ prompt("This prompt comes out if this function is really being called"); if (document.formulir.opsinegara.value!="Virgina Islands (USA)";){ document.formulir.opsistate.options[5].selected=true; } } </Script> </head> <BODY> <?php if (isset($pid)){ $idproduk=$_GET['pid']; echo "Produk ID: $idproduk<br>"; } ?> <table border=0 align="center" cellpadding=0 cellspacing=0 width=800> <tr><td></td><td><table border=0 align="center" cellpadding=0 cellspacing=0 width=400> <form method="post" action="cgi/cgipelangganbaru.php?pid=$idproduk" name="formulir"> <tr> <td align="right" bgcolor="#F5D78B"> 3. Country: </td> <td align="center"> </td> <td align="left"> <select name="negara" onchange="jsopsi()" name="opsinegara"> <?php include_once "koneksi.php"; $sqlnya="select country from countries"; $kelas=new koneksi(); $klas=$kelas->getkoneksi("survey",$sqlnya); if ( mysql_num_rows($klas) > 0 ) { while( list($negara) = mysql_fetch_row($klas) ) { echo "<option id=\"opsi"."$negara\" value=\"$negara\">$negara</option>"; } } else { echo 'No results found'; } ?> </select> </td> </tr> <tr> <td align="right" bgcolor="#F5D78B"> 4. State: </td> <td align="center"> </td> <td align="left"> <select name="opsistate"> <?php include_once "koneksi.php"; $sqlnya="select state,statename from states"; $kelas=new koneksi(); $klas=$kelas->getkoneksi("survey",$sqlnya); if ( mysql_num_rows($klas) > 0 ) { echo "<option name=\"state\" value=\"\"></option>"; while( list($state,$statename) = mysql_fetch_row($klas) ) { echo "<option name=\"state\" value=\"$state\">$statename ($state)</option>"; } } else { echo 'No results found'; } ?> </select> </td> </tr> </form> </table> </td><td></td></tr></table> </BODY> </HTML> == On Sat, 25 Aug 2007 22:26:49 +0530 "Sanjeev N" <snsanju@gmail.com> wrote: > Hi, > > JavaScript codes works in php files. Yours code is also fine. > But check your calling function and defined functions. Calling function is > jsopsi and defined function is opsi. Please check. Other than this > everything is ok > > Warm Regards, > Sanjeev > http://www.sanchanworld.com/ > http://webdirectory.sanchanworld.com - Submit your website URL > http://webhosting.sanchanworld.com - Choose your best web hosting plan > -----Original Message----- > From: Patrik Hasibuan [mailto:patrikh@penguin-teknologi.com] > Sent: Saturday, August 25, 2007 9:17 PM > To: php-general@lists.php.net > Subject: [php] Does JavaScript not work in php web file? > > Dear my friends... > > I am confused to implement JavaScript code in php web file. > > I want my JavaScript change the "selected" item of <select> tag based on the > "selected" item of previous <selected> in same php page. > > Please help me.... tell my where is my mistake. > > === > Here is my code pelangganbaru.php > === > <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> > > <HTML> > <HEAD> > <META name="generator" content="HTML Tidy for Linux/x86 (vers 31 October > 2006), see www.w3.org"> > > <TITLE>guru.com - Menu for new customer</TITLE> > </HEAD> > <Script Language="JavaScript"> > function opsi(){ > if (document.formulir.opsinegara.value!="Virgina Islands (USA)";){ > document.formulir.opsistate.options[0].selected=true; > } > } > </Script> > > <BODY> > <?php > if (isset($pid)){ > $idproduk=$_GET['pid']; > echo "Produk ID: $idproduk<br>"; > } > ?> > > <table border=0 align="center" cellpadding=0 cellspacing=0 width=800> > <tr><td></td><td><table border=0 align="center" cellpadding=0 cellspacing=0 > width=400> > <form method="post" action="cgi/cgipelangganbaru.php?pid=$idproduk" > name="formulir"> > <tr> > <td align="right" bgcolor="#F5D78B"> > 3. Country: > </td> > <td align="center"> > </td> > <td align="left"> > <select name="negara" onchange="jsopsi()" name="opsinegara"> > <?php > include_once "koneksi.php"; > $sqlnya="select country from countries"; > $kelas=new koneksi(); > $klas=$kelas->getkoneksi("survey",$sqlnya); > if ( mysql_num_rows($klas) > 0 ) { > while( list($negara) = mysql_fetch_row($klas) ) { > echo "<option id=\"opsi"."$negara\" > value=\"$negara\">$negara</option>"; > } > } else { > echo 'No results found'; > } > ?> > </select> > </td> > </tr> > <tr> > <td align="right" bgcolor="#F5D78B"> > 4. State: > </td> > <td align="center"> > </td> > <td align="left"> > <select name="opsistate"> > <?php > include_once "koneksi.php"; > $sqlnya="select state,statename from states"; > $kelas=new koneksi(); > $klas=$kelas->getkoneksi("survey",$sqlnya); > if ( mysql_num_rows($klas) > 0 ) { > echo "<option name=\"opsi".$state."\" value=\"\"></option>"; > if ($negara=="Virgin Islands (US)"){ > echo "<option name=\"nonusa\" value=\"00\" selected>Non > USA</option>"; > }else{ > echo "<option name=\"nonusa\" value=\"00\">Non > USA</option>"; > } > while( list($state,$statename) = mysql_fetch_row($klas) ) { > echo "<option name=\"state\" value=\"$state\">$statename > ($state)</option>"; > } > } else { > echo 'No results found'; > } > ?> > </select> > </td> > </tr> > <tr> > <td align="right"> > <input type="submit"> > </td> > <td align="center"> > </td> > <td align="left"> > <input type="reset"> > </td> > </tr> > </form> > </table> > </td><td></td></tr></table> > </BODY> > </HTML> > -- > Patrik Hasibuan <patrikh@penguin-teknologi.com> > Junior Programmer > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > -- Patrik Hasibuan <patrikh@penguin-teknologi.com> Junior Programmer |
|
|||
|
Hi,
I tried this code by removing PHP codes and made as HTML. As you said prompt dint come to me also. When I checked JavaScript errors then I saw that prompt function had some errors (like ; in wrong place). I have corrected the errors in script. <Script Language="JavaScript"> function jsopsi(){ prompt("This prompt comes out if this function is really being called"); if(document.formulir.opsinegara.value!="Virgina Islands (USA)"){ document.formulir.opsistate.options[5].selected=true; } } </Script> And the complete code after converting to HTML is as follows. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <META name="generator" content="HTML Tidy for Linux/x86 (vers 31 October 2006), see www.w3.org"> <TITLE>guru.com - Menu for new customer</TITLE> <Script Language="JavaScript"> function jsopsi(){ prompt("This prompt comes out if this function is really being called"); if(document.formulir.opsinegara.value!="Virgina Islands (USA)"){ document.formulir.opsistate.options[5].selected=true; } } </Script> </head> <BODY> <?php if (isset($pid)){ $idproduk=$_GET['pid']; echo "Produk ID: $idproduk<br>"; } ?> <table border=0 align="center" cellpadding=0 cellspacing=0 width=800> <tr><td></td><td><table border=0 align="center" cellpadding=0 cellspacing=0 width=400> <form method="post" action="cgi/cgipelangganbaru.php?pid=$idproduk" name="formulir"> <tr> <td align="right" bgcolor="#F5D78B"> 3. Country: </td> <td align="center"> </td> <td align="left"> <select name="negara" onchange="jsopsi()" name="opsinegara"> <option id="opsi1" value="negara1">negara1</option> <option id="opsi2" value="negara2">negara2</option> <option id="opsi3" value="negara3">negara3</option> <option id="opsi4" value="negara4">negara4</option> <option id="opsi5" value="negara5">negara5</option> </select> </td> </tr> <tr> <td align="right" bgcolor="#F5D78B"> 4. State: </td> <td align="center"> </td> <td align="left"> <select name="opsistate"> <option name="state1" value="1">1</option> <option name="state2" value="2">4</option> <option name="state3" value="3">3</option> </select> </td> </tr> </form> </table> </td><td></td></tr></table> </BODY> </HTML> You please check with PHP codes included (also I suggest you check the javascript errors if it don't works). Warm Regards, Sanjeev http://www.sanchanworld.com/ http://webdirectory.sanchanworld.com - Submit your website URL http://webhosting.sanchanworld.com - Choose your best web hosting plan -----Original Message----- From: Patrik Hasibuan [mailto:patrikh@penguin-teknologi.com] Sent: Sunday, August 26, 2007 3:12 PM To: php-general@lists.php.net Subject: Re: [php] Does JavaScript not work in php web file? Dear Sanjeev, you're right. I am sorry for my carelessness. I corrected my code and than try it again. But again, it does not work as I expect. I even put << prompt("this function is being called"); >> in the jsopsi() but again the prompt does not come out. Pffuih... I am desperate only because of this small thing. Please keep telling me what's wrong with my code. Do I have to write the JavaScript in another way if the javascript is placed in php file? == //This is my current code: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <META name="generator" content="HTML Tidy for Linux/x86 (vers 31 October 2006), see www.w3.org"> <TITLE>guru.com - Menu for new customer</TITLE> <Script Language="JavaScript"> function jsopsi(){ prompt("This prompt comes out if this function is really being called"); if (document.formulir.opsinegara.value!="Virgina Islands (USA)";){ document.formulir.opsistate.options[5].selected=true; } } </Script> </head> <BODY> <?php if (isset($pid)){ $idproduk=$_GET['pid']; echo "Produk ID: $idproduk<br>"; } ?> <table border=0 align="center" cellpadding=0 cellspacing=0 width=800> <tr><td></td><td><table border=0 align="center" cellpadding=0 cellspacing=0 width=400> <form method="post" action="cgi/cgipelangganbaru.php?pid=$idproduk" name="formulir"> <tr> <td align="right" bgcolor="#F5D78B"> 3. Country: </td> <td align="center"> </td> <td align="left"> <select name="negara" onchange="jsopsi()" name="opsinegara"> <?php include_once "koneksi.php"; $sqlnya="select country from countries"; $kelas=new koneksi(); $klas=$kelas->getkoneksi("survey",$sqlnya); if ( mysql_num_rows($klas) > 0 ) { while( list($negara) = mysql_fetch_row($klas) ) { echo "<option id=\"opsi"."$negara\" value=\"$negara\">$negara</option>"; } } else { echo 'No results found'; } ?> </select> </td> </tr> <tr> <td align="right" bgcolor="#F5D78B"> 4. State: </td> <td align="center"> </td> <td align="left"> <select name="opsistate"> <?php include_once "koneksi.php"; $sqlnya="select state,statename from states"; $kelas=new koneksi(); $klas=$kelas->getkoneksi("survey",$sqlnya); if ( mysql_num_rows($klas) > 0 ) { echo "<option name=\"state\" value=\"\"></option>"; while( list($state,$statename) = mysql_fetch_row($klas) ) { echo "<option name=\"state\" value=\"$state\">$statename ($state)</option>"; } } else { echo 'No results found'; } ?> </select> </td> </tr> </form> </table> </td><td></td></tr></table> </BODY> </HTML> == On Sat, 25 Aug 2007 22:26:49 +0530 "Sanjeev N" <snsanju@gmail.com> wrote: > Hi, > > JavaScript codes works in php files. Yours code is also fine. > But check your calling function and defined functions. Calling function is > jsopsi and defined function is opsi. Please check. Other than this > everything is ok > > Warm Regards, > Sanjeev > http://www.sanchanworld.com/ > http://webdirectory.sanchanworld.com - Submit your website URL > http://webhosting.sanchanworld.com - Choose your best web hosting plan > -----Original Message----- > From: Patrik Hasibuan [mailto:patrikh@penguin-teknologi.com] > Sent: Saturday, August 25, 2007 9:17 PM > To: php-general@lists.php.net > Subject: [php] Does JavaScript not work in php web file? > > Dear my friends... > > I am confused to implement JavaScript code in php web file. > > I want my JavaScript change the "selected" item of <select> tag based on the > "selected" item of previous <selected> in same php page. > > Please help me.... tell my where is my mistake. > > === > Here is my code pelangganbaru.php > === > <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> > > <HTML> > <HEAD> > <META name="generator" content="HTML Tidy for Linux/x86 (vers 31 October > 2006), see www.w3.org"> > > <TITLE>guru.com - Menu for new customer</TITLE> > </HEAD> > <Script Language="JavaScript"> > function opsi(){ > if (document.formulir.opsinegara.value!="Virgina Islands (USA)";){ > document.formulir.opsistate.options[0].selected=true; > } > } > </Script> > > <BODY> > <?php > if (isset($pid)){ > $idproduk=$_GET['pid']; > echo "Produk ID: $idproduk<br>"; > } > ?> > > <table border=0 align="center" cellpadding=0 cellspacing=0 width=800> > <tr><td></td><td><table border=0 align="center" cellpadding=0 cellspacing=0 > width=400> > <form method="post" action="cgi/cgipelangganbaru.php?pid=$idproduk" > name="formulir"> > <tr> > <td align="right" bgcolor="#F5D78B"> > 3. Country: > </td> > <td align="center"> > </td> > <td align="left"> > <select name="negara" onchange="jsopsi()" name="opsinegara"> > <?php > include_once "koneksi.php"; > $sqlnya="select country from countries"; > $kelas=new koneksi(); > $klas=$kelas->getkoneksi("survey",$sqlnya); > if ( mysql_num_rows($klas) > 0 ) { > while( list($negara) = mysql_fetch_row($klas) ) { > echo "<option id=\"opsi"."$negara\" > value=\"$negara\">$negara</option>"; > } > } else { > echo 'No results found'; > } > ?> > </select> > </td> > </tr> > <tr> > <td align="right" bgcolor="#F5D78B"> > 4. State: > </td> > <td align="center"> > </td> > <td align="left"> > <select name="opsistate"> > <?php > include_once "koneksi.php"; > $sqlnya="select state,statename from states"; > $kelas=new koneksi(); > $klas=$kelas->getkoneksi("survey",$sqlnya); > if ( mysql_num_rows($klas) > 0 ) { > echo "<option name=\"opsi".$state."\" value=\"\"></option>"; > if ($negara=="Virgin Islands (US)"){ > echo "<option name=\"nonusa\" value=\"00\" selected>Non > USA</option>"; > }else{ > echo "<option name=\"nonusa\" value=\"00\">Non > USA</option>"; > } > while( list($state,$statename) = mysql_fetch_row($klas) ) { > echo "<option name=\"state\" value=\"$state\">$statename > ($state)</option>"; > } > } else { > echo 'No results found'; > } > ?> > </select> > </td> > </tr> > <tr> > <td align="right"> > <input type="submit"> > </td> > <td align="center"> > </td> > <td align="left"> > <input type="reset"> > </td> > </tr> > </form> > </table> > </td><td></td></tr></table> > </BODY> > </HTML> > -- > Patrik Hasibuan <patrikh@penguin-teknologi.com> > Junior Programmer > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > -- Patrik Hasibuan <patrikh@penguin-teknologi.com> Junior Programmer -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |