[英]how to store php variable passed as id to javascript in a javascript variable
我正在嘗試將 div id
傳遞給 javascript 並在控制台上將其打印為:
<?php echo 67;?>
67 是這里的id
。 我想知道如何將此值存儲在我的 javascript 變量storeid
為var storeid = 67
?
我的代碼:
echo "<h2 class='editme' id='<?php echo $id;?>' contentEditable='true'> Hello</h2>";
echo "<script>
$('h2.editme').click(function(){
console.log(this.id);
var storeid;
});
</script>";
另外,我想使用該id
值來更新我的 sql 中的值。 我只能在以某種方式獲得該 id 的值后測試我的代碼。 如何將id
的值存儲到 javascript 變量?
我的代碼(這是在獲得 id 后更新 sql)-不確定是否正確我可以在獲得id
值后對其進行測試:
echo "<h2 class='editme' id='<?php echo $id;?>' contentEditable='true'> Hello</h2>";
echo "<script>
$('h2.editme').click(function(){
var content2 = $(this).html();
console.log(this.id);
var storeid;//once I get the id
$.ajax({
url: 'updateDescription(storeid, content2)',
success: function(respon) {
$('.editme').html(respon);
}
});
});
</script>";
function updateDescription($user_unique_id, $content2)
{
try {
require "testing.php";
$sql = "UPDATE icecream SET
desc = :desc
WHERE id = :id";
$stmt->bindParam(':desc', $content2, PDO::PARAM_STR);
$stmt->bindParam(':id', $user_unique_id);
$stmt->execute();
echo 'Record updated';
} catch (PDOException $e) {
echo 'Connection failed ' . $e->getMessage();
}
}
這邊走:
echo '<script>
var storeid = '. $id .'
</script>';
更易讀的方式:
// somefile.php
<?php
$id = 67;
?>
<h2 class='editme' id='<?php echo $id;?>' contentEditable='true'> Hello</h2>
<script>
var storeid = <?php echo $id; ?>
// rest of javascript code
</script>
<?php
// rest of the php code
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.