View Single Post
  #4 (permalink)  
Old 07-15-07, 10:27 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
This command alone will do nothing:
javascript Code:
  1. document.write("<?php echo 'hello!'; ?>");
But when combined with the script tags it will echo "Hello!'
providing you have PHP on the server you are using.
Javascript Code:
  1. <script>
  2. document.write("<?php echo 'hello!'; ?>");
  3. </script>
Just as pressing a button in html will output a value from PHP using Javascript
providing you have PHP on the server you are using.

Example:
PHP Code:

<!--  PHP Section -->
<?php>
$message1 "You pressed the MESSAGE 1 button";
$message2 "You pressed the MESSAGE 2 button";
$message3 "You pressed the MESSAGE 3 button";
$message4 "You pressed the MESSAGE 4 button";
?>

<!-- HTML Section -->
<button onclick="msg1()">MESSAGE 1</button>
<button onclick="msg2()">MESSAGE 2</button>
<button onclick="msg3()">MESSAGE 3</button>
<button onclick="msg4()">MESSAGE 4</button>
<button onclick="alert('<?php echo 'You pressed the MESSAGE 5 button' ?>')">MESSAGE 5</button>

<!-- Javascript Section -->
<script>
function msg1(){alert("<?PHP echo $message1 ?>");}
function msg2(){alert("<?PHP echo $message2 ?>");}
function msg3(){alert("<?PHP echo $message3 ?>");}
function msg4(){alert("<?PHP echo $message4 ?>");}
</script>
You can also use Javascript's Alert command to execute a PHP
function that has the database routine in it.
You can put any PHP code in a PHP function and let Javascript use it.
The only thing you have to remember is to echo something last that
the Alert command can use.

Here is a simple example that uses Javascript's Alert command to open
a file in write mode and write something to the file. Then echo a
message saying "Saving data to the file".

I think with a little practice you will find that you can get javascript
to have PHP do most anything you want it to do.:
PHP Code:

<?php
function create_file()
{
 
$a fopen("test_data_write_.txt""w");
 
fwrite($a"Hello World.\r\n");
 
fclose($a);
 echo 
"Saving data to the file";}
?>
<script>
alert("<?PHP create_file() ?>");
alert("You just created a file called \"test_data_write.txt\".\nYou could have as easily, written data to a database.");
</script>
__________________
Jerry Broughton

Last edited by job0107; 07-15-07 at 10:42 PM.
Reply With Quote