简体   繁体   English

从PHP提取数据到HTML并为其分配ID

[英]Fetching data into HTML from PHP and assigning id to it

I have a php code ie 我有一个PHP代码,即

<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "uppercase"; 
$con = mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName) or 
die(mysqli_error());      
mysqli_select_db($con,$dbName);
$sql = "SELECT * FROM urltable ;";
$result = mysqli_query($con,$sql);
$resultCheck = mysqli_num_rows($result);    
if ($resultCheck > 0 ) {
while ($row = mysqli_fetch_assoc($result)){
$row['name'];
echo $row['url'];
}
}
mysqli_close($con);
?>

What I want to do is take the data stored in ($row['name']) and store it into HTML and assign some id to it. 我想做的是获取存储在($ row ['name'])中的数据,并将其存储到HTML中并为其分配一些ID。

for eg:My javascript code 例如:我的JavaScript代码

chrome.tabs.getSelected(null,function(tab) {
var tablink = tab.url;
var tabtitle = tab.title;
document.getElementById("lol").value=tablink;
document.getElementById("card").value=tabtitle;
});

HTML CODE HTML代码

<input type="hidden" id="lol" name="lol" value="" />
<input type="hidden" id="card" name="card" value="" />   
First Name: <input type="text" name="name1">
<input type="Submit">
</form>
<form action="http://localhost/retrieve.php" method="post">
<input type="text" id="lol2" name="lol2" value="" />

<input type="Submit">
</form>

This is a part of my javascript and HTML code. 这是我的JavaScript和HTML代码的一部分。 I took the value of variables(tablink and tabtitle) and assigned them id's like (lol and card) into HTML. 我取了变量(tablink和tabtitle)的值,并将它们的ID像(大声笑和卡)分配给HTML。

I want to do same with my PHP variable ($row['name']) and assign it id 'lol2'. 我想对我的PHP变量($ row ['name'])进行同样的操作,并将其分配为ID'lol2'。 How should I do it? 我该怎么办?

If I understand correctly, you want to put the value of your variable $row['name'] into an input? 如果我理解正确,您想将变量$row['name']放入输入中吗?

If so, you can output it in the value attribute of an input using echo : 如果是这样,则可以使用echo将其输出到输入的value属性中:

<input type="hidden" id="lol2" name="lol2" value="<? echo $row['name']; ?>" /> 

Assuming you may have more than one row, you can add a counter that increments after every while loop creating lol2,lol3,lol4... , and move the loop into the body of the form that creates an input with every loop. 假设您可能有多于一行,则可以添加一个计数器,该计数器在每次while循环创建lol2,lol3,lol4... ,然后将循环移动到使用每个循环创建input的表单主体中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM