[英]How do I use a js variable in a echo php function call?
我的实际应用是从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:''literal在脚本结束之前没有终止jsToPhpFunction.php:15:97。 为什么是这样? 我可以做任何事情来让变量通过吗?
不确定这是否是你想要实现的目标?
<?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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.