我需要能够输入以下任何类型的信息 到一个文本字段。
到目前为止它的工作原理是我有5个不同的PHP文件 每种类型的信息都要输入一个。
如果我不断更改函数requestCustomerInfo(),这适用于每个。
我正在使用Ajax XHR请求 - 下面的工作示例: 这是在 index.php 页面中。
function handleHttpResponse() {
if (http.readyState == 4) {
if(http.status==200) {
var results=http.responseText;
document.getElementById('divCustomerInfo').innerHTML = results;
}
}
}
function requestCustomerInfo() {
//look up info by customer name
var url = "GetCustomerName.php?name="; // The server-side script
var sName = document.getElementById("CustomerId").value;
http.open("GET", url + escape(sName), true);
//look up info by customer email
//var url = "GetCustomerEmail.php?email="; // The server-side script
//var sEmail = document.getElementById("CustomerId").value;
//http.open("GET", url + escape(sEmail), true);
//look up info by customer phone number
//var url = "GetCustomerPhone.php?phone="; // The server-side script
//var sPhone = document.getElementById("CustomerId").value;
//http.open("GET", url + escape(sPhone), true);
//look up info by customer product serial number
//var url = "GetProductSerialNumber.php?serial="; // The server-side script
//var sSerial = document.getElementById("CustomerId").value;
//http.open("GET", url + escape(sSerial), true);
//look up info by customer product service ticket number
//var url = "GetServiceTicket.php?ticket="; // The server-side script
//var sTicket = document.getElementById("CustomerId").value;
//http.open("GET", url + escape(sTicket), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
function getHTTPObject() {
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
if (!xmlhttp){
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
}
return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object
//This is the look up by customer name PHP file: - typical of the 5 php files.
<?php
//customer ID
$sName = $_GET["name"];
$q= $_GET["q"];
//variable to hold customer info
$sInfo = "";
//database information
$sDBServer = "localhost";
$sDBName = "customers";
$sDBUsername = "root";
$sDBPassword = "root";
//create the SQL query string
$sQuery = "SELECT c.customerId, c.name, c.address, c.city, c.state, c.zip, c.phone, c.email, t.ticket
FROM customers c
left join serviceTicket t
on c.customerId = t.customerId
WHERE name = '$sName' ";
// OR c.email = '$sEmail'
// OR c.phone = '$sPhone'
// OR t.ticket = '$sTicket'
// OR t.serial = '$sSerial'";
//make the database connection
$oLink = mysql_connect($sDBServer,$sDBUsername,$sDBPassword);
@mysql_select_db($sDBName) or $sInfo = "Unable to open database";
if($sInfo == '') {
$bg = ($bg=='#ffffff' ? '#FCFCFC' : '#ffffff');
if($oResult = mysql_query($sQuery) and mysql_num_rows($oResult) > 0) {
$aValues = mysql_fetch_array($oResult,MYSQL_ASSOC);
$sInfo = "Customer account number: ".$aValues['customerId']."<br />".
"Customer name: ".$aValues['name']."<br />".
"Address: ".$aValues['address']."<br />".
"City: ".$aValues['city']."<br />".
"State: ".$aValues['state']."<br />".
"Postal code: ".$aValues['zip']."<br />".
"Phone: ".$aValues['phone']."<br /><br />".
"<a href=\"mailto:".$aValues['email']."\">".$aValues['email']."</a><br/><br/>";
} else {
$sInfo = "Customer with Name $sName doesn't exist.";
}
}
?>
<div id="divInfoToReturn"> <?php echo $sInfo ?> </div> //display above info
<?php
$cid = $aValues['customerId'].'<br/>'; //customerId from above
//select list dropdown for selecting a particular customer product.
$ticketquery = "Select * from serviceTicket where customerId = ".$aValues['customerId']."";
$srvcticket = mysql_query($ticketquery);
echo '<span class="selecttickettext">';
echo '<select name="tickets" onchange="showUser(this.value)" id="select">';
echo '<option selected ="">Select a Customer Product and Service Ticket</option>';
echo "<option>------------------------------------</option>";
echo '</span>';
while ($ticketrow = mysql_fetch_array($srvcticket)){
$id = $ticketrow['id'];
$ticket = $ticketrow['ticket'];
$product = $ticketrow['product'];
$serial = $ticketrow['serial'];
$model = $ticketrow['model'];
$computer = $ticketrow['computer'];
$os = $ticketrow['os'];
$issue = $ticketrow['issue'];
$docs = $ticketrow['docs'];
$cso = $ticketrow['cso'];
echo "<option value='$id'> "." $product "." $ticket";
};
echo "</select>\n";
mysql_close($oLink);
//create the SQL query string
$pQuery = "Select * from serviceTicket where customerId = '$cid'";
//make the database connection
$oLink = mysql_connect($sDBServer,$sDBUsername,$sDBPassword);
@mysql_select_db($sDBName) or $pInfo = "Unable to open database";
if($pInfo == '') {
if($oResult = mysql_query($pQuery) and mysql_num_rows($oResult) > 0) {
$aValues = mysql_fetch_array($oResult,MYSQL_ASSOC);
$pInfo = "Service ticket:" . " " .$aValues['ticket']."<br />".
"Product:" . " " .$aValues['product']."<br />".
"Serial number:" . " " .$aValues['serial']."<br />".
"Product model:" . " " .$aValues['model']."<br />".
"Computer brand:" . " " .$aValues['computer']."<br />".
"Operating OS:" . " " .$aValues['os']."<br />".
"Printer issue:" . " " .$aValues['issue']."<br />".
"KB docs:" . " " .$aValues['docs']."<br />".
"Customer service order:" . " " .$aValues['cso']."<br />";
} else {
$pInfo = "Customer product for $sName not found.";
}
}
?>
这是选择用户js文件: 用于传递信息以获取用户的选择下拉列表。
// JavaScript Document
var xmlhttp;
function showUser(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="getuser.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
这是 getuser js 文件: 用于填充生产信息部分。
<?php
//database information
$sDBServer = "localhost";
$sDBName = "customers";
$sDBUsername = "root";
$sDBPassword = "root";
$q=$_GET["q"];
$con = mysql_connect($sDBServer, $sDBUsername, $sDBPassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db( $sDBName );
$sql="SELECT * FROM serviceTicket WHERE id = '".$q."'";
//$sql="SELECT * FROM serviceTicket WHERE id = 3";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo '<div class="productInfo">Product Information </div>';
echo "Product : " . $row['product'].'<br/>' ;
echo "Serial number : " . $row['serial'] .'<br/>';
echo "Model number : " . $row['model'].'<br/>' ;
echo "Computer brand : " . $row['computer'] .'<br/>';
echo "Operating system : " . $row['os'].'<br/>';
echo '<div class="productIssue">Issues and Resolution documents </div>';
echo "Product issues" .'<br/>'. $row['issue'].'<br/>';
echo "Knowledge base docs : " . $row['docs'] .'<br/>';
echo "Customer service order : " . $row['cso'].'<br/>';
}
mysql_close($con);
?>
以上所有内容都有效,但我希望能够查找一个字段 客户信息。
到目前为止工作版: http://www.stevenjsteele.com/ajaxmysqljquery/
任何帮助将不胜感激。
答案 0 :(得分:0)
某种正则表达怎么样?您将测试某个表达式是否为真。
你可以使用很多预先制作的表达方式。
电子邮件你可以谷歌一个, 电话号码取决于他们将如何输入。 (10位数或xxx-xxx-xxxx) 如果它是固定长度的串行,但有数字和字母 名字和姓氏将是两个字母或两个字母之间的空格 票号也可以是固定长度的号码。
您可以检查并查看它匹配哪一个,然后将其传递给正确的文件。