Forum Moderators: coopster

Message Too Old, No Replies

Read value from php file method with JavaScript

         

user9

1:33 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



I'm trying to display contents read from a file by php with a JavaScript function. I'm not sure what I'm doing wrong.


function showMsg()
{
<?php
$lines = file("text.txt");
?>

alert("<?php echo $lines[0];?>");
}

Thank you for your help

Warboss Alex

1:38 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



Javascript and PHP don't mix in this sense - PHP is executed on the server before any Javascript is interpreted by the browser.

What you need, is something like this (in the same php file):

<?php
$file_arr = $file("file.txt");
$alert = $file_arr[0];
?>

//start html output here

<script language="Javascript">
function showMsg() {
alert("<?php echo $alert;?>");
}
</script>

<input type="button" value="click me" onClick="showMsg()" />

Hope this helps!

user9

2:24 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



Thank you very much Warboss Alex, that helped a lot.