一、php传数据给js:
背景:我在Php中有个变量$id,我想要传到js中,在js中通过ajax传到别的php中进行处理。
<div class="q_area">
<input type="text" name="question" id="myquestion" placeholder="请输入您的问题,小伙伴们将竭尽全力为您解答!">
<button class="publish" id="surepublish" landid=<?php echo $id;?> >发表</button>
</div>
①于是我把$id 寄放在button标签的属性中
<button class="publish" id="surepublish" landid=<?php echo $id;?> >发表</button>
②在js中取出
<script>
var surepublish=document.getElementById('surepublish');
var landid = surepublish.getAttribute('landid');
</script>
var landid = surepublish.getAttribute('landid');
二、简单方法
<?php
if(isset($uid)){
$sql="SELECT * FROM user_form WHERE userid={$uid}";
$res=mysqli_query($link,$sql);
$row = mysqli_fetch_assoc($res);
}
?>
var touxiang=document.getElementById('touxiang');
var username=document.getElementById('username');
var userinfo=document.getElementById('userinfo');
if(isuser){
username.innerText="<?php echo $row['username']?>";
touxiang.src="<?php echo $row['portrait']?>";
userinfo.innerText="<?php echo $row['briefinfo']?>";
}
username.innerText="<?php echo $row['username']?>";
touxiang.src="<?php echo $row['portrait']?>";
userinfo.innerText="<?php echo $row['briefinfo']?>";直接在变量后面(等号右边)写php echo.