[英]how to store php variable passed as id to javascript in a javascript variable
I am trying to pass div id
to javascript and print it out on console as :我正在尝试将 div id
传递给 javascript 并在控制台上将其打印为:
<?php echo 67;?>
The 67 is id
here. 67 是这里的id
。 I was wondering how can I store this value in my javascript variable storeid
as var storeid = 67
?我想知道如何将此值存储在我的 javascript 变量storeid
为var storeid = 67
?
My code:我的代码:
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>";
Also, I want to use that id
value to update the values in my sql.另外,我想使用该id
值来更新我的 sql 中的值。 I can only test my code once I could somehow get the value of that id.我只能在以某种方式获得该 id 的值后测试我的代码。 How can I store the value of the id
to javascript variable?如何将id
的值存储到 javascript 变量?
My code (This is to update sql once I get the id) - Not sure if it's right I can test it after getting id
value:我的代码(这是在获得 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();
}
}
this way:这边走:
echo '<script>
var storeid = '. $id .'
</script>';
more readable way:更易读的方式:
// 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.