My PHP needs some style !!

A

Anonymous

Guest
Hi folks,

Hopefully this should be quite straightforward.
I am using a php script to write some html form data to MySQL in the event that a submit button is pressed:
Code:
if (isset($_POST['Submit']))
Initially I check the connection to MySQL and get an echo back to inform the user of any errors/success:
Code:
$con=mysqli_connect("localhost","u_name","password");
if(mysqli_connect_errno()){
echo "Failed to connect to MySQL: ".mysqli_connect_error();}
else{
echo "Connection to MySQL via 'instlab' established.";}
At the moment the feedback text just prints at the top of the html user interface (form), however, I would like to position it within a predefined area on the page if possible.
I tried linking a .css script file that I use as my html stylesheet using:
Code:
<link rel="stylesheet" href="Style.css">
but this does not seem to work. I think I understand that php is not seen by the browser which would maybe be a reason this is not working.
Any advice would be appreciated :) .
 
Hi,

1) You probably want type="text/css" in your css link e.g.
Code:
<link rel="stylesheet" href="Style.css" type="text/css">

However, this is unlikely to be your problem. You don't show where the php is being executed in relation to your html. If it is executed BEFORE any html is output, then the echo message will appear above the <html> tag and may look like it is at the 'top' of the screen. You can use 'view source' (firefox: cntl-U) to see where your output is in relation to your html.

No amount of css is going to change that!

I suggest:
1) For fatal errors (like you db connection), use die() to terminate output as there is not much point carrying on after printing the message. I like to add a unique error number to help user's report the problem. You could even write your own my_fatal() function to format the msg if you want

2) For non-fatal msg e.g. 'connection established', I suggest you concatenate the messages into a variable and then print it inside a suitable html container e.g.

Code:
<?php
$gsAlert = ''; // Init
$con=mysqli_connect("localhost","u_name","password");
if(mysqli_connect_errno()){
  // Fatal error
  die ("Error 19201 - Failed to connect to MySQL: ".mysqli_connect_error());
} else {
  // Add to alert msg. Note trailing space for nice formatting.
  $gsAlert .= "Connection to MySQL via 'instlab' established. "; 
}
?>
<html> ...
<body> ...
  <div id="alert">
    <?php print $gsAlert; // print alert msgs ?>
  </div>
...
</body> ...
 
Wow, I didn't see your post is 2 days old PLUS 1 YEAR. You've probably either solved this problem or retired and taken up fishing. Oh well...
 
Back
Top