i'm working on a form in php mysql. so in my page there is a form and below that i shown the data of that form in table format.now i want to print only the table structure thats why i just made a PRINT button as:
<span style="float:right;">PRINT</span>
but it will print the whole page with form field and table and i just want the tableto print.
Here is the design of my whole page with form and table:
<html>
<head></head>
<body>
<div class="container">
<form class="form-horizontal" action="" method="post" name="userform1" id="company-form" enctype="multipart/form-data">
<?php if($_GET[id]){?>
<fieldset>
<legend>Add Company</legend>
<div class="control-group">
<label class="control-label">Company Name</label>
<div class="controls">
<input type="text" name="company" id="company" value="<?php echo $selup['company']?>">
</div>
</div>
<div class="control-group">another field</div>
<div class="control-group">another field</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn" name="submit" id="submit" value="Submit">Submit</button>
</div>
</div>
<table class="table table-bordered">
<tbody>
<tr>
<td>S.No.</td>
<td>Company Name</td>
<td>Type</td>
<td>Action</td>
</tr>
<?php
// to print the records
$select = "select * from company where type='Miscellaneous'";
$query1 = mysql_query($select);
while($value = mysql_fetch_array($query1)){ ?>
<tr>
<td><?php echo $value[id];?></td>
<td><?php echo $value[company ];?></td>
<td><?php echo $value[type];?></td>
<!--<td> </td>-->
<?php /*?><td><?php echo $value[amount];?></td>
<td><?php echo $value[date];?></td><?php */?>
<td><i class="icon-edit"></i>
<i class="icon-trash"></i></td>
</tr><?php }?>
</tbody>
</table>
</fieldset>
<form>
</div>
</body>
so i just want to print the table not whole page.
Use css to hide elements you don't want to print:
#media print {
.control-group {
display: none;
}
}
function printContent(el){
var restorepage = document.body.innerHTML;
var printcontent = document.getElementById(el).innerHTML;
document.body.innerHTML = printcontent;
window.print();
document.body.innerHTML = restorepage;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>My page</h1>
<div id="div1">DIV 1 content...</div>
<button onclick="printContent('div1')">Print Content</button>
<div id="div2">DIV 2 content...</div>
<button onclick="printContent('div2')">Print Content</button>
<p id="p1">Paragraph 1 content...</p>
<button onclick="printContent('p1')">Print Content</button>
</body>
</html>
You could make a print-css (which does the same as #media print, but I think it is cleaner, and you can disable the css include via javascript, if you need it):
<link rel="stylesheet" type="text/css" href="print.css" media="print" />
In that css, you hide all elements which should not get printed, for example:
.wrapper, .header {display: none;}
One posible solution, perhaps not the best option:
1. Open a new window with JS
2. Copy the whole table into the new window (with jQuery for example)
3. Print the new window
4. Close the window
Sure it has a blink effect but It will work.
Related
I am working on a website where I am getting input of semester number from the user and I want to display the subjects in the semester they entered accordingly.
For this, I planned to create tables for each semester which would have the subjects in it.
For now, I am successful to retrieve data from one table. But I am required to get subjects as per the entered semester by the student.
Following is the code,which works perfectly for semester 5. Please suggest alterations as per the requirements asked above.
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- very important -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="subject.css">
</head>
<body>
<h1 class="text-center text-capitalize font-weight-bold">Subject Information</h1>
<div class="container">
<div>
<br>
<div>
<form class="form-inline">
<!-- <div class="mx-auto" style="width: 200px;"> -->
<div class="form-group">
<label for="Sem"> Semester: </label>
<div>
<input type="text" name="sem_no" placeholder="Enter current semester" id=sem class="form-control">
</div>
<br>
</div>
</form>
</div>
</div>
</div>
<div>
<!-- TABLE DISPLAYED WITH DATA FROM RESPECTIVE DATABASE -->
<div class= "container">
<div class="col-lg-12 ">
<br><br>
<h2 class="text-center">Subjects</h2>
<table class="table table-striped table-hover table-bordered">
<tr>
<th>Subject code</th>
<th>Subject Name</th>
<th>Faculty name</th>
</tr>
<?php
$con = mysqli_connect('localhost','root');
mysqli_select_db($con,'subjects');
$q= "select * from sem5";
$query = mysqli_query($con,$q);
while($res = mysqli_fetch_array($query))
{
?>
<tr>
<td><?php echo $res['subject_no']; ?></td>
<td><?php echo $res['subject_name']; ?></td>
<td><?php echo $res['faculty_name']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</body>
</html>
Please suggest alterations. Will be highly obliged.
You need to do a HTTP GET request and use PHP to get the value. Then use PHP to make the SQL query. Here it is in its simplest form.
I've added a submit button which submits the form and executes the HTTP GET with the parameters attached to the URL. You will see ?sem_no=x after it is submitted. That's the Query String.
...
...
<div class="form-group">
<label for="Sem"> Semester: </label>
<div>
<input type="text" name="sem_no" placeholder="Enter current semester" id=sem class="form-control">
</div>
<div>
<input type="submit"/>
</div>
<br>
...
...
...
...
//check the value was submitted and it is not blank
<?php if(isset($_GET['sem_no']) && !empty($_GET['sem_no'])) { ?>
<!-- TABLE DISPLAYED WITH DATA FROM RESPECTIVE DATABASE -->
<div class= "container">
<div class="col-lg-12 ">
<br><br>
<h2 class="text-center">Subjects</h2>
<table class="table table-striped table-hover table-bordered">
<tr>
<th>Subject code</th>
<th>Subject Name</th>
<th>Faculty name</th>
</tr>
<?php
$con = mysqli_connect('localhost','root');
mysqli_select_db($con,'subjects');
$q= 'select * from sem'.$_GET['sem_no'];
$query = mysqli_query($con,$q);
while($res = mysqli_fetch_array($query))
{
?>
<tr>
<td><?php echo $res['subject_no']; ?></td>
<td><?php echo $res['subject_name']; ?></td>
<td><?php echo $res['faculty_name']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
<?php
//closing if
} ?>
...
...
As mentioned in the comments earlier, this database design is quite poor. I know you are learning though. Have you considered having a subjects table that has a Semester column? Then you could do a query like:
SELECT * FROM subjects where semester_number = x
There are even better ways to do it beyond that but this would be a good starting point for you.
The button with in my code will not change at all, i have tried listing all the divs and this still has not worked
This is the code for the page, php:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<!-- edit the refrence to show them the different options -->
<link rel="stylesheet" type="text/css" href="IndexopOr.css">
<!--<link rel="stylesheet" type="text/css" href="css/styler.php">-->
<title>Wccs Canteen</title>
</head>
<body>
<h1 class="Top_Text">WCCS Lunch Order</h1>
<div id="Menu-fixer1">
<div id="Menu_fin1">
<img src="WCCS Logo.png" id="lighthouse">
</div>
</div>
<div id="Menu-fixer">
<div id="Menu_fin">
<table>
<tr>
<td>
<!-- this will change when the css source is changed between Indexop1 and Indexop2, currently it is linked to Index, the file i will change when we have decided what we like -->
<span>Menu </span>
</td>
</tr>
<tr>
<td>
<span>Order </span>
</td>
</tr>
<tr>
<td>
<span>Contact Us </span>
</td>
</tr>
</table>
</div>
</div>
<div id="menu_bar">
<div id="menu_user">
<style type="text/css">
#menu_user {
position: absolute;
right: 10px;
bottom:20%;
font-size: 24px;
color: white;
font-family: HelvLight, verdana;
}
</style>
<?php
//seeing as you like documentation sir i added this in telling you about sessions, remind me to tell you if you want it explained
if (isset($_SESSION['food'])) {
echo 'Student: '.$_SESSION['food'];
echo $_SESSION['ln'];
} else {
echo "You are not logged in!";
}
?>
</div>
<div id="menubar_bottom">
<h1 class="menu_Hpg">Menu</h1>
</div>
</div>
<div id="list_space">
<div id="Right_list">
</div>
<div id="Left_orders">
<button id="but">Confirm</button>
</div>
<!--<div id="Bar"></div>-->
<div id="mid_bar"></div>
</div>
</body>
</html>
Then the css code:
#but button {
color:blue;
}
there is alot of css that is the part editing the button, please tell me why it doesn't work. It works when i just type style next to it and add it but it doesn't allow me to put a hover option on there.
#but button means that you are looking for button inside element with id but. You do not have such situation in your code. You have button with id but so it must be like this:
button#but
change '#but button' to 'button#but' in your css, because button has the id not its parent element
button#but {
color:blue;
}
try this selector, it select button that having attribute id=but
button#but
If you want to use multiple buttons, you must use classes and not id's. The id's can be used only one per page.
in any case, the syntax is correct button#but{} or #but{}.
how do i echo a value from php to html in a division (div2--a div i created) of a paragraph(p).
for example: i have a value called $greetings and i assign a value of "Welcome" to it
$greetings = "Welcome";
I tried:
`<p>
<?php
$index = "WELCOME";
echo "<div2>$index</div2>";
?>
</p>`
but it doesn't display the value of $greetings--welcome
instead it displays ( $index"; ?> )
HERES MY ENTIRE CODE:
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Tech Planet</title>
<link rel="stylesheet" type="text/css" href="style.css">
<img src="logo.gif" alt="logo" width="150" height="50">
<div class ='seachAndProducts'><form action="header.php" method="GET">
<input id="search" type="text" placeholder="Search Tech Planet...">
<input id="submit" type="submit" value="Search">
</form></div>
<div class = 'navAndFooter'><ul>
<li><b>Home</b></li>
<li><b>Products</b></li>
<li><b>About</b></li>
<li><b>Contact us</b></li>
</ul></div>
<img src="cart.jpg" alt="cart" width="40" height="40">
</head>
<body>
<h3><img src="WelcometoTechPlanet.gif" width="250" height="40"></h3>
<h5><div class ='ourAndFeatProductsHeader'>Our Products</div>
<div class='seachAndProducts'>
<div class='ourProductsList'>Audio Systems</div>
<div class='ourProductsList'>Cameras</div>
<div class='ourProductsList'>Laptops</div>
<div class='ourProductsList'>Memory Cards</div>
<div class='ourProductsList'>Monitors</div>
<div class='ourProductsList'>Phones</div>
<div class='ourProductsList'>Televisions</div>
<div class='ourProductsList'>USBs</div>
<div class='ourProductsList'>All Products</div>
</div><div class ='ourAndFeatProductsHeader'>Featured Products</div>
<div2><img src="8wb50.gif" alt="" width="250" height="200"></div2></h5>
<p><?php
$index = "WELCOME";
echo "<h1>$index</h1>";
?></p>
<!-- used © because © displays © -->
<div class = 'navAndFooter'>© Tech Planet. All Rights Reserved.</div>
</body>
</html>
Here are a few things to note...
Don't put an h1 tag in the p tag; and you are echoing $index, not $greetings.
Try this
<?php
// Pick the one you want.
$greetings = "Welcome";
$index = "WELCOME";
// Pick the one you want
echo "<h1>$index</h1>";
echo "<h1>$greetings</h1>";
?>
Then, make sure you save it as a .php file, and that you are on a web server with php enabled.
I need a code that displays the IP of the person/guest that commented on my website.
Here's the index page;
<?php
include "core/init.php";
?>
<!DOCTYPE html>
<link rel="stylesheet" type="text/css" href="http://hjalplista.comxa.com/daniel_emelie/style.Css">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Välkommen till hjälplistan!</title>
<body>
<div id="header">
<div align="right"><img src="http://i.imgur.com/y0aiNIt.jpg"></div>
</div>
<div id="inside">
<center>
<div id="location"><a href='index.php'>Hem</a> - <a href='administrative/adminonlyaccess.php'><i>Admin</i></a><br><br>
</div>
<hr/>
<br>
<div id="text">
<br>
<table border="1" cellpadding="1" cellspacing="1" style="width: 100%;">
<tbody>
<tr>
<td><span style="color:slategray;"><strong><center>Nr</center></strong></span></td>
<td><span style="color:slategray;"><strong><s><center>Fått hjälp</center></s></strong></span></td>
<td><span style="color:slategray;"><strong><center>Namn</center></strong></span></td>
<td><span style="color:slategray;"><strong><center>Uppgift</center></strong></span></td>
<td><span style="color:slategray;"><strong><center>IP-Adress</center></strong></span></td>
</tr>
<?php
include "core/inc/conn.php";
mysql_select_db("comments");
$getquery = mysql_query("SELECT * FROM comments ORDER BY id DESC");
while($rows = mysql_fetch_assoc($getquery))
{
$id = $rows['id'];
$comment_name = $rows['name'];
$comment = $rows['comment'];
echo"<tr><th><font color='lightgray'>$id</font></th>";
echo"<th><font color='red'>Fungerar ej!</font></th>";
echo "<th><u><font color='black'>$comment_name</font></u></th>";
echo"<th><font face='black'>$comment</font></th>";
echo"<th></th></tr>";
}
if(isset($_GET['error']))
{
echo "<p>15 Bokstäver max!";
}
?>
</table>
<br><br>
</body>
<head>
<center>
</div>
<br><br>
<form action="post_comment.php" method="post">
<label> Namn: <input type="text" name="name"></label><br><br>
<label> Uppgift: <input type="text" name="comment"></label><br><br>
<input type="submit" name="post" value="Skicka">
</form>
<br><br>
<br><br>
<?php
echo "<br><br><br>";
echo "<hr/>";
echo "<p> © Wieselgrenskolan. All rights reserved. </p>";
?>
</head>
</html>
<html>
<div align="right">
<!-- START OF HIT COUNTER CODE -->
<br><script language="JavaScript" src="http://www.counter160.com/js.js?img=11"></script><br><img src="http://www.counter160.com/images/11/left.png" alt="Free web hosting" border="0" align="textright"><img alt="Web hosting" src="http://www.counter160.com/images/11/right.png" border="0" align="texttop">
<!-- END OF HIT COUNTER CODE -->
</div>
</html>
And it's supposed to be in the echo "<th></th></tr>";. Do I need a MySQL table for that?
I've created a index in mysql called ''commentip'' but I don't know how to fix the missing parts.
You can get the IP in php in most cases using $_SERVER['REMOTE_ADDR'] However if they are using a proxy or you are using a reverse proxy like cloudflare or incapsula then you can use $_SERVER['HTTP_X_FORWARDED_FOR'].
So, my formular is redirecting to the wrong site!
Look at my example:
ATM I've got the internet address:
http://localhost/myworkspace/mywebsite/index.php
It's hosted on a localhost-xampp server. It's homepage is:
http://localhost/xampp/
Ok ... Now I tried several things to get into the file where the script is that is working with the data of the formular:
<form action="index.php">
-> I'm getting redirected to the Xampp Homepage
<form action="index.php?mod=home">
-> I'm getting redirected to the Xampp Homepage
<form action="myworkspace/mywebsite/index.php">
-> I'm getting redirected on "http://localhost/myworkspace/mywebsite/myworkspace/mywebsite/index.php"
(the arrows express what happens when clicking on "send")
Why is this happening? As seen in the examples, the browser is not moving outside the myworkspace/mywebsite-folders. But still, it loads the index.php which is located in localhost/xampp (it's localhost/index.php). If you see my original link of the website, there actually is an index.php file in "myworkspace/mywebsite".
Here's some code of me:
the addguestbook.php, which is included into the index.php (see below)
<?php
//******************************************************//
//********************Database stuff********************//
//******************************************************//
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="mywebsite"; // Database name
//********************Tables***************************//
$tbl_name="guestbook"; // Guestbook
echo $hostname.$path.$get;
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$comment = $_POST['comment'];
$datetime = date("l, jS M Y, g:i a"); //date time
// Connect to server and select database.
mysql_connect($host, $username, $password)or die("cannot connect server: ".mysql_error());
mysql_select_db($db_name)or die("cannot select DB: ".mysql_error());
$sql="INSERT INTO ".$tbl_name."(id, name, email, website, comment, datetime)VALUES('".$post_id."', '".$name."', '".$email."', '".$website."', '".$comment."', '".$datetime."')";
$result=mysql_query($sql);
mysql_close();
} else
?>
<form method="post" action="myworkspace/mywebsite/index.php">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="guestbookFormCell" colspan='2'><input
class="guestbookInputFieldText" name="name" type="text"
value="Name *" size="40" maxlength="30" /></td>
</tr>
<tr>
<td class="guestbookFormCell" colspan='2'><input
class="guestbookInputFieldText" name="email" type="text"
value="E-Mail (won't become displayed)" size="40" maxlength="40" />
</td>
</tr>
<tr>
<td class="guestbookFormCell" colspan='2'><input
class="guestbookInputFieldText" name="website" type="text"
value="Website" size="40" maxlength="50" /></td>
</tr>
<tr>
<td class="guestbookFormCell" colspan='2'><textarea
class="guestbookInputFieldText" name="comment" cols="37" rows="5">Comment *</textarea>
</td>
</tr>
<!--
<tr>
<td>CAPTCHA</td>
</tr>
-->
<tr>
<td><button class="guestbookFormCell guestbookButton" type="submit"
name="submit">
<span class='guestbookButtonText'>Send</span>
</button></td>
<td><button class="guestbookFormCell guestbookButton" style="float:right;"type="reset"
name="reset">
<span class='guestbookButtonText'>Reset</span>
</button></td>
</tr>
</table>
</form>
and the index.php (it's pretty big. most of it is just a design-structure, so ignore the html stuff. it's just divs and tables.
<html>
<head>
<title>JustBasti's website</title>
<script type="text/javascript" src="lightbox/js/prototype.js"></script>
<script type="text/javascript"
src="lightbox/js/scriptaculous.js?load=effects,builder"></script>
<script type="text/javascript" src="lightbox/js/lightbox.js"></script>
<link rel="stylesheet" href="lightbox/css/lightbox.css" type="text/css"
media="screen" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php
session_start();
// Path-parameters for link-building
$hostname = $_SERVER['HTTP_HOST'];
$path = $_SERVER['PHP_SELF'];
$_SESSION['basepath'] = dirname($_SERVER['PHP_SELF']);
?>
<div id='top'>
<div id='header'>
<div id='headerText' class='lightText text bigText'>
<div>
Hello, My name is <font class='blueText'>Sebastian Fast</font>
</div>
<div>
I just finished my <font class='blueText'>education in IT</font>
</div>
<div>
and am now <font class='blueText'>aiming to</font>
</div>
<div>
experience the <font class='blueText'>most wonderful places</font>
</div>
<div>
around <font class='blueText'>the world</font>
</div>
<div>
Read more about myself <font class='blueText'>here</font>
</div>
</div>
<div id='headerLineBox'>
<div class='lineTopDiv'>
<div id='headerTopLine' class='lineTopEnd'></div>
</div>
<div class='verticalLine'>
<div id='headerLine' class='line'></div>
</div>
<div class='lineBottomDiv'>
<div class='lineBottomEnd'></div>
</div>
</div>
<div id='headerNavigation' class='lightText text bigText'>
<div>
<a href='index.php?mod=home' class='lightLink'>Home</a>
</div>
<div>
<a href='index.php?mod=news' class='lightLink'>News</a>
</div>
<div>
<a href='index.php?mod=countries' class='lightLink'>Countries</a>
</div>
<div>
<a href='index.php?mod=gallery' class='lightLink'>Gallery</a>
</div>
</div>
</div>
</div>
<div id='middle'>
<div id='content'>
<table>
<!-- CONTENT -->
<?
// Paths
echo " <a href='index.php?mod=home'>Home</a>
<a href='index.php?mod=news'>News</a>
<a href='index.php?mod=allnews'>All News</a>
<a href='index.php?mod=countries'>Countries</a>
<a href='index.php?mod=gallery'>Gallery</a>
<a href='index.php?mod=guestbook'>Guestbook</a>
<a href='index.php?mod=admin'>Administrator</a>";
if (!isset($_SESSION['angemeldet']) || !$_SESSION['angemeldet']) {
include('login/login.php');
}
// Check which link got clicked & import data
if (!$_GET){
include('data/home.php');
} elseif (isset($_GET['mod']) && !isset($_GET['post']) && !isset($_GET['album']) && !isset($_GET['country'])){
Switch($_GET['mod']){
case 'home':
include('data/home.php');
exit;
case 'news':
include('data/latestPosts.php');
exit;
case 'allnews':
include('data/allPosts.php');
exit;
case 'countries':
include('data/viewCountries.php');
exit;
case 'gallery':
include('data/gallery.php');
exit;
case 'admin':
include('admin/admin.php');
exit;
}
} elseif (isset($_GET['mod']) && isset($_GET['post'])){
$get = "?mod=".$_GET['mod']."&post=".$_GET['post'];
$post_id = $_GET['post'];
include('data/viewPost.php');
include('data/guestbook.php');
} elseif (isset($_GET['mod']) && isset($_GET['album'])){
$get = "?mod=".$_GET['mod']."&album=".$_GET['album'];
$post_id = $_GET['album'];
include('data/viewAlbum.php');
include('data/guestbook.php');
} elseif (isset($_GET['mod']) && isset($_GET['country'])){
$country = $_GET['country'];
include('data/viewCountry.php');
}
?>
</table>
</div>
</div>
<div id='graphicDIV'>
<div id='graphic'></div>
</div>
<div id='bottom'>
<div id='guestbook'>
<table border='0' cellspacing='0' cellpadding='0'>
<tr>
<td id='guestbookHeadline1'><font class='lightText bigText text'>Allgemeines Gästebuch</font>
</td>
<td id='guestbookTopLine' class='lineTopEnd'></td>
<td id='guestbookHeadline2'><font class='lightText bigText text'>Schreibe selbst</font>
</td>
</tr>
<tr id='guestbook2'>
<?
$post_id = 000000000000;
$get = "?mod=home";
?>
<td id='guestbookComment'>
<? include('data/viewguestbook.php'); ?>
</td>
<td id='guestbookLine'></td>
<td id='guestbookFormular'>
<? include('data/addguestbook.php'); ?>
</td>
</tr>
</table>
</div>
<div id='footerLeftLine' class='lineLeftEnd'>
<!-- LineLeftEnd -->
</div>
<div id='footerLine' class='horizontalLine'>
<!-- Line -->
</div>
<div id='footerRightLine' class='lineRightEnd'>
<!-- LineRightEnd -->
</div>
<div id='footer'>
<div class='footerLink'>
<!-- AdministratorLink -->
</div>
<div class='footerLink'>
<!-- ImpressumLink -->
</div>
<div class='footerLink'>
<!-- NewsArchiveLink -->
</div>
</div>
</div>
</body>
</head>
</html>
Thanks for help!
<form action="myworkspace/mywebsite/index.php">
should be
<form action="/myworkspace/mywebsite/index.php">
What you currently have is a path relative to your current page. The path that I have given is relative to your document root, which is http://localhost.
See also:
Absolute and Relative Paths, from About.com
Path (computing), from Wikipedia