Jump to content

PHP variables populate to HTML


cjbeck71081

Recommended Posts

I am a new to PHP and i think my question is basic, but i am having no luck finding the answer.

I have an sql database on my website with data populate for fields UserID, FirstName, LastName, and Email
[b]
I created an HTML page called "userid.html" with this code:[/b]

[color=red][font=Verdana][font=Verdana]</head>

<body>

<form method="post" action="http://epicri.com/info.php">


<p align="center">&nbsp;</p>
<p align="center">&nbsp;</p>
<p align="center">&nbsp;</p>
<form id="form1" name="form1" method="post" action="">
  <label>
  <div align="center"><span class="style1">Please Enter User ID: </span><br />
    <br />
    <input name="userid" type="text" size="10" maxlength="5" />
    <br />
    <br />
    <input type="submit" name="Go" value="Submit" />
  </div>
  </label>
</form>
<p align="center">&nbsp;</p>
</body>
</html>[/color]

[b]that sent the value of the USERID entered into a textbox to a PHP script named "info.php" that then query'd the database for the data that looked like this:[/b]

[color=green]</head>

<body>

<?php
$hostname = "mysql.epicri.com";
$username = "epicr001";
$password = "xxxxx";
$usertable = "persontab";
$dbName = "epicr001"; //
$word = $_POST["userid"]


?>

<?php
$con = mysql_connect($hostname,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($dbName, $con);

$result = mysql_query("SELECT firstname,lastname,email,userid FROM $usertable
WHERE userid='$word'");

while($row = mysql_fetch_array($result))

  {
  echo $row['firstname'] . " " . $row['lastname'];
  echo "<br />";
  }


?>

</body>[/color]


What i would like to do is now populate an HTML page with the results of the Query without using the "echo" code. For example after the information is sent to the PHP the resulting HTML page would look like:


[color=blue][b]John Smith[/b][/color]
UserID: [color=purple]12221[/color]
First Name: [color=purple]John[/color]
Last Name: [color=purple]Smith[/color]
Email Address: [color=purple]john@epicri.com[/color]

I am almost sure my problem is the syntaxt of passing PHP Variables for use in an HTML form, but i dont know

Any help is apprecaited
Thank You
Chris


Link to comment
Share on other sites

Okay use this:
[code]$htmlCode = <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Unititled Document</title>
</head>
<body>

HTML;

$row = mysql_fetch_array($result);

$name = $row['firstname'] . '_' . $row['lastname'];

$htmlCode .= <<<HTML
<b>{$name}</b><br />
<b>User ID:</b> {$row['userid']}<br />
<b>First Name:</b> {$row['firstname']}<br />
<b>Last Name:</b> {$row['Lastname']}<br />
<b>Email Address:</b> {$row['email']}

</body>
</html>
HTML;

// create the HTML File
// the html file will be name:
// fistname_lastname.html
$htmlFile = fopen($name . '.html', "w+");

// write to the html file
fwrite($htmlFile, $htmlCode, strlen($htmlCode));

// now we close the file
fclose($htmlFile);

echo "HTML file created!<br />\n<br />\n<a href=\"{$name}.html\">{$name}.html</a>";[/code]

instead of the following:
[code]while($row = mysql_fetch_array($result))

  {
  echo $row['firstname'] . " " . $row['lastname'];
  echo "
";
  }[/code]

NOTE: This code is untested, so it may return errors.
Link to comment
Share on other sites

You can write any html code you like. When it comes to to put something in that comes from the database you can use this

[code] <?=$row['firstname']?>[/code]

So in html it will look like this

[code]<td class=someclass align=center>First Name: <?=$row['firstname']?></td>[/code]

or the other way around. start your table above your while statement then echo out the html code with the database results. You should start the table above the while statement that way you can loop thru each row starting with <tr> otherwise you will start a new table on every loop
[code]echo "<table>"
while($row = mysql_fetch_array($result))  {
  echo "<tr>
<td>".$row['firstname']."</td><td>" . $row['lastname']."</td>
</tr>";
  }
echo "</table>"[/code]

It that what you are looking for???

Ray
Link to comment
Share on other sites

Thank you both for your assistance.

The output from query worked great!.

Ultimately what i was looking for however was the second answer regarding the <td><?=$row['firstname']?></td> code.

I realise I made a mistake in how i asked the question, but both responses were a huge help.

I have been reading information on code for some 100 hours in the last 2 weeks ultimately looking for this information... i shoudlve came here in the first place.

Thank You again guys
Chris
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.