如何在echo PHP函数调用中使用js变量?

时间:2019-03-28 21:43:00

标签: javascript php variables

我的实际应用是将内容从db替换为元素id。我从文本文件(其中的1558个)中知道它,并且效果很好。我*只是想尝试这种方法。提前谢谢你

此附加代码显示了我尝试过的内容。 php函数中的回显表明我将其纳入函数中。警报会显示发送值和返回值。

<html>
<head>
<script type="text/javascript">
   var b = "MyText.txt";
</script>
</head>
</html>

<?php

function add($str){
    echo "My strtest file is : " . $str . " ";  
    echo  " ";
    $MyTexkFile =  "<html><head></head><div> <p> This is my html text display </p></div id = \'mydiv\'><body><body></html>";

    echo "The string is " . " " . $MyTexkFile . " ";    
    $newstr = $str . " Add some text to str";
//return $MyTexkFile ;
//return $str;
return $newstr;
}
?>
<script>
function phpadd(b) {
alert(b);
var a = "1234";
alert(a);
  var padd = '<?php echo add("3579.txt")?>';  
  console.log(padd );
}
</script>
<button onclick='phpadd("4567.txt")'>add</button>

正如我在上面说的那样,在我尝试传递js变量(a或b)之前,代码可以正常工作。我试过单引号和双引号。有无波浪号。 我的错误:SyntaxError:“”文字未在脚本jsToPhpFunction.php:15:97结束之前终止。为什么是这样?我可以做些什么来使变量通过吗?

1 个答案:

答案 0 :(得分:0)

不确定这是您要实现的目标吗?


<?php

if(isset($_POST['submit'])){

    $str = $_POST['hiddenVal'];

  echo "My strtest file is : " . $str . " ";  
  echo  " ";
  $MyTexkFile =  "<html><head></head><div> <p> This is my html text display </p></div id = \'mydiv\'><body><body></html>";

  echo "The string is " . " " . $MyTexkFile . " ";    
  $newstr = $str . " Add some text to str";

}
?>
<script>
function phpadd(a) {
    document.getElementById("hiddenVal").value = a;
}
</script>
<form action="" method="post">
<input type="text" name="hiddenVal" style="display:none" id="hiddenVal" />
<button name="submit" onclick='phpadd("4567.txt")'>add</button>
</form>
相关问题