Hi im having trouble with a page I am working on.
Basically this page is a simple form that asks for a voucher number, and when submitted returns the data for that voucher from the database.
What I want to do is make it so in the text box that you enter the voucher number, you have the choice of entering a number, OR the persons name. ie: so it will search for either field and return the same results.
How do I go about it?? I thought it would be a case of just adding
OR voucherID = 'issuedName'
to my sql select query, but that does not work...
Any ideas??
Here is my code for the 2 pages...
findVoucher.php
<!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=iso-8859-1" />
<title>Voucher Management</title>
<link rel="stylesheet" type="text/css" href="../styles/main.css" />
</head>
<body>
<div id="main">
<h1 style="text-align:center">Voucher Management</h1>
<p align="right"><a href="../logout.php">Logout</a> </p>
<div style="border: 1px dashed black; padding: 1.5em">
<h3>Lookup Voucher Details </h3>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var voucherID = document.getElementById('voucherID').value;
var queryString = "?voucherID=" + voucherID;
ajaxRequest.open("GET", "ajax-lookup.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
<form name='myForm'>
<table width="60%">
<tr>
<td width="30%"><p>Voucher Number/Name:</p></td>
<td width="40%"><input type='text' id='voucherID' /></td>
<td width="30%"><p class="instruction">Enter the voucher number that you want to look up</p></td>
</tr>
<tr>
<td></td>
<td><input type='button' onclick='ajaxFunction()' value='Lookup Voucher' /></td>
<td><p class="instruction">Click here to search for the voucher number you have entered</p></td>
</tr>
</table>
</form>
<div id='ajaxDiv'>Your result will display here</div>
</div>
<p align="left"><a href="index.php">Back to Voucher Menu</a> </p>
</div>
</body>
</html>
and
ajax-lookup.php
<?php
$dbhost = "localhost";
$dbuser = "un";
$dbpass = "pwd";
$dbname = "bookingsdb";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String
$voucherID = $_GET['voucherID'];
// Escape User Input to help prevent SQL Injection
$voucherID = mysql_real_escape_string($voucherID);
//build query
$query = "SELECT * FROM vouchers WHERE voucherID = '$voucherID' [COLOR="Blue"]OR voucherID = '$issuedName'[/COLOR]";
//Execute query
$qry_result = mysql_query($query) or die(mysql_error());
//Build Result String
$display_string = "<table border='1px' padding='5px'>";
$display_string .= "<tr>";
$display_string .= "<th> Voucher # </th>";
$display_string .= "<th> Name </th>";
$display_string .= "<th> Date Issued </th>";
$display_string .= "<th> Expiry Date </th>";
$display_string .= "<th> Conditions </th>";
$display_string .= "<th> Issued By </th>";
$display_string .= "<th> Redeemed (Y/N) </th>";
$display_string .= "</tr>";
// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
$display_string .= "<tr>";
$display_string .= "<td> $row[voucherID] </td>";
$display_string .= "<td> $row[issuedName] </td>";
$display_string .= "<td> $row[issuedDate] </td>";
$display_string .= "<td> $row[expiryDate] </td>";
$display_string .= "<td> $row[conditions] </td>";
$display_string .= "<td> $row[issuedBy] </td>";
$display_string .= "<td> $row[redeemed] </td>";
$display_string .= "</tr>";
}
$display_string .= "</table>";
echo $display_string;
?>