Reputation: 61
I am able to pass the server name to PHP which runs the query successfully. In the html file I want the rating option to change depending on what value was returned from the PHP file. In my file I set it to D but I need to change that to relflect what is being returned from PHP.
server.html
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
var id = $('#existingserver').val();
$('#assetCenter').click(function(){
var id = $('#textfield').val();
$.get('servertest.php',{q:id}, function(htmlData){
$('#txtHint').html(htmlData);
var rating = $(htmlData).find("td[data-col=rating]").text();
alert(rating);
});
});
});
</script>
</head>
<body>
<form>
<label>Existing Server</label><input type="text" name="existingserver" id="textfield" maxlength="15"/>
<input type="checkbox" id="assetCenter" >Select to Pull Asset Center Data<br>
<br />
<br />
Rating
<select name="rating" id="rating" >
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
</form>
<br />
<div id="txtHint"><b>Server info will be listed here.</b></div>
</body>
</html>
servertest.php
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'assignip', 'assignip');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ipreservation", $con);
$sql="SELECT * FROM acdata WHERE servername = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Servername</th>
<th>Contact</th>
<th>Classification</th>
<th>Rating</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['servername'] . "</td>";
echo "<td>" . $row['contact'] . "</td>";
echo "<td>" . $row['classification'] . "</td>";
echo "<td>" . $row['rating'] . "</td>";
echo "</tr>";
echo "<td data-col='rating'>" . $row['rating'] . "</td>";
}
echo "</table>";
mysql_close($con);
?>
database fields: servername, contact, classification, rating Data: Server1, Ray, Production, A
Upvotes: 0
Views: 1073
Reputation: 102743
The short answer is to use a JQuery selector to get the rating:
// get the text of the 4th td
var rating = $(htmlData).find("td").eq(3).text();
$("#rating").val(rating);
However, you might notice this approach is kind of brittle (aka tightly coupled) -- if something changes in the UI, say you re-order the columns, then the above logic will break.
I'd suggest returning the data from the server as JSON, and then applying a client-side template to get the HTML table. At the very least, give the columns a name like this:
echo "<td data-col='rating'>" . $row['rating'] . "</td>";
And then you can select on the client side by referencing the name:
var rating = $(htmlData).find("td[data-col=rating]").text();
Upvotes: 1
Reputation: 3830
In your PHP code, change the output so it displays the value you want to show, without any html tags.
Next in your html code, change your $(document).ready callback so that 'D' is replaced with the response text of the call (that is what PHP will return).
Upvotes: 1