0

I have a php code i.e

<?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.

for eg:My javascript code

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

<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. I took the value of variables(tablink and tabtitle) and assigned them id's like (lol and card) into HTML.

I want to do same with my PHP variable ($row['name']) and assign it id 'lol2'. How should I do it?

shaswat kumar
  • 309
  • 8
  • 18

1 Answers1

0

If I understand correctly, you want to put the value of your variable $row['name'] into an input?

If so, you can output it in the value attribute of an input using echo:

<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.

kzhao14
  • 2,330
  • 12
  • 20
  • yes, but I have tried what you suggested. When I run the code suggested by you, I dont get the output (i.e value of $row) instead that whole echo $row['name'] gets printed as it is.(I think may be because I saved it as an html file) I will only have lol2 and no other variables like lol3 and lol4... . And this html file is actually a part of my chrome extension and the php file is stored elsewhere in htdocs – shaswat kumar Mar 07 '19 at 14:35
  • @shaswatkumar Do you have PHP enabled in your file? Are you using a `.php` file? This does work for php files. – kzhao14 Mar 07 '19 at 14:37
  • No the html file is stored in my chrome extension folder and php file in htdocs, in short I want data from my (php file i.e in xampp-htdocs folder) to (html file in my chrome extension folder) – shaswat kumar Mar 07 '19 at 14:41
  • @shaswatkumar ah, okay. How are you linking your php file to your html file? – kzhao14 Mar 07 '19 at 14:44
  • using xampp localhost – shaswat kumar Mar 07 '19 at 14:47
  • @shaswatkumar I mean how does your html file receive the data? – kzhao14 Mar 07 '19 at 14:50
  • I am connecting my html file (chrome extension file) to php file (xampp-htdocs file) through (
    ) and retrieve.php file communicates with my sql table at localhost. So I can bring data from SQL table to retrieve.php. But I am not getting how to bring it to html from retrieve.php
    – shaswat kumar Mar 07 '19 at 15:15
  • @shaswatkumar Okay. What you can do to retrieve data from PHP to HTML is use ajax. Take a look at this: https://stackoverflow.com/questions/16707648/using-jquery-ajax-to-retrieve-data-from-mysql – kzhao14 Mar 07 '19 at 15:29