Table row populated by drop down selection - php

i have a drop down list populated from a database(it is the first cell in a table row) then based on that selection i want it to populate the rest of the row from the same database, each cell is a different table in the database, so far i only have it populating the first cell (calories), i can't figure out how to get it to populate the rest of the cells, thanks
<?php
$link=mysqli_connect("localhost", "root", "");
mysqli_select_db($link,"meal_planner");
?>
<!DOCTYPE html>
<html>
<head>
<title>Meal Planner</title>
<link href="main2.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($){
// Hide all panels to start
var panels = $('.accordion > dd').hide();
// Show first panel on load (optional). Remove if you want
panels.first().show();
// On click of panel title
$('.accordion > dt > a').click(function() {
var $this = $(this);
// Slide up all other panels
panels.slideUp();
//Slide down target panel
$this.parent().next().slideDown();
return false;
});
});
</script>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<dl class="accordion">
<dt>Monday</dt>
<dd>
<table>
<tr>
<th>Breakfast</th>
<th>Calories</th>
<th>Protein</th>
<th>Carbs</th>
<th>Fat</th>
<th>Serving Sizing</th>
</tr>
<tr>
<td>
<select name="breakfa"s onchange="showUser(this.value)">
<option value="">Select a Protein:</option>
<?php
$res=mysqli_query($link,"select name, protein_id from protein");
while($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row["protein_id"]; ?>"><?php echo $row["name"]; ?></option>
<?php
}
?>
</select>
</td>
<td><div id="txtHint"><b>0</b></div></td>
<td>50</td>
<td>50</td>
<td>50</td>
<td><input type="" name=""></td>
</tr>
</table>
</dd>
<dt>Tuesday</dt>
<dd>TEST</dd>
<dt>Wednesday</dt>
<dd>TEST.</dd>
</dl>
</body>
</html>
php
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','','meal_planner');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM protein WHERE protein_id = '".$q."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['calories'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

Related

PHP Save as PDF not working

Good Day!
I have a dropdown menu which select employee number and shows table data on basis of that particular selection. I have save as pdf code which saves table data in pdf. It is working fine but my table column headers are only shown not the values.
I have dropdown in one file having javascript which is sent to another file where my table is shown.
Below is the code for my all files and what I have tried for this so far.
For Dropdown:
<?php
include("connect-db.php");
?>
<html>
<head>
<title>PHP insertion</title>
<link rel="stylesheet" href="css/insert.css" />
<link rel="stylesheet" href="css/navcss.css" />
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","search_empnumber.php?emp_id="+str,true);
xmlhttp.send();
}
}
/*function showAssessories(acc) {
if (acc == "") {
document.getElementById("txtHint2").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint2").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","getassessories.php?emp_id="+acc,true);
xmlhttp.send();
}
}*/
</script>
</head>
<body>
<div class="maindiv">
<?php include("includes/head.php");?>
<?php include("menu.php");?>
<div class="form_div">
<form>
<div class="forscreen">
<h1>Search Records:</h1>
<p>You may search by Employee Number</p>
<select name="employeenumber" class="input" onchange="showUser(this.value)">
<option value="">Please Select Employee Number:</option>
</div>
<?php
$select_emp="SELECT distinct(emp_number) FROM employees";
$run_emp=mysql_query($select_emp);
while($row=mysql_fetch_array($run_emp)){
$emp_id=$row['emp_id'];
$first_name=$row['first_name'];
$last_name=$row['last_name'];
$emp_number=$row['emp_number'];
$total_printers=$row['total_printers'];
$total_scanners=$row['total_scanners'];
$total_pc=$row['total_pc'];
$total_phones=$row['total_phones'];
$other_equips=$row['other_equips'];
?>
<option value="<?php echo $emp_number;?>"><?php echo $emp_number;?></option>
<?php } ?>
</select>
<html>
</form>
</div>
<br>
<div id="txtHint"></div>
<br>
<div id="txtHint2"></div>
</div>
<?php include 'includes/foot.php';?>
</body>
</html>
The function for dropdown: onchange="showUser(this.value)"
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","search_empnumber.php?emp_id="+str,true);
xmlhttp.send();
}
}
search_empnumber.php File:
<!DOCTYPE html>
<html>
<form action="search_empnumber.php" method="POST">
<input type="submit" value="SAVE AS PDF" class="btnPDF" name="submit" />
</form>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
.link{ color:#00F; cursor:pointer;}
</style>
</head>
<body>
<?php
//$emp_id =($_REQUEST['emp_id']);
$emp_id =($_REQUEST['emp_id']);
$count=1;
include("connect-db.php");
//$sql = "SELECT * from employees where department='".$department."'";
//$sql = "select * from employees as pg inner join accessories as pd on pg.emp_number= pd.emp_number ";
//$result = mysql_query($sql) or die(mysql_error());
//$result = mysql_query($query) or die(mysql_error());
//$emp_id =($_POST['employeenumber']);
$sql="SELECT * FROM employees WHERE emp_number = '".$emp_id."'";
$result = mysql_query($sql) or die(mysql_error());
$count=1;
echo "<table>
<tr>
<th>S.No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Employee Number</th>
<th>Department</th>
<th>Email</th>
<th>Total Printers</th>
<th>Total Scanners</th>
<th>Total Pc</th>
<th>Total Phones</th>
<th>Other Equipments</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $count . "</td>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "<td>" . $row['emp_number'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['total_printers'] . "</td>";
echo "<td>" . $row['total_scanners'] . "</td>";
echo "<td>" . $row['total_pc'] . "</td>";
echo "<td>" . $row['total_phones'] . "</td>";
echo "<td>" . $row['other_equips'] . "</td>";
echo "</tr>";
$count++;
}
echo "</table>";
echo '<div class="forscreen">';
// echo '<br />';echo '<button class="btnPrint" type="submit" value="Submit" onclick="window.print()">Print</button>';
//echo '<input type="submit" value="SAVE AS PDF" class="btnPDFsearch" name="submit"/>';
// echo '<input type="submit" value="SAVE AS PDF" class="btnPDF" name="submit" />';
echo '</div>';
echo '<div class="forscreen">';
//echo '<br />';echo '<button class="btnPrevious" type="submit" value="Submit" onclick="history.back(); return false;">Previous Page</button>';
echo '</div>';
?>
</body>
</html>
PDF code (I tried) in same file of search_empnumber:
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
include('connect-db.php');
?>
<?php
//if($_POST['submit']){
if (isset($_POST['submit'])){
require_once 'includes/practice.php';
$pdf->SetFont('times', '', 11);
$tb1 = '</br></br>
<table cellspacing="0" cellpadding="5" border="1">
<tr style="background-color:#f7f7f7;color:#333; ">
<td width="60">First Name</td>
<td width="60">Last Name</td>
<td width="80">Employee Number</td>
<td width="80">Department</td>
<td width="60">Email</td>
<td width="80">Total Printers</td>
<td width="80">Total Scanners</td>
<td width="60">Total PCs</td>
<td width="60">Total Phones</td>
<td width="60">Other Equipments</td>
</tr>
</table>';
include('connect-db.php');
//$emp_id =($_POST['emp_id']);
$emp_id =($_POST['employeenumber']);
$result1= mysql_query("SELECT * FROM employees where emp_number = '".$emp_id."'");
while($row = mysql_fetch_assoc($result1)){
$tb2 = '<table cellspacing="0" cellpadding="5" border="1">
<tr>
<td width="60">' . $row['first_name'] . '</td>
<td width="60">' . $row['last_name'] . '</td>
<td width="80">'. $row['emp_number'] .'</td>
<td width="80">'.$row['department'].'</td>
<td width="60">'.$row['email'].'</td>
<td width="80">'.$row['total_printers'].'</td>
<td width="80">'.$row['total_scanners'].'</td>
<td width="60">'.$row['total_pc'].'</td>
<td width="60">'.$row['total_phones'].'</td>
<td width="60">'.$row['other_equips'].'</td>
</tr>
</table>';
$tb1 = $tb1.$tb2;
}
$pdf->writeHTML($tb1, true, false, false, false, '');
ob_end_clean();
$pdf->Output('Report.pdf', 'D');
}
?>
The files i.e. practoce.php and pdf libraries are not added becuase they are just creating tables in defined formats
You are mixing up GET and POST. Try one (or both) of the following:
1) In the dropdown code, replace <form> with:
<form method="POST">
2) In the table generation code, read the value back with:
$emp_id =($_REQUEST['employeenumber']);
and also:
if (isset($_REQUEST['submit'])){
To add to your problems, in the first version of search_empnumber.php you posted, you read back $emp_id =($_REQUEST['emp_id']); when the posted value will actually be in $_REQUEST['employeenumber'].
I don't know which part is going wrong for you, but there's some strange things going on in that dropdown code:
<select name="employeenumber" class="input" onchange="showUser(this.value)">
<option value="">Please Select Employee Number:</option>
</div> <!-- ISSUE: Closed a div which was never opened. You're lucky the select box even shows anything-->
<?php
$select_emp="SELECT distinct(emp_number) FROM employees";
$run_emp=mysql_query($select_emp);
//ISSUE: You loop through a lot of data, but then do nothing with it. With every pass of the loop, you simply change the variables, and nothing else. I think you meant to move your <option> into the loop
while($row=mysql_fetch_array($run_emp)){
//ISSUE: You only selected 'emp_number' in your query. Most of these array keys are never filled
$emp_id=$row['emp_id'];
$first_name=$row['first_name'];
$last_name=$row['last_name'];
$emp_number=$row['emp_number']; //This is the only one filled
$total_printers=$row['total_printers'];
$total_scanners=$row['total_scanners'];
$total_pc=$row['total_pc'];
$total_phones=$row['total_phones'];
$other_equips=$row['other_equips'];
?>
<!--ISSUE: Only the last $emp_number from the query is filled. The rest you kept overwriting in your loop-->
<option value="<?php echo $emp_number;?>"><?php echo $emp_number;?></option>
<?php } ?>
</select>

autocomplete input with database record

I want to add my PhpMyAdmin record into a autocomplete function when someone types a name or something it will show the value in the database to be selected. The values that has to be in my input field is from table address_state
my code:
<?php
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT person_id, person_firstname, person_lastname,
person_email, person_phonenumber,
address_street,address_housenumber,
address_city,address_state,address_zipcode,cv_path
FROM person
inner join address on address.address_id = person.person_address
inner join cv on cv.cv_id = person.person_cv
WHERE CONCAT(`person_firstname`, `person_lastname`, `address_street`, `address_housenumber`, `address_zipcode`, `address_city`, `address_state`, `person_email`, `person_phonenumber` )
LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT person_id, person_firstname, person_lastname,
person_email, person_phonenumber,
address_street,address_housenumber,
address_city,address_state,address_zipcode,cv_path
FROM person
inner join address on address.address_id = person.person_address
inner join cv on cv.cv_id = person.person_cv ";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "usbw", "persons");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Detail</title>
<style>
table,tr,th,td
{
border: 1px solid black;
}
</style>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form action="admin2.php" method="post" onchange="showUser(this.value)">
<input type="text" name="valueToSearch" placeholder="Hoi" style="margin-left: 50px;">
<input type="submit" name="search" value="Filter"><br><br>
<div id="txtHint">
<table>
<tr>
<th>Voornaam</th>
<th>Achternaam</th>
<th>Straat</th>
<th>Huisnummer</th>
<th>Postcode</th>
<th>Stad</th>
<th>Provincie</th>
<th>Email</th>
<th>Mobiel</th>
<th>cv</th>
<th>delete</th>
</tr>;
<!-- populate table from mysql database -->
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['person_firstname'];?></td>
<td><?php echo $row['person_lastname'];?></td>
<td><?php echo $row['address_street'];?></td>
<td><?php echo $row['address_housenumber'];?></td>
<td><?php echo $row['address_zipcode'];?></td>
<td><?php echo $row['address_city'];?></td>
<td><?php echo $row['address_state'];?></td>
<td><?php echo $row['person_email'];?></td>
<td><?php echo $row['person_phonenumber'];?></td>
<td><?php echo "<a href='http://localhost:8080/website/" . $row['cv_path'] . "'>cv file</a>";?></td>
<td><?php echo "<a href='delete.php?person_id=" . $row['person_id'] . "'>delete</a>";?></td>
</tr>
<?php endwhile;?>
</table>
</div>
</form>
</body>
</html>

Bootstrap Data Tables with MySQL content

I have a form in which a user select a reference of a candelabra.
When the choice done, a list of incidents relative to the candelabra appears in a bootstrap table.
The table is completed with items from a Mysql database.
All is ok. Now I'd like to add a button in another column called "test". But I have no buttons.. I put a screenshot of what I see..
My code is this one :
FORM FILE
<?php
require_once 'login.php';
$sql= "SELECT ptlum FROM ptlum where ptlum LIKE '%AZ%'";
$result = mysql_query($sql) or die("Requete pas comprise");
//while($data = mysql_fetch_array($result))
// {
//echo "<option>".$data[ptlum]."</option>";
// }
?>
<html>
<head>
<meta charset="utf-8">
<link href="examples.css" rel="stylesheet">
<link href="http://minikomi.github.io/Bootstrap-Form-Builder/assets/css/lib/bootstrap.min.css" rel="stylesheet">
<link href="http://minikomi.github.io/Bootstrap-Form-Builder/assets/css/lib/bootstrap-responsive.min.css" rel="stylesheet">
<link href="http://minikomi.github.io/Bootstrap-Form-Builder/assets/css/custom.css" rel="stylesheet">
<link href="bootstrap.table.css" rel="stylesheet">
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form class="form-horizontal" >
<legend>Rechercher une panne en cours ou archivée</legend>
<div class="control-group">
<label class="control-label" for="selectbasic-0">Sélectionner un point lumineux</label>
<div class="controls">
<select name="users" onchange="showUser(this.value)">
<?php
while ($row = mysql_fetch_array($result))
{
echo "<option>".$row[ptlum]."</option>";
}
?>
</select>
</div>
</div>
</form>
<script src= "jquery.js"></script>
<script src= "bootstrap.min.js"></script>
<script src= "bootstrap.table.js"></script>
<br>
<div id="txtHint"><b></b></div>
</body>
</html>
PHP FILE WITH CALLING THE DATABASE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="https://cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet">
</head>
<body>
<script src= "jquery.js"></script>
<script src= "https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script src= "https://cdn.datatables.net/1.10.9/js/dataTables.bootstrap.min.js"></script>
<?php
$q = $_GET['q'];
//echo $q;
$con = mysqli_connect('localhost','root','root','sdeer');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM depannages WHERE ptlum = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table id=\"example\" class=\"table table-striped table-bordered\" cellspacing=\"0\" width=\"100%\">
<tr>
<th>Pt Lum</th>
<th>Matériel</th>
<th>Prestation</th>
<th>Date</th>
<th>Nature</th>
<th>Test</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['ptlum'] . "</td>";
echo "<td>" . $row['materiel'] . "</td>";
echo "<td>" . $row['presta'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['nature'] . "</td>";
echo "</tr>";
echo "</tbody>";
}
echo "</table>";
mysqli_close($con);
?>
<script>
$(document).ready(function() {
var table = $('#example').DataTable( {
"columnDefs": [ {
"targets": -1,
"data": null,
"defaultContent": "<button>Click!</button>"
} ]
} );
</script>
</body>
</html>
How to make it if I want to have a button ? When the user will click on the button, a new window will appear.
Thanks !
You can make a anchor tag look like as button. Just you need to add role="button".
New Window Button
Edited Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="https://cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet">
<script src= "jquery.js"></script>
<script src= "https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script src= "https://cdn.datatables.net/1.10.9/js/dataTables.bootstrap.min.js"></script>
</head>
<body>
<?php
$q = $_GET['q'];
$con = mysqli_connect('localhost','root','root','sdeer');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM depannages WHERE ptlum = '$q'";
$result = mysqli_query($con,$sql);
?>
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Pt Lum</th>
<th>Matériel</th>
<th>Prestation</th>
<th>Date</th>
<th>Nature</th>
<th>Test</th>
</tr>
</thead>
<tbody>
<?
while($row = mysqli_fetch_array($result))
{?>
<tr>
<td><?echo $row['ptlum'];?></td>
<td><?echo $row['materiel'];?></td>
<td><?echo $row['presta'];?></td>
<td><?echo row['date'];?></td>
<td><?echo $row['nature'];?></td>
<td>
New Window Button
</td>
</tr>
<?}?>
<tbody>
</table>
<?mysqli_close($con);?>
<script>
$(document).ready(function() {
var table = $('#example').DataTable( {
"columnDefs": [ {
"targets": -1,
"data": null,
"defaultContent": "<button>Click!</button>"
} ]
} );
</script>
</body>
</html>

Execute PHP Mysql query on the basis of select option value

I have a select input having several options in it as html and I want to execute a query on the basis of a selected value in the dropdown list.
<select name="test">
<option value="apple">apple</option>
<option value="banana">banana</option>
<option value="mango">mango</option>
</select>
I want to execute the following query with a criterion value selected in the above drop down list.
<?php $sql=mysqli_query($con,"select * from tblsession where sessionid='".$_REQUEST['test']."'"); ?>
pls help.
Okay, in this same page your going to have to use some javascript so you can be capable of working on AJAX.
So here's the code for the first page:
<html>
<head>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("para").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("para").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
<select name="test" onchange="showUser(this.value)">
<option value="Apple">Apple</option>
<option value="Bannana">Bannana</option>
<option value="Bannana">Mango</option>
</select>
</form>
<br>
<div id="para"><b>What you will excute will be listed here...</b></div>
</body>
</html>
And this is getuser.php
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('host','username','password','db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"db");
$sql="SELECT * FROM test WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['apple'] . "</td>";
echo "<td>" . $row['bannana'] . "</td>";
echo "<td>" . $row['mango'] . "</td>";
}
mysqli_close($con);
?>
</body>
</html>

Show 'All Records' Option

I've put together the form below that allows a user to retrieve database records from the date they select from a drop down menu.
<html>
<head>
<script type="text/javascript">
function ajaxFunction(name)
{
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer")
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{// code for IE6, IE5
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("my_div").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getrecords.php?dateoftrip="+name,true);
xmlhttp.send();
}
function getquerystring() {
var form = document.forms['frm1'];
var word = form.word.value;
qstr = 'w=' + escape(word); // NOTE: no '?' before querystring
return qstr;
}
</script>
<style type="text/css">
<!--
.style1 {
font-family: Calibri;
font-size: 14px;
}
-->
</style>
</head>
<body>
<form action="getrecords.php" method="get" name="frm1">
<table width="148" border="0">
<tr>
<td width="152"><p class="style1">Select a date from below</p>
<div align="center">
<?php
include("db.php");
$query="select * from finds group by dateoftrip";
echo '<select onchange="ajaxFunction(this.value)">';
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<option name='name' value=".$rows['dateoftrip'].">".$rows['dateoftrip']."</option>";
}
echo "</select>";
?>
</div></td>
</tr>
</table>
</form>
<div id="my_div"></div>
</body>
</html>
and this is the php script which retrieves the records.
<?php
include("db.php");
$dateoftrip= $_GET['dateoftrip'];
$findname= $_GET['findname'];
$finddescription= $_GET['finddescription'];
$query="select * from finds where dateoftrip='$dateoftrip'";
echo "<table>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>Find Name : </td>";
echo "<td>".$rows['findname']."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Find Description : </td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
?>
I'd like to extend the functionality a little if at all possible, by adding a 'All Records' option to the drop down menu which obviously returns all the records for the current user.
I've been searching for this for a few days now, and I haven't found an example where there is this added functionality.
I just wondered whether someone could perhaps provide some guidance on how I may go abut this please.
Amended Form Code
<html>
<head>
<script type="text/javascript">
function ajaxFunction(name)
{
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer")
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{// code for IE6, IE5
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("my_div").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getrecords.php?dateoftrip="+name,true);
xmlhttp.send();
}
function getquerystring() {
var form = document.forms['frm1'];
var word = form.word.value;
qstr = 'w=' + escape(word); // NOTE: no '?' before querystring
return qstr;
}
</script>
<style type="text/css">
<!--
.style1 {
font-family: Calibri;
font-size: 14px;
}
-->
</style>
</head>
<body>
<form action="getrecords.php" method="get" name="frm1">
<table width="148" border="0">
<tr>
<td width="152"><p class="style1">Select a date from below</p>
<div align="center">
<?php
include("db.php");
$query="select * from finds group by dateoftrip";
echo '<select onchange="ajaxFunction(this.value)"><OPTION name="name" value="ALLRECORDS">';
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<option name='name' value=".$rows['dateoftrip'].">".$rows['dateoftrip']."</option>";
}
echo "</select>";
?>
</div></td>
</tr>
</table>
</form>
<div id="my_div"></div>
</body>
</html>
Create an option for the 'ALL' thing
echo '<select onchange="ajaxFunction(this.value)"><OPTION name="name" value="ALLRECORDS">All Records</option>';
Handle your query for the 'ALL' thing
if ($dateoftrip=="ALLRECORDS") {
$query="select * from finds";
} else {
$query="select * from finds where dateoftrip='$dateoftrip'";
}
On a side note you should look about SQL Injection, not using the '*' selector but typing the explicit name of attributes you search

Categories