Page 1 of 1

how to pass values in php through javascript

Posted: Thu Feb 04, 2010 10:33 pm
by swathisundar
I require to appear the Name which i click on the "new.php" page in the "Window_open.php" page..I use some Java Script also to accomplish this but I couldn't..

Could you pleas Help ...!!!

Expand|Select|Wrap|Line Numbers

1. ----------The Code in "Window_open.php" -------------------
2.
3. <html>
4.
5. <head>
6. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
7. <title>New Page 1</title>
8. </head>
9.
10. <body>
11.
12.
13. <FORM>
14. <INPUT type="button" value="New Window!" onClick="window.open('new.htm','mywindow','width=400,height=200')">
15. <br>
16. </FORM>
17.
18. <table width="533" height="242" border="1">
19. <tr>
20. <td align="center" valign="middle">Name<br><?php echo $post["txt"]?></td>
21. </tr>
22. </table>
23. </body>
24.
25. </html>
26.
27.
28. ------------------code in "new.php"---------------------------------

Expand|Select|Wrap|Line Numbers

1. <html>
2. <body>
3.
4. <p><font size="5">This is the result of&nbsp; on click Opening
5. Window..............</font><br>
6. <br>
7. <br>
8. <a id="100" onClick="window.close(),pass(100)">Taniya Moses<br></a>
9. <a id="101" onClick="window.close(),pass(101)">Ranjan Ramanayaka<br></a>
10. <a id="102" onClick="window.close(),pass(102)">Wikram Silva<br></a>
11. <a id="103" onClick="window.close(),pass(103)">Wikram Silva<br></a>
12. <a id="104" onClick="window.close(),pass(104)">Janaka Wujerathna<br></a>
13. <br>
14.
15.
16. <Script type="text/javascript">
17. function pass(I)
18. {
19. var C = document.getElementById("I").value;
20. document.getElementById("txt").value = C;
21. window.location.href="new.html";
22. //window.close();
23. }
24. </Script>
25. </p>
26. <form action="window_open.html", method="post">
27. <input type="hidden" id="txt" name="txt" />
28. </form>
29.
30. </body>
31.
32. </html>

Re: how to pass values in php through javascript

Posted: Fri Feb 05, 2010 6:57 pm
by JAB Creations
First please use BBcode, such as [ code ] (without the spaces inside the brackets).

Second don't put script elements inside the body. Yes it's technically valid though it's such a bad practice and it will lead you down the wrong path ultimately, trust me on this.

Thirdly PHP can write in to anything, that's why it's serverside. In Firefox 3.5+ when you view source it will create links so you can view the source, and then click the link to your script include. If you see PHP code instead of the effects of PHP code then you're going to need to add this in to your Apache .htaccess file...

Code: Select all

AddType application/x-httpd-php .js
Also clear IE's cache and make sure that IE see's the JavaScript file as a script file (viewing in Windows Explorer's details and making sure it says Script instead of HTML file even if the extension is .js). If it's not appearing as a script file you can override the headers either with .htaccess for the file type or use PHP. Here is an example with PHP...

Code: Select all

<?php header('content-type:text/javascript');?>
Once you've made sure you've got that covered there is a tactic you should use to minimize bandwidth. You're going to want two JavaScript files for all of your site's JavaScript. First you want index.js for all of the static scripts that never have anything that change (e.g. you won't be using PHP to echo anything in this file). Secondly you'll want your onload.js file for your anonymous onload event and JavaScript variables that will most likely have variables that have dynamic values. That is where you want to echo stuff out with PHP.

So an example...

onload.js

Code: Select all

<?php header('content-type:text/javascript');
header('content-type:application/x-javascript');
ob_start("ob_gzhandler");
echo '//<![CDATA['."\n";
echo "var section = '".$_SESSION['cms_section']."';\n";
echo "var page = '".$_SESSION['cms_page']."';\n";
echo "var audio = '".$_SESSION['audio']."';\n";
?>
window.onload = function()
{
 //onload function here
 //onload function here
 //onload function here
 alert('call all onload functions here, you can also have PHP dictate what happens here');
}
<?php echo '//]]>';
ob_end_flush();?>
Remember to test your work with Firefox, Safari, Opera, and IE to make sure there are no script errors that would break your page.

My last bit of advice, adhere to Firefox's strict warnings, it's worth it in the end and will give you an edge over others.

Good luck! :)