I'm pretty new to html, php, mysql and i have to like learn the basics # my new workplace.
I'm having an annoying problem with my Form Validation. I'm using ubuntu server in combination with PuTTY
My problem is: that my 'Validation' and 'empty Field' check is not working propperly.
So when i go into my browser, my Form (Table) shows up as it should. When I hit the Submit button WITHOUT writing any stuff into the fields, the Form stays on the page and my Errors appear: ("Name is required, email, Nachname") That's right so far.
But when i fill in anything into the field(s), and then hit the Submit button, the form just disappears and i get like a blank page (but still having my CSS background n stuff).
No matter if comes up to the requirements, or not.
I'm trying to find out whats wrong since 3 whole days 9hrs/day # my workplace.
So hopefully anyone of you can help me finally get this thing work.
everything i post now is in the same order as i have it in my PuTTy
(nano)
My script starts like this:
CSS:
<html>
<head>
<title> Formular FINAL </title>
<style>
body {
background-image: url("http://fewpict.com/images/background-pictures/background-pictures-01.jpg");
}
.db_table {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
overflow: hidden;
overflow-y: auto;
position: fixed;
top: 80%;
left: 50%;
transform: translate(-50%, -50%);
width: 50%;
height: 100px;
}
.db_table td, tr {
color: white;
text-align: center;
}
.center_div {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.center_div td {
font-family: "Comic Sans", Comic Sans MS, cursive;
color: white;
text-align: left;
}
.error {color: #FF0000;}
</style>
</head>
<body>
PHP-Form Validation:
<?php
$VornameErr = "";
$emailErr = "";
$NachnameErr = "";
$Vorname = $_POST['Vorname'];
$email = $_POST['email'];
$Nachname = $_POST['Nachname'];
$allesok = "";
//input type hidden
if(isset($_POST['action'])){
//ÜBERPRÜFUNGSVARIABLE
$allesok = 1;
$errors = array();
if (empty($_POST) === false) {
$required_fields = array('Vorname', 'Nachname', 'email');
foreach($_POST as $key=>$value) {
if (empty($value) && in_array($key, $required_fields) === true ){
$errors[] = 'Fields marked with an asterisk are required';
break 1;
}
}
}
//Vorname Überprüfen
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Vorname"])) {
$allesok = 0;$VornameErr = "Name is required";
} else {
$Vorname = test_input($_POST["Vorname"]);
if (!preg_match("/[a-zA-Z]{3,}/",$Vorname)) {
$allesok = 0;$VornameErr = "Only letters and atleast 3 alpha characters Allowed";
}
}
}
if (empty($_POST["email"])) {
$allesok = 0;$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$allesok = 0;$emailErr = "Invalid email format";
}
}
//Nachname Überprüfen
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Nachname"])) {
$allesok = 0; $NachnameErr = "Nachname is required";
} else {
$Nachname = test_input($_POST["Nachname"]);
if (!preg_match("/[a-zA-Z]{3,}/",$Nachname)) {
$allesok = 0;$NachameErr = "Only letters and atleast 3 alpha characters Allowed";
}
}
}
function check_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}
MySQL:
if ($allesok) {
define('DB_NAME', 'formular');
define('DB_USER', 'David');
define('DB_PASSWORD', '****');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
if(isset($_POST['sent'])) {
$value1 = $_POST['Vorname'];
$value2 = $_POST['Nachname'];
$value3 = $_POST['email'];
$sql = "INSERT INTO formular (Vorname, Nachname, email) VALUES ('$value1', '$value2', '$value3')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
} else {
$msg1='<p> Your information was submitted successfully.</p>';
}
}
Echo Form:
if(isset($_POST['sent'])) {
?>
<div class="center_div">
<table>
<tr>
<td style="width: 200px;">Vorname: </td>
<td style="border-bottom: 1px solid black;"><?php echo $_POST['Vorname']; ?> </br></td>
</tr>
<tr>
<td style="width: 200px;">Nachname: </td>
<td style="border-bottom: 1px solid black;"><?php echo $_POST['Nachname']; ?> </br></td>
</tr>
<tr>
<td style="width: 200px;">E-Mail: </td>
<td style="border-bottom: 1px solid black;"><?php echo $_POST['email']; ?> </br> </td>
</tr>
</table>
<input type="button" value="Zurück" onClick="history.back();">
</div>
<?php
echo $msg1."<br /><br /><br />";
//Liste anzeigen
} elseif(isset($_POST['show_table'])) {
//fake formular <-----was made for still having the possibility to fill out stuff when i view the LIST
echo "<div class='center_div'>";
echo "<form action='toto2.php' method='POST'/>";
echo"<table>";
echo "<tr>";
echo "<th></th>";
echo "<th></th>";
echo "<th>span class='error'>* required field.</span></th>";
echo "<tr>";
echo "<td style= 'width: 200px;' > Vorname:* </td>";
echo "<td> <input type='text' name='Vorname' placeholder='Your Vorname...' /></td>";
echo "<td><span class='error'>*$VornameErr </span></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 200px;'> Nachname:* </td>";
echo "<td> <input type='text' name='Nachname' placeholder='Your Nachname...' /></td>";
echo "<td><span class='error'>*$NachnameErr</span></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 200px;'> E-Mail:* </td>";
echo "<td><input type='email' name='email' placeholder='Your E-Mail address...' /></td>";
echo "<td><span class='error'>*$emailErr</span></td>";
echo "</tr>";
echo "</table>";
echo "<input type='submit' value='SEND' name='sent' />";
echo "<input type='submit' value='Einträge anzeigen' name='show_table' />";
echo "<input type='button' value='Einträge ausblenden' onClick='history.back();'>";
echo "</div>";
echo "</form>";
//DB Tabelle
$query = "SELECT * FROM formular;";
$result = mysql_query($query);
echo '<div class="db_table">';
echo '<table>';
echo '<tr>';
echo '<th>ID</th>';
echo '<th>Vorname</th>';
echo '<th>Nachname</th>';
echo '<th>email</th>';
echo '</tr>';
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "</tr>";
}
echo '<tr>';
echo '<td>';
echo '<input type="button" value="Zurück" onClick="history.back();">';
echo '</td>';
echo '</tr>';
echo '</table>';
echo '</div>';
}
} else {
?>
HTML Form:
<div class="center_div">
<span class="error"></span>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table>
<tr>
<th></th>
<th></th>
<th><span class="error">* required field.</span></th>
<tr>
<td style= "width: 200px;" > Vorname:* </td>
<td> <input type="text" name="Vorname" placeholder="Your Vorname..." /></td>
<td><span class="error">* <?php echo $VornameErr;?></span></td>
</tr>
<tr>
<td style="width: 200px;"> Nachname:* </td>
<td> <input type="text" name="Nachname" placeholder="Your Nachname..." /></td>
<td><span class="error">* <?php echo $NachnameErr;?></span></td>
</tr>
<tr>
<td style="width: 200px;"> E-Mail:* </td>
<td><input type="text" name="email" placeholder="Your E-Mail address..." /></td>
<td><span class="error">* <?php echo $emailErr;?></span></td>
</tr>
</table>
<input type="hidden" name="action" value="1">
<input type="submit" value="SEND" name="sent" />
<input type="submit" value="Einträge anzeigen" name="show_table" />
<input type="button" value="Einträge ausblenden" onClick="history.back();">
</form>
</div>
<?php
}
mysql_close();
?>
</body>
</html>
Related
Here I have HTML, PHP and Jquery code with where I can edit all my record using the modal, the problem is that it keeps disappearing after I click on my update button, the modal only shows for about half a second. Can you help me? I'm actually new and just learning Jquery
this is the front view
<?php
include 'php/header.php';
require_once 'php/connect.php';
?>
<script>
$(document).ready(function(){
$("#input").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#body tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
</script>
<style>
table, th, td {
border: 1px solid black;
color: black;
width: auto;
margin: auto;
}
td {
text-align: center;
}
.edit {
background-color: blue;
font-family: algerian;
color: lime;
border-radius: 30px;
}
.edit:hover {
cursor: pointer;
background-color: yellow;
color: black;
}
.delete {
color: white;
background: black;
font-family: algerian;
}
.delete:hover {
color: black;
background: yellow;
}
</style>
<h1 style=" font-family: algerian; color: lime" align="center">Assets</h1>
<div class="filter">
<input style="height: 23px; float: right;" id="input" type="text" placeholder="Search.." autocomplete="off"></div>
<br>
<br>
<table align="center">
<tr>
<a onclick="" href=""></a>
<thead>
<th>Asset Num</th>
<th>FA num</th>
<th>Employee</th>
<th>Team</th>
<th>Contractor</th>
<th>Status</th>
<th>Category</th>
<th>Disposed</th>
<th colspan="2">Actions</th>
</thead>
<tbody id="body">
<?php
$sql = " SELECT * FROM assets
LEFT JOIN team
ON assets.team_id = team.team_id
LEFT JOIN contractor
ON assets.contractor_id = contractor.contractor_id
LEFT JOIN employee
ON assets.employee_id = employee.employee_id
LEFT JOIN item_status
ON assets.status_id = item_status.status_id
LEFT JOIN category
ON assets.category_id = category.category_id
ORDER BY category ";
$result = mysqli_query($conn, $sql);
if ($result->num_rows> 0) {
while($row = $result->fetch_assoc())
{
if (empty($row["disposal_status_id"])) {
$row["disposal_status_id"] = "NO"; }
else {
$row["disposal_status_id"] = "YES";
}
echo "<tr>";
echo "<td>". $row["asset_num"] ."</td>" ;
echo "<td>". $row["fa_num"]."</td>";
echo "<td>". $row["first_name"]. " " .$row["last_name"]. "</td>";
echo "<td>". $row["team"]."</td>";
echo "<td>". $row["F_name"]."</td>";
echo "<td>". $row["item_status"]."</td>";
echo "<td>". $row["category"]."</td>";
echo "<td>". $row["disposal_status_id"] ."</td>";
echo "<td><form method='POST' id='form'><button name='edit' class='edit' value=". $row['assets_id'] . " >Update</button></form>";
echo "<td><a class='delete' onclick=\"return confirm('Are you sure you want to delete this record?')\"
href='php/delete_asset.php?id=".$row['assets_id']."'>Delete</a>";
echo "</tr>";
}
}
else {
echo "<p style='font-family: algerian; font-size: 30px; margin-left: 45%; color: yellow ' >No Record</p>";
}
?>
</tbody>
</table>
and this is the modal view
<script>
$(document).ready(function() {
$(".edit").click(function(){
$("#modal").css('display', 'block');
});
});
</script>
<script>
$(document).ready(function() {
$("#cancel").click(function(){
$("#modal").css("display", "none");
});
});
</script>
<script>
$(document).ready(function(){
$("#form").submit(function() {
var edit = $(this).val();
$.ajax({
type: "POST",
data: {edit:edit},
});
});
});
</script>
<?php
include 'php/connect.php';
$id = $_POST['edit'];
$d = mysqli_query($conn, " SELECT * FROM assets
LEFT JOIN employee
ON assets.employee_id = employee.employee_id
LEFT JOIN team
ON assets.team_id = team.team_id
LEFT JOIN contractor
ON assets.contractor_id = contractor.contractor_id
LEFT JOIN item_status
ON assets.status_id = item_status.status_id
LEFT JOIN category
ON assets.category_id = category.category_id
LEFT JOIN disposal_status
ON assets.disposal_status_id = disposal_status.disposal_status_id
WHERE assets_id = '$id'");
$check = mysqli_fetch_array($d);
function load_employee()
{
include 'php/connect.php';
$output = '';
$sql = "SELECT * FROM employee ORDER BY first_name";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
$output .= "<option value='" . $row['employee_id'] ."'>"
. $row['first_name'] . " "
. $row['last_name'] . " </option>";
}
return $output;
}
?>
<div id="modal">
<script>
$(document).ready(function(){
$('#employee').change(function(){
var employee_id = $(this).val();
$.ajax({
url:"php/get_employee_team.php",
method: "POST",
data: {employee_id:employee_id},
dataType:"text",
success:function(data)
{
$('#team').html(data);
}
});
});
});
</script>
<link rel="stylesheet" type="text/css" href="../mystyle.css">
<style>
#modal {
position: fixed;
display: none;
width: 100%;
height: 100%;
top: 0%;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0,0,0,0.5);
z-index: 2;
cursor: pointer;
background-color: rgba(0,0,0,0.8); /* Black w/ opacity */
}
</style>
<form method="POST" action="php/update_assets.php" >
<table align="center">
<th colspan="4" >Update Asset # <?php echo $check['assets_id']; ?></th>
<tr></tr>
<input style="display: none" type="text" name="assets_id" value="<?php echo $check['assets_id'] ?>">
<td>Asset Num:</td>
<td><input type="text" name="asset_num" value="<?php echo $check['asset_num'] ?>" autocomplete="off" required></td>
<td>Unit Condition:</td>
<td><input type="text" name="condition" value="<?php echo $check['condition'] ?>" autocomplete="off" ></td>
<tr></tr>
<td>FA Num:</td>
<td><input type="text" name="fa_num" value="<?php echo $check['fa_num'] ?>" autocomplete="off" required></td>
<td>Audit Date:</td>
<td><input type="date" name="audit" value="<?php echo $check['audit_date'] ?>" autocomplete="off"></td>
<tr></tr>
<tr>
<td>Employee:</td>
<td>
<select id="employee" name="employee" >
<?php
echo "<option value='" . $check['employee_id'] ."'>"
. $check['first_name'] . " "
. $check['last_name'] . " </option>";
?>
<option><?php echo load_employee(); ?></option>
</select>
</select></td>
<td>Location:</td>
<td><input type="text" name="location" value="<?php echo $check['location'] ?>" autocomplete="off"></td>
</tr>
<tr>
<td>Team:</td>
<td>
<select style="color: black" id="team" name="team" readonly>
<option value= "<?php echo $check['team_id'] ?>" ><?php echo $check['team'] ?></option>
</select>
</td>
<td>Insurance Date:</td>
<td><input type="date" name="insurance" value="<?php echo $check['audit_date'] ?>"></td>
</tr>
<td>Contractor:</td>
<td><select name="contractor">
<?php
echo "<option value='" . $check['contractor_id'] ."'>"
. $check['F_name'] . " "
. $check['L_name'] . " </option>";
?>
<option>
<?php
$sql = "SELECT * FROM contractor" ;
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['contractor_id'] ."'>"
. $row['F_name'] . " "
. $row['L_name'] . " </option>";
}
?>
</option>
</select> </td>
<td>Comments:</td>
<td><input style="overflow: scroll;" type="text" name="comments" value="<?php echo $check['comments'] ?>" autocomplete="off"></td>
<tr></tr>
<td>Status:</td>
<td><select name="status">
<?php
echo "<option value='" . $check['status_id'] ."'>"
. $check['item_status'] . " </option>";
?>
<option>
<?php
$sql = "SELECT * FROM item_status " ;
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['status_id'] ."'>"
. $row['item_status'] . " </option>";
}
?>
</option>
</select>
</td>
<td colspan="2"> <br></td>
<tr></tr>
<td>Asset Category:</td>
<td>
<select name="category">
<?php
echo "<option value='" . $check['category_id'] ."'>"
. $check['category'] . " </option>";
?>
<option>
<?php
$sql = "SELECT * FROM category" ;
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['category_id'] ."'>"
. $row['category'] . " </option>";
}
?>
</option>
</select>
</td>
<td colspan="2"> <br></td>
<tr></tr>
<td>Description:</td>
<td><input type="text" name="description" value="<?php echo $check['description'] ?>" autocomplete="off">
<td colspan="2" style="text-align: center; color: blue; font-family: algerian" >DISPOSAL DETAILS</td>
<tr></tr>
<td>Serial Number:</td>
<td><input type="text" name="serial" value="<?php echo $check['serial_num'] ?>" autocomplete="off"></td>
<td>Disposal Location</td>
<td><input type="text" name="disposal_loc" value="<?php echo $check['disposal_location'] ?>" autocomplete="off"></td>
<tr></tr>
<td>Date Aquired:</td>
<td><input type="date" name="acquired" value="<?php echo $check['date_get'] ?>" autocomplete="off"></td>
<td>Date Disposed</td>
<td><input type="date" name="disposal_date" value="<?php echo $check['disposal_date'] ?>" autocomplete="off"></td>
<tr></tr>
<td>Purchase Price:</td>
<td><input type="text" name="price" value="<?php echo $check['purchase_price'] ?>" autocomplete="off"></td>
<td>Disposed Status</td>
<td>
<select id="dept" name="disposal_status">
<?php
if ($check['disposal_status_id'] == 0) {
echo "<option value='". $checl['disposal_status_id'] ."' >";
echo "NOT YET";
echo "</option>";
}
?>
<?php
$sql = "SELECT * FROM disposal_status " ;
$result = mysqli_query($conn, $sql);
while ($row =mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['disposal_status_id'] ."'>"
. $row['status'] . " </option>";
}
?>
</select>
</td>
<tr></tr>
<br>
<td style="text-align: center;" colspan="4"><button type="submit" name="btn" class="submit" >Submit</button>
<p class="submit" id="cancel">Cancel</p></form>
</td>
</table>
</div>
I try to send the data in the same page and try to change the CSS code when I click on the Update button, sending the id and fetching the data required using ajax Jquery, Hope someone can help me, thanks in advance
I think you need to use preventDefault() function because the nature of submiting form is to refresh the page that's also the reason why the modal is appearing just a couple of seconds only or sometimes it is not appearing.
Please try these line of code.
$("#form").on("submit",function(event) {
event.preventDefault();
var edit = $(this).val();
$.ajax({
type: "POST",
data: {edit:edit},
});
});
});
I am developing an online examination system. Everything works fine but the questions are selected and displayed from question number one to the last question. I wanted to do the following:
Select the questions and display them one after the other at random.
Add a skip button so that students can skip any question they can't answer and it will be rolled back to them later.
Give a time to the answering session so that when the time is up it will stop the student.
This is my code for the question selection.
<!DOCTYPE html">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
body{background-color:#f4fff8;}
#aq{
position:relative;
top:50px;
}
#st{
position:relative;
top:30px;
}
.btn{
background-color:#dedbb8;
color:#016e37;
text-shadow:3px 3px 3px gray;
box-shadow:3px 3px 3px gray;
height:40px;
text-align:center;
font-size:25px;
font-family:'Times New Roman';
font-weight:bold;
font-style:italic;
margin-top:2px;
border-radius: 2px;
}
.style8{
padding-top:5px;
font-size:20px;
}
#pt{
padding-top:20px;
}
h1{
position:relative;
color:green;
top:30px;
}
h2{
position:relative;
color:orange;
top:30px;
}
</style>
</head>
<body>
<?php
require_once("dbconnect.php");
include("header.php");
include("footer.php");
include("stdlogsession.php");
extract($_GET);
extract($_POST);
extract($_SESSION);
$student_id=$_SESSION['login_user'];
$student_id = stripslashes($student_id);
$student_id = mysqli_real_escape_string($db_conn,$student_id);
if(!empty($_GET['examid']))
{
$examid =$_GET['examid'];
}
// Selecting Database
$db = mysqli_select_db($db_conn,$mysql_database);
$rs=mysqli_query($db_conn,"select * from objquestions where exam_id='$examid'") or die('Error: ' .mysql_error($db_conn));
if(!isset($_SESSION['qn']))
{
$_SESSION['qn']=0;
}
if(!empty($_POST['submit'])=='Answer' && isset($ans))
{
mysqli_data_seek($rs,$_SESSION['qn']);
$row= mysqli_fetch_row($rs);
if($ans==$row[8])
{
$remarks= "Correct";
$mark=$row[9];
} else {
$remarks= "Wrong";
$mark=0;
}
mysqli_query($db_conn,"insert into results(exam_id,student_id,quesNum,choice,remarks,mark) values ('$row[1]','$student_id','$row[2]', '$ans','$remarks','$mark')") or die(mysqli_error($db_conn));
$_SESSION['qn']=$_SESSION['qn']+1;
}
if($_SESSION['qn']>mysqli_num_rows($rs)-1)
{
$examN=mysqli_query($db_conn,"select examName from exampaper where exam_id='$examid'") or die('Error: ' .mysqli_error($db_conn));
$row = mysqli_num_rows($examN);
if ($row > 0) {
$rows=mysqli_fetch_assoc($examN);
$Enam=$rows['examName'];
}
echo "<center><h1>congrats! you have successfully finished your " .$Enam."</h1></center>";
echo "<center><h2> Click <a href=instresult.php>here</a> for your results </h2></center>";
unset($_SESSION['qn']);
exit();
}
if(!empty($_POST['submit'])=='Skip' && !isset($ans))
{
mysqli_data_seek($rs,$_SESSION['qn']);
$row= mysqli_fetch_row($rs);
$_SESSION['qn']=$_SESSION['qn']+1;
}
$rs=mysqli_query($db_conn,"select * from exampaper where exam_id='$examid'") or die('Error: ' .mysql_error($db_conn));
$rows = mysqli_num_rows($rs);
while($rows=mysqli_fetch_row($rs))
{
echo "<center>";
echo "<table id=aq >";
echo "<tr><td>
<span class=style8>SUBJECT:</span></td>
<td class=style8>$rows[2]</td>
</tr>";
echo "<tr><td>
<span class=style8>EXAMINATION NAME:</span></td>
<td class=style8>$rows[1]</td>
</tr>";
echo "<tr><td>
<span class=style8>EXAMINATION DATE:</</span></td>
<td class=style8>$rows[3]</td>
</tr>";
echo "<tr><td>
<span class=style8>TOTAL TIME:</span></td>
<td class=style8>$rows[4]</td>
</tr>";
echo "<tr><td>
<span class=style8>INSTRUCTIONS:</span></td>
<td class=style8>$rows[5]</td>
</tr>";
echo "</table>";
echo "</center>";
}
$rs=mysqli_query($db_conn,"select * from objquestions where exam_id='$examid'") or die(mysqli_error($db_conn));
if($_SESSION['qn']>mysqli_num_rows($rs)-1)
{
unset($_SESSION['qn']);}
echo "<center>";
echo "<table id=st>";
mysqli_data_seek($rs,$_SESSION['qn']);
$row= mysqli_fetch_row($rs);
echo "<form name=myfm method=post action=exam.php>";
$n=$_SESSION['qn']+1;
echo "<tR ><td id=pt><span class=style8>Quetion ". $n .": $row[3]</style></td></tr>";
echo "<tr><td class=style8><input type=radio name=ans value=A>$row[4]</td></tr>";
echo "<tr><td class=style8> <input type=radio name=ans value=B>$row[5]</td></tr>";
echo "<tr><td class=style8><input type=radio name=ans value=C>$row[6]</td></tr>";
echo "<tr><td class=style8><input type=radio name=ans value=D>$row[7]</td></tr>";
echo "<tr><td><input class=btn type=submit name=submit value='Answer'> <input align=right class=btn type=submit name=submit value='Skip'></td></tr></form>";
echo "</table>";
echo "</center>";
?>
</body>
</html>
I've encountered a problem which i'll try to describe below...
Let's say i've got a button for each table row (table is created by acquiring the data from the database...). the button sends a massive of values via post.
<button value='$e->eventId|$e->eventType|$e->eventDate|$e->eventPrice|$e->eventDescr' name='editValue'>EDIT</button>
I've written a function to explode the values of the massive and fill the inputs on the same page.
if(!empty($_POST['editValue'])) {
$editValue_fill = explode("|", $_POST["editValue"]);
$eventId = $editValue_fill[0];
$eventType = $editValue_fill[1];
$eventDate = $editValue_fill[2];
$eventPrice = $editValue_fill[3];
$eventDescr = $editValue_fill[4];
}
Next comes the function to edit the row in the database.
function editEvent ($id, $type, $date, $price, $descr){
$mysqli = new mysqli($GLOBALS["serverHost"], $GLOBALS["serverUsername"], $GLOBALS['serverPassword'], $GLOBALS['dbName']);
$stmt = $mysqli->prepare("UPDATE events_archive SET eventType='?', eventDate='?', eventPrice='?', eventDescr='?' WHERE id='?'");
$stmt->bind_param("ssdss", $type, $date, $price, $descr, $id);
$stmt->execute();
header("Refresh:0");
if($stmt->execute()){
$eventNotice="Event successfully updated!";
}else{
$eventNotice = "Failed to save...";
}
return $eventNotice;
}
And, of course, the usage of the function which apparently doesnt work. The page just refreshes and nothing happens.
if(empty($eventTypeError)&& empty($eventDateError)&& empty($eventPriceError) && empty($eventDescrError)
&& isset($_POST['eventType']) && isset($_POST['eventDate']) && isset ($_POST['eventPrice']) && isset
($_POST['eventDescr']) && !empty($eventId)){
$eventNotice = editEvent($eventId, cleanInput($eventType), cleanInput($eventDate), cleanInput($eventPrice), cleanInput($eventDescr));
}
So basically, gets values from the row -> fills them into inputs in the same page -> if $eventId is set then updates the row, if not - creates a new row (diff. function)
Could anyone give me a tip or help solving the problem? I've been trying to understand the issue and failed dramatically...
<?php
require ("functions.php");
var_dump($_POST);
//kas on sisseloginud, kui ei ole siis
//suunata login lehele
//kas ?logout on aadressireal
if (isset($_GET['logout'])){
session_destroy();
header("Location: login.php");
}
if (!isset ($_SESSION["userId"])){
header("Location: login.php");
}
$confirm = "";
$eventNotice = "";
$eventTypeError = "";
$eventDateError = '';
$eventPriceError = '';
$eventDescrError = '';
$eventType = '';
$eventDescr = '';
$eventPrice = '';
$eventDate = date("Y-m-d");
if (isset ($_POST["eventType"])){
if (empty($_POST['eventType'])){
$eventTypeError = "Please choose the event type!";
} else {
$eventType = $_POST["eventType"];
}
}
if (isset ($_POST ["eventDate"])){
if (empty ($_POST ["eventDate"])){
$eventDateError = "Please choose the date!";
} else {
$eventDate = $_POST["eventDate"];
}
}
if (isset ($_POST ["eventPrice"])){
if (empty ($_POST ["eventPrice"])){
$eventPriceError = "Please type in the price!";
} else {
$eventPrice = $_POST["eventPrice"];
}
}
if (isset ($_POST ["eventDescr"])){
if (empty ($_POST ["eventDescr"])){
$eventDescrError = "Please type in the description!";
}elseif (strlen($_POST["eventDescr"])< 10) {
$eventDescrError = "Description must be longer than 10 symbols!";
$eventDescr = $_POST['eventDescr'];
}else{
$eventDescr = $_POST['eventDescr'];
}
}
$event = getAllEvents();
if(!empty($_POST['delValue'])) {
delEvent($_POST['delValue']);
}
if(!empty($_POST['editValue'])) {
$editValue_fill = explode("|", $_POST["editValue"]);
$eventId = $editValue_fill[0];
$eventType = $editValue_fill[1];
$eventDate = $editValue_fill[2];
$eventPrice = $editValue_fill[3];
$eventDescr = $editValue_fill[4];
echo ($eventId);
echo ($eventDate);
echo ($eventPrice);
echo ($eventType);
echo ($eventDescr);
}
if(empty($eventTypeError)&& empty($eventDateError)&& empty($eventPriceError) && empty($eventDescrError)
&& isset($_POST['eventType']) && isset($_POST['eventDate']) && isset ($_POST['eventPrice']) && isset
($_POST['eventDescr'])){
if(isset($_POST['editValue'])){
$eventNotice = editEvent($eventId, cleanInput($eventType), cleanInput($eventDate), cleanInput($eventPrice), cleanInput($eventDescr));
}elseif(!isset($_POST['editValue'])){
$eventNotice = newEvent(cleanInput($eventType), cleanInput($eventDate), cleanInput($eventPrice), cleanInput($eventDescr));
}
}
?>
<html>
<style>
#import "styles.css";
ul.tab {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
ul.tab li {float: left;}
ul.tab li a {
display: inline-block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
}
ul.tab li a:hover {
background-color: #ddd;
}
ul.tab li a:focus, .active {
background-color: #ccc;
}
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
</style>
<body>
<form method ="post">
<table class="table1">
<tr>
<td style="text-align:center"><h1>Data</h1></td>
</tr>
<tr>
<th><h2>Profile</h2></th>
</tr>
<tr>
<td>
<table class="table2">
<tr>
<td colspan="3"">Welcome <?=$_SESSION['email'];?>!</td>
</tr>
<tr>
<td colspan="3" style="text-align:center">Log out</td>
</tr>
</table>
<tr>
<td>
<tr>
<td>
<ul class="tab">
<li>Add/edit</li>
<li>Archive</li>
</ul>
<div id="Add/edit" class="tabcontent">
<input type="hidden" value='eventId'>
<table class="table2">
<tr>
<td>Event type:<span class = 'redtext'>*</span></td>
<td style="text-align:left">
<select name="eventType">
<?php if(empty($eventType)){?>
<option value="" selected>Choose here</option>
<?php } else { ?>
<option value="">Choose here</option>
<?php } ?>
<?php if($eventType == "Planned service"){?>
<option value="Planned service" selected>Planned service</option>
<?php } else { ?>
<option value="Planned service">Planned service</option>
<?php } ?>
<?php if($eventType == "Unplanned service"){?>
<option value="Unplanned service" selected>Unplanned service</option>
<?php } else { ?>
<option value="Unplanned service">Unplanned service</option>
<?php } ?>
<?php if($eventType == "Fuel checks"){?>
<option value="Fuel checks" selected>Fuel checks</option>
<?php } else { ?>
<option value="Fuel checks">Fuel checks</option>
<?php } ?>
<?php if($eventType == "Tuning"){?>
<option value="Tuning" selected>Tuning</option>
<?php } else { ?>
<option value="Tuning">Tuning</option>
<?php } ?>
<?php if($eventType == "Car accident"){?>
<option value="Car accident" selected>Car accident</option>
<?php } else { ?>
<option value="Car accident">Car accident</option>
<?php } ?>
</select>
</td>
</tr>
<tr><td colspan="3" class="redtext" style="text-align:center"><?=$eventTypeError?></td></tr>
<tr>
<td>Date:<span class = 'redtext'>*</span></td>
<td style="text-align:left"><input name="eventDate" type ="date" min="1900-01-01" max = "<?=date('Y-m-d'); ?>" value = "<?=$eventDate?>" placeholder="YYYY-MM-DD"></td>
</tr>
<tr><td colspan="3" class="redtext" style="text-align:center"><?=$eventDateError?></td></tr>
<tr>
<td>Price:<span class = 'redtext'>*</span></td>
<td style="text-align:left"><input type="text" name="eventPrice" placeholder="ex. 15.50" onkeypress="return onlyNumbersWithDot(event);" / value = "<?=$eventPrice?>"></td>
<script type="text/javascript">
function onlyNumbersWithDot(e) {
var charCode;
if (e.keyCode > 0) {
charCode = e.which || e.keyCode;
}
else if (typeof (e.charCode) != "undefined") {
charCode = e.which || e.keyCode;
}
if (charCode == 46)
return true
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
</tr>
<tr><td colspan="3" class="redtext" style="text-align:center"><?=$eventPriceError?></td></tr>
<tr>
<td>Description:<span class = 'redtext'>*</span></td>
<td style="text-align:left"><textarea name="eventDescr" cols="50" rows="10" placeholder="Describe event here..."><?=$eventDescr?></textarea></td>
</tr>
<tr><td colspan="3" class="redtext" style="text-align:center"><?=$eventDescrError?></td></tr>
<tr>
<td colspan="3" style="text-align:center"><button type ="submit" value = "Submit">Save</button></td>
</tr>
<tr>
<td colspan="3" style="text-align:center"><p class = "redtext"><?=$eventNotice;?></p></td>
</tr>
</table>
</div>
</form>
<form method="post">
<div id="Archive" class="tabcontent">
<table class="table2">
<tr>
<td colspan="3"">
<?php
$html = "<table>";
$html .= "<tr>";
$html .= "<th>Event type</th>";
$html .= "<th>Date</th>";
$html .= "<th>Price(€)</th>";
$html .= "<th>Description</th>";
$html .= "<th>Delete</th>";
$html .= "<th>Edit</th>";
$html .= "</tr>";
foreach($event as $e){
$html .= "<tr>";
$html .= "<td>$e->eventType</td>";
$html .= "<td>$e->eventDate</td>";
$html .= "<td>$e->eventPrice</td>";
$html .= "<td>$e->eventDescr</td>";
$html .= "<td><button style='border:none; background-color: transparent;' value='$e->eventId' name='delValue' onclick=\"return confirm('Do you really want to delete this row?')\"><img src='delete.png' width='20' height='20'></button></td>";
$html .= "<td><button style='border:none; background-color: transparent;' value='$e->eventId|$e->eventType|$e->eventDate|$e->eventPrice|$e->eventDescr' name='editValue'><img src='edit.png' width='20' height='20'></button></td>";
$html .= "</tr>";
}
$html .= "</table>";
echo $html;
?>
</td>
</tr>
</div>
<script>
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
</td>
</tr>
</table>
</td>
</tr>
</div>
</form>
</body>
</html>
Looking forward to Your help!
Best regards :)
if(isset($_POST['editValue'])){
$eventNotice = editEvent($eventId, cleanInput($eventType), cleanInput($eventDate), cleanInput($eventPrice), cleanInput($eventDescr));
}
I've been having a really annoying "error"...
My page have a header, left sidebar and a footer. After i submit the form everything goes really good EXCEPT now the footer is in the middle of the page!! I tried everything i knew to and searched but i still can't solve that.
BEFORE SUBMIT with footer on the right place : http://i48.tinypic.com/ly1dl.jpg
AFTER SUBMIT with footer on the middle of the page : http://i47.tinypic.com/168a3uq.jpg
Here is The code :
Header:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="stylesheets/stylesheet.css" type="text/css" rel="stylesheet" />
<title>Wilson Electric & Alarm Company</title>
</head>
<body>
<div id="header">
<h1 id="toptitle"> Wilson Electric & Alarm Company </h1>
</div>
<div id="navigation_top">
</div>
<div id="column_left">
<ul id="left_menu">
<li> Insert Page </li>
<li> Search Page </li>
<li> Menu 4 </li>
<li> Menu 5 </li>
<li> Menu 6 </li>
<li> User </li>
<li> Admin </li>
<li> Login </li>
<li> Create Username </li>
</ul>
</div>
<div id="content">
Footer :
</div>
<div id="footer">
This is a Footer.
</div>
</body>
</html>
Stylesheet : (there is also a default reset in my stylesheet before the #container)
#container {
width:100%;
height:100%;
}
#header {
width: 100%;
height:120px;
}
#column_left {
float:left;
width:15%;
height:100%;
border-right: 3px solid #06F;
border-left: 3px solid #06F;
}
#content {
width:100%;
height:100%;
}
#footer {
width:100%;
height: 30px;
border-top:3px solid #06F;
}
#navigation_top {
width:100%;
height:15px;
border-bottom:3PX solid #06F;
}
#toptitle {
padding:30px;
font-size:300%;
font-weight:700;
}
.formtitle {
font-weight:800;
font-size:150%;
}
#form {
margin-left:230px;
padding-top:30px;
}
table,th, td {
border: 1px solid black;
}
table {
width:1024px;
}
th {
font-weight:800;
background-color:#0FF;
}
And here is the page code that is giving me those problems!!
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
if (isset($_POST['search'])) {
$date1 = mysql_prep($_POST['date1']);
$date2 = mysql_prep($_POST['date2']);
$latte = mysql_prep($_POST['latte']);
$coffee = mysql_prep($_POST['paid']);
$query = "SELECT *
FROM payroll
WHERE (day BETWEEN '{$date1}' AND '{$date2}')
AND (company = '{$latte}')
AND (paid = '{$coffee}')
ORDER BY day ASC ";
$result = mysql_query($query, $connection);
$woof = "SELECT SUM(p.hours) AS sum_hours
, COUNT(DISTINCT p.day) AS cnt_days
FROM PAYROLL p
WHERE p.day BETWEEN '{$date1}' AND '{$date2}'
AND company = '{$latte}'
AND paid = '{$coffee}' ";
$raw = mysql_query($woof, $connection);
if(!$raw) { die(mysql_error());}
$meow = mysql_result($raw, 0, 0);
$days = mysql_result($raw, 0, 1);
if(!$result) {
echo "FAIL";
} else {
$message = "<table>
<tr>
<th> Date </th>
<th> Hours </th>
<th> Job Title </th>
<th> Job Description </th>
<th> For </th>
<th> Paid </th>
</tr>";
while($row = mysql_fetch_array($result)) {
$company = $row['company'];
if($company == 0) {
$company = "Wilson Electric";
} if($company == 1) {
$company = "Wilson Rental";
} if ($company == 2) {
$company = "Church of Christ";
}
$paid = $row['paid'];
if($paid == 0) {
$paid = "Yes";
} else {
$paid = "<form action=\"update.php\" method=\"post\" ><input type=\"checkbox\" name=\"paid\" value=\"0\"> NO ";
}
$message .= "<tr>";
$message .= "<td class=\"center\">" . $row['day'] . "</td>";
$message .= "<td class=\"center\">" . $row['hours'] . "</td>";
$message .= "<td style=\"padding:5px;\">" . $row['job_title'] . "</td>";
$message .= "<td style=\"padding:5px;\">" . $row['job_description'] . "</td>";
$message .= "<td style=\"padding:5px;\">" . $company . "</td>";
$message .= "<td class=\"center\">" . $paid . "</td>";
$message .= "</tr>";
}
$message .= "<tr>";
$message .= "<td class=\"center\"> Total Days: " . $days . "</td>";
$message .= "<td class=\"center\"> Total Hours: " . $meow . "</td>";
$message .= "<td class=\"center\"> </td>";
$message .= "<td class=\"center\"> </td>";
$message .= "<td class=\"center\"> </td>";
$message .= "<td class=\"center\"> <input type=\"submit\" name=\"gamind\" value=\"Update\"> </form> </td>";
$message .= "</tr>";
}
}
?>
<?php include("includes/header.php"); ?>
<form action="" method="post" id="form">
<h1 class="formtitle"> Search </h1>
<br />
<table id="table1">
<th> From </th>
<th> To </th>
<th> For </th>
<th> Paid </th>
<tr>
<td>
<input type="date" name="date1" value="" >
</td>
<td>
<input type="date" name="date2" value="" >
</td>
<td>
<select name="latte" >
<option value="0"> Wilson Electric </option>
<option value="1"> Wilson Rental </option>
<option value="2"> Church of Christ</option>
</select>
</td>
<td>
<input type="radio" name="paid" value="0"> Yes <br />
<input type="radio" name="paid" value="1"> No <br />
</tr>
<tr>
<td colspan="4" align="right"> <input type="submit" name="search" value="Search">
</td>
</tr>
</table>
<br />
<br />
<p>
<?php
if(!empty($message)) {
echo "<h1 class=\"formtitle\"> Payroll Result </h1>";
echo $message;
}
?>
</p>
</form>
<?php include("includes/footer.php"); ?>
<?php mysql_close($connection); ?>
Sorry for the loooong post!! I hope someone knows the solution for my problem!
Thanks! :)
There's a lot of code here to take in, but I think your $message builds <tr>s, but when you echo $message, you're not inside a table element..?
I have tried a few things but cant seem to implement pagination properly, can someone point me in the right direction and give me a few code examples of how i can properly implement pagination in my code
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<link href="/lightbox/css/lightbox.css" rel="stylesheet" />
<link rel="stylesheet" href="img/reveal.css">
<script src="/lightbox/js/jquery-1.7.2.min.js"></script>
<script src="/lightbox/js/lightbox.js"></script>
<script src="img/jquery.min.js" type="text/javascript"></script>
<script src="img/jquery.reveal.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" type="text/javascript"></script>
<style type="text/css">
#divspace {
position: relative;
width: 100%;
height: 50px;
z-index: -9999;
vertical-align bottom;
}
#divspace1 {
position: relative;
width: 100%;
height: 50px;
z-index: -9999;
vertical-align: bottom;
padding-left: 400px;
}
#search1 {
position: absolute;
width: 203px;
height: 30px;
z-index: -9999;
vertical-align: bottom;
left: 832px;
top: 1px;
z-index: 9999;
}
#text1 {
position: absolute;
width: 203px;
height: 30px;
z-index: -9999;
vertical-align: bottom;
top: 13px;
z-index: 9999;
left: 268px;
font-family: Tahoma, Geneva, sans-serif;
font-weight: bold;
color: #FFF;
font-style: italic;
font-size: 22px;
}
#apDiv1 {
position:absolute;
width:560px;
height:27px;
z-index:1;
left: 93px;
top: 247px;
}
#cat {
position:absolute;
width:227px;
height:500px;
z-index:1;
left: 3px;
top: 48px;
background-color: #FFF;
font-family:"Lucida Console", Monaco, monospace;
}
.container1 {
position:relative;
padding: 0 20px 0 20px;
margin: auto;
width: 1000px;
background-image: url(images/skyblue.png);
margin-top: 20px;
margin-bottom: 20px;
padding-bottom: 300px;
-moz-border-radius: 8px;
border-radius: 8px;
}
.label{
text-align:right;
}
#submit{
text-align:center;
}
</style>
<script type = "text/javascript">
function myfunction(url)
{
window.location.href = url;
}
</script>
<script type="text/javascript">
$(document).ready(function(){
$(".expanderHead").click(function(){
var $exsign = $("#expanderSign");
$(this).find("#expanderContent").slideToggle();
$exsign.html($exsign.text() == '+' ? '-': '+');
// simplify your if/else into one line using ternary operator
// if $exsign.text() == "+" then use "-" else "+"
});
});
</script>
</head>
<body>
<div id="header">
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=439699742746900";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div id="search">
<center>
<form method="GET" action="search.php" style= "padding: 1px;">
<input name="search" id="s" type="text" value="<?php echo $_GET['search']; ?>" size="20" />
<select name="category" id="category" >
<?php if(isset($_GET['submit'])) { ?>
<option value="<?php echo $_GET['category']; ?>" selected="selected"><?php echo $_GET['category']; ?></option>
<?php }else{ ?>
<option value=""> -- select -- </option>
<?php } ?>
<option value="">All</option>
<option value="Books">Books</option>
<option value="Textbooks">TextBooks</option>
<option value="Tickets">Tickets</option>
<option value="Electronics">Electronics</option>
<option value="Clothing">Clothing</option>
<option value="Accessories">Accessories</option>
<option value="Furniture">Furniture</option>
<option value="Imagery">Imagery</option>
<option value="Business">Business</option>
<option value="Clothing">Clothing</option>
<option value="Multi">Multimedia</option>
</select>
<select name="university" id="university" >
<option value="">Aston University</option>
</select>
<input id="searchSubmit" type="submit" value="" name="submit"/>
</form>
</center>
</div>
<div id="imagelogo" onclick = "window.location.href = 'index.php'" >
<p> Buy and sell stuff around University</p>
</div>
<ul id="navigation" name="navigation">
<li id="nav-home">Home | Search | Selling | Buying | FAQ | Contact </li>
</ul>
<div id="account">
<?php
if( isset( $_SESSION['username'] ) ){
echo "<a href='securedpage1.php'>My Account</a><img src='images/uni-icon.png' width='30' height='18' style='vertical-align: middle;'/>";
}else{
echo "<a href='login.php' >Login</a><img src='images/uni-icon.png' width='30' height='18' style='vertical-align: middle;'/>";
}
?>
</div>
<div id="registerlogout">
<?php
if( isset( $_SESSION['username'] ) ){
echo "<a href='logout.php'>Logout</a>";
}else{
echo "<a href='register.php'> Register</a>";
}
?>
</div>
<center>
<center>
</div>
<div class="container1">
<div id="cat">
<table width="225" border="1" cellspacing="10" cellpadding="10">
<tr>
<td>Accessories</td>
</tr>
<tr>
<td>Accommodation</td>
</tr>
<tr>
<td>Books</td>
</tr>
<tr>
<td>Business</td>
</tr>
<tr>
<td>Clothing</td>
</tr>
<tr>
<td>Electronics</td>
</tr>
<tr>
<td>Furniture</td>
</tr>
<tr>
<td>Imagery</td>
</tr>
<tr>
<td>Multimedia</td>
</tr>
<tr>
<td>Services</td>
</tr>
<tr>
<td>Tickets</td>
</tr>
</table>
</div>
<div id="text1">Items For Sale:
</div>
<div id="search1">
<form method="GET" action="search.php" style= "padding: 1px;">
<select name="price" id="price" >
<?php if(isset($_GET['submit'])) { ?>
<option value="<?php echo $_GET['price']; ?>" selected="selected"><?php echo $_GET['price']; ?></option>
<?php }else{ ?>
<option value=""> -- select -- </option>
<?php } ?>
<option value=""></option>
<option value="DESC">Highest to Lowest</option>
<option value="ASC">Lowest to Highest</option>
</select>
<input id="searchSubmit" type="submit" value="" name="submit"/>
</form>
</div>
<div id="divspace"></div>
<div style=" padding-left: 240px" ">
<?php
// Include database connection settings
include('config.php');
include('config.inc');
// Check and set username
$username = (isset($_SESSION['username']) ? $_SESSION['username'] : 'guest');
// Check and set category
$category = (!empty($_GET['category']) ? $_GET['category'] : null);
// Check and set search
if(!empty($_GET['search'])){
$search = $_GET['search'];
}else{
$search = null;
}
// Check that $_GET['price'] is ASC if not set to DESC
// as static values its ok to directly put in the query
if(isset($_GET['price']) && $_GET['price'] == 'ASC'){
$price = 'ASC';
}else{
$price = 'DESC';
}
if ($search !== null){
$sql = "SELECT * FROM people WHERE MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE)";
$q = $conn->prepare($sql) or die("failed!");
// Bind the params to the placeholders
$q->bindParam(':search', $search, PDO::PARAM_STR);
$q->execute();
}
if ($search !== null && $category !== null){
$sql = "SELECT * FROM people WHERE MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE) AND category = :category";
$q = $conn->prepare($sql) or die("failed!");
// Bind the params to the placeholders
$q->bindParam(':search', $search, PDO::PARAM_STR);
$q->bindParam(':category', $category, PDO::PARAM_STR);
$q->execute();
}
if ($category !== null && $search !== null && isset($price)){
$sql = "SELECT *
FROM people
WHERE MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE)
AND category = :category
ORDER BY price ".$price;
$q = $conn->prepare($sql);
// Bind the params to the placeholders
$q->bindParam(':search', $search, PDO::PARAM_STR);
$q->bindParam(':category', $category, PDO::PARAM_STR);
$q->execute();
}
if ($category == null && $search !== null && isset($price)){
$sql = "SELECT *
FROM people
WHERE MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE)
ORDER BY price ".$price;
$q = $conn->prepare($sql);
// Bind the params to the placeholders
$q->bindParam(':search', $search, PDO::PARAM_STR);
$q->execute();
}
if ($q){
//declaring counter
$count=0;
while($r = $q->fetch(PDO::FETCH_ASSOC)){
$row = $r;
$fname = $row['fname'];
$lname = $row['lname'];
$firstname = $row['firstname'];
$surname = $row['surname'];
$expire = $row['expire'];
$oDate = strtotime($row['expire']);
$sDate = date("d/m/y",$oDate);
//counter equals
$count++;
//insert an image every 5 rows
if($count==5){
$count=0;
echo "<table width='50%' style='border-bottom:1px solid #000000;'>";
echo "<tr>";
echo "<td>";
echo "<div id='page-wrap'>";
echo "<div class='discounted-item freeshipping'>";
echo "<a href='images/box1.png' rel='lightbox'><img src='images/box1.png' width='160px' height='200px' /></a>";
echo "<div class='reasonbar'><div class='prod-title' style='width: 70%;'>AN AD CAN GO HERE</div><div class='reason' style='width: 29%;'><b>Ad Company</b></div></div>";
echo "<div class='reasonbar'><div class='prod-title1' style='width: 70%;'>Description about the advert from a company</div><div class='reason1' style='width: 29%;'>Category: Advert</div></div>";
echo "<div class='reasonbar'><div class='prod-title2' style='width: 70%;'>HELLO, User</div><div class='reason2' style='width: 29%;'></div></div>";
echo "</td>";
echo "</tr>";
echo "</td>";
echo "</tr>";
echo "</table>";
}
echo "<table width='50%' style='border-bottom:1px solid #FFFFFF'>";
echo "<tr>";
echo "<td>";
echo "<div id='page-wrap'>";
echo "<div class='discounted-item freeshipping'>";
echo "<a href='./img/users/" . $row['category'] . "/" . $row['username'] . "/" . $row['filename'] . "' rel='lightbox'><img src=\"./img/users/" . $row['category'] . "/" . $row['username'] . "/" . $row['filename'] . "\" alt=\"\" width='80px' height='100px' /></a>";
echo "<div class='expanderHead'>";
echo "<div class='reasonbar'><div class='prod-title'>" .$row['fname'] . "</div><div class='reason' style='width: 29%;'><b>". $row['firstname'] . " " . $row['surname'] ."</b></div></div>";
echo "<div id='expanderContent' style='display:none'><div class='reasonbar'><div class='prod-title1'>" . $row['lname'] . "</div><div class='reason1' style='width: 29%;'>Category:<br /> ". $row['category'] . "</div></div>";
echo "<div class='reasonbar'><div class='prod-title2' style='width: 70%;'><form action='adclick.php' method='post'><input type='hidden' name='username' value='" . $row['username'] . "'/><input type='submit' name='submit' value='Reply To this ad'></form></div><div class='reason2' style='width: 29%;'></div></div></div>";
echo "<div class='reasonbar'><div class='prod-title2' style='width: 70%;'>Expires: $sDate</div><div class='reason2' style='width: 29%;'>Price: £". $row['price'] . "</div></div>";
echo "</td>";
echo "</tr>";
echo "</td>";
echo "</tr>";
echo "</div>";
echo "</table>";
}
}
else
echo "No results found for \"<b>$search</b>\"";
//disconnect
mysql_close();
?>
</div>
</div>
<div class="footer">
<p> Private Policy | Terms and Conditions | FAQ </p>
</div>
</body>
</html>
Sample Code:
$start is the starting number,$limit is the ending number (for one page).. $count is total number of records
echo "<h5>Showing ".$start." to ".($start+$limit)." Records of ".$count." Records</h5>";
if($start<=($count-$limit))
{
echo '<a style="float:right" href="'.$_SERVER['PHP_SELF'].'?start='.($start+$limit).'&limit='.$limit"><t1>Next</t1></a>';
}
$prev = $start-$limit;
if ($prev >= 0)
{
echo '<a style="float:left" href="'.$_SERVER['PHP_SELF'].'?start='.$prev.'&limit='.$limit"><t2>Previous</t2></a>';
}
$i=0;
$l=1;
echo "<p align='center'>";
for($i=0;$i < $count;$i=$i+$limit)
{
if($i <> $start)
{
echo "<a href='listing_test.php?start=$i&limit=$limit'><font face='Verdana' size='2'><b> $l </b></font></a> ";
}
else
{
echo "<font face='Verdana' size='4' color=#2E9AFE ><b> $l </b></font>";
}
$l=$l+1;
}
echo "</p>";
EDIT:
$result = mysql_query("SELECT * FROM table_name LIMIT ".$start.",".$limit);
$total=mysql_query("SELECT * FROM table_name");
$count=mysql_num_rows($total);