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