运行PHP回显Javascript

时间:2013-11-15 06:17:53

标签: javascript php

我刚刚进入PHP,Javascript和HTML的世界,并希望社区对以下代码提供一些帮助。基本上我想将PHP从ODBC_connection中提取的变量传递到文本框中。第1-3行是我测试得到它更新的框,但PHP没有响应的任何东西都没有运行。我对此完全陌生,所以我意识到我必须遗漏一些微不足道的东西。

我欢迎任何有关我可以做些什么来解决这个问题的建议或意见,或者我可以做得更好。 谢谢。

<script type='text/javascript'>
document.getElementById('modeltxt').value = "test2";
</script>
<?php
echo "<script type='text/javascript'>";
echo "document.getElementById('modeltxt').value =\"TEST3\";";  
echo "document.getElementById('customertxt').value = $customer;";  
echo "document.getElementById('endusertxt').value = $enduser;";  
echo "document.getElementById(dongletxt').value = $dongle;";
echo "document.getElementById('shipdatetxt').value = $shipdate;";
echo "document.getElementById('chasistypetxt').value = $chasistype;";
echo "document.getElementById('chasisnumbertxt').value = $chasisnumber;";  
echo "document.getElementById('opsystxt').value = $opsys;";
echo "document.getElementById('dvd1txt').value = $dvd1;";  
echo "document.getElementById('dvd2txt').value = $dvd2;";
echo "document.getElementById('storagetxt').value = $storage;";  
echo "document.getElementById('nodrivetxt').value = $nodrive;";
echo "document.getElementById('drivesizetxt').value = $drivesize;";  
echo "document.getElementById('interface1txt').value = $interface1;";  
echo "document.getElementById('interface2txt').value = $interface2;";  
echo "document.getElementById('interface3txt').value = $interface3;";  
echo "document.getElementById('interface4txt').value = $interface4;";  
echo "document.getElementById('interface5txt').value = $interface5;";  
echo "document.getElementById('interface6txt').value = $interface6;";  
echo "document.getElementById('commentstxt').value = $comments;";
echo "document.getElementById('warrantyexptxt').value = $warrantyexp;";
echo "document.getElementById('extendedwarrantytxt').value = $extwarexp;";  
echo "document.getElementById('onsitetxt').value = $onsite;";
echo "document.getElementById('sqlversiontxt').value = $sqlversion;"; 
echo "<\script>";

2 个答案:

答案 0 :(得分:2)

您可以使用以下

创建动态JS
Define Content-Type on the top of your .js.php file:
<?
header('Content-Type: application/javascript');
// Write your php code
?>
 and call the js file like this ..

<script type="application/javascript" src="JS_PATH/name-of-file.js.php"></script>

如果你想使用内联php值,你可以像这样写

<script type="application/javascript">
document.getElementById('modeltxt').value = "<?php echo $dummy_value ?>";
</script>

答案 1 :(得分:0)

无需这样做。您可以在php中使用javascript,如下所示: -

正如你在问题中提到的那样

  

基本上我想将PHP从ODBC_connection中提取的变量传递给文本框。

<?php
  // Php block of code

?>
<script type="text/javscript">
  document.getElementById('modeltxt').value = "TEST3";
  document.getElementById('customertxt').value = "<?php echo $customer;?>"; 
   ......
   ....
   ...
</script>
<?php

// Another block of code

?>