我想弄清楚如何使用這些來自 mysql 數(shù)據(jù)庫的 php 變量并發(fā)送電子郵件。當用戶點擊請求按鈕時,它會轉到 myFunction()test.php 文件: $x = db_query('SELECT * FROM test_site_database.names'); foreach($xx as $item) { if($item) { if ($item->name == "Paul"){ $dname = $item->name; $state = $item->state; $city = $item->city; } } echo '<tr>'; echo '<td>' . $dname . '</td>'; //prints Paul echo '<td>' . $state . '</td>'; //prints CA echo '<td>' . $city . ' </td>'; //prints LA echo '<td><button onclick="myFunction()">Request</button></td>'; global $y; $y=dname; <script> function myFunction() { <?php global $user; $to = "myemail@gmail.com"; $subject = $user->name; $txt = $y; // $headers = "From: test@email.com"; mail($to,$subject,$txt,$headers); ?> } </script>所以在我點擊請求后收到的電子郵件中是用戶名和 $dname(那個人的全名)但是我的數(shù)據(jù)庫中有 10 個 Paul,我只從數(shù)據(jù)庫中最后插入的信息中獲取信息,但它是一個包含 10 個的表保羅和我點擊請求讓我們說第 3 行我想通過電子郵件發(fā)送該數(shù)據(jù),而不是數(shù)據(jù)庫中的最后一個保羅。
2 回答

溫溫醬
TA貢獻1752條經(jīng)驗 獲得超4個贊
解決方法:
function myFunction(str) {
document.getElementById("demo").innerHTML = str; //now I get the correct data inside str from $dname
我像這樣傳遞了 php 變量:
echo '<td><button onclick="myFunction(' . $dname . ')">Request</button></td>';
這樣你就可以開始使用所有正確的數(shù)據(jù),無論你需要在函數(shù)中做什么。
- 2 回答
- 0 關注
- 619 瀏覽
添加回答
舉報
0/150
提交
取消