PHP Variables into Javascript Function

Peter.Cort

TF's First Dry Ice User!
Messages
5,018
Location
Boston, MA
Hi all. Hopefully someone can fix my issue; been tearing my hair out for hours now trying to figure this out... What I'm trying to do is through a brief PHP function pull the amount of files in a directory, and then pass that number into a Javascript function so I can create an array with those numbers.

I'm trying super simple things to try and figure out why it isn't showing anything and this is the most basic of what I'm trying to do, so there must be some low level thing that I'm just balking on.

Code:
<?php $simple = "simple"; ?>
<html>
<head>
<script type="text/javascript">
<!--
alert("<?php echo $simple ?>");
-->
</script>
</head>

The result of this is an alert box with <?PHP echo $simple ?> in it. I've tried everything from single quotes around the php statement, write it as <?php echo $simple; ?>. Nothing works, it's just perpetually treated as a string and never grabbing the PHP variable. I'll give more code if necessary but I think this is enough. TIA...!
 
Try looking @ this example:


<form method="POST" action="stockinfo.php">
<p align="center">
<input type="text" name="symbol" size="5">
<br>
<input type="submit" name="Submit" value="Get Now!">
</p>
</form>

Then adding the following to it:

<html>
<head>
<title>More Info</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<script
language=javascript
src='http://testserver.com/tools/MP01?queryid=QF310&symbol=####'>
</script>
</body>
</html>

Combined:

<html>
<head>
<title>More Info</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<script
language=javascript
src='http://testserver.com/tools/MP01?queryid=QF310&symbol=<?php echo $_POST["symbol"]; ?>'>
</script>
</body>
</html>

Hope it helps,
Matt
 
mmm doesn't really help sorry :/ My problem resides with where to put the php file, as at the end of the day it's a small php script that needs to run to get me the desired results, I just need to put 2 integers into a javascript file.
 
Welp I fixed the issue with the help of some people on stackoverflow.. Turns out that I can't run the specific function and things that I wanted to do in a .html file, has to be a .php file. I figured that you should be able to run it in a .html cause you can do a lot of inline php with html but I guess this isn't one of the things!
 
Last edited:
Back
Top Bottom