簡體   English   中英

如何在echo php函數調用中使用js變量?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM