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
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).
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();
Related
At the moment, I can use a drop down list to retrieve data which is presented on a table. I want to make every row of a table clickable. Once I have clicked on that particular row, I want the data of that row (id, age, state, city, healthcare, team, cause, planned date, from and AAA_diam) to be displayed in a new php page. It should look like this and I hope that I'm being clear enough.
Display of information in a new PHP page once I've clicked on a row of a table.
Id: $id (from the table row)
age: $age
city: $city
healthcare: $healthcare
team: $team
cause: $cause
planned date: $planned date
from: $from
AAA_diam: $AAA_diam
<html>
<head>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<form name="Select_filter3" method="POST" action="index.php">
<select id="dropdown3" name="filter3">
<option value=""></option>
<option value="1">Amount of present</option>
<option value="2">Amount of absent</option>
<option value="3">Amount of present: destination</option>
<option value="4">Antal of absent: destination</option>
<option value="5">Amount A_diam > 3cm</option>
<option value="6">Amount A_diam > 5cm</option>
</select>
<input id="search_box2" type="text" name="search_box3" value="" />
<input id="submit2" type ="submit" name ="search3" value ="Ok">
</body>
</html>
<?php
include "connection1.php";
mysql_query ('SET NAMES UTF8;');
mysql_query ('SET COLLATION_CONNECTION=utf8_general_ci;');
mysql_client_encoding($connect);// where $conn is your connection
if (isset($_POST['search3'])) {
switch($_POST['filter3']) {
Default:
$sql = " ";
break;
case 1:
$sql = "SELECT * FROM mytable WHERE age = '35' AND city != 'Los Angeles' ";
echo "<table border='1'>
<tr>
<th>ID</th>
<th>age</th>
<th>state</th>
<th>city</th>
<th>healthcare</th>
<th>Team</th>
<th>cause</th>
<th>Planned date</th>
<th>From</th>
<th>diameter</th>
</tr>";
$query = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['state'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['healthcare'] . "</td>";
echo "<td>" . $row['Team'] . "</td>";
echo "<td>" . $row['cause'] . "</td>";
echo "<td>" . $row[’planned date’] . "</td>";
echo "<td>" . $row['from'] . "</td>";
echo "<td>" . $row['AAA_diam'] . "</td>";
echo "</tr>";
}
echo "</table>";
break;
mysql_close($connect);
?>
you can handle the click event of row using jQuery like
$(document).ready(function(){
$("#your_table_id").delegate("tr.rows", "click", function(){
/*find the clicked row id column and redirect to user for that record
some thing like this http://www.yoursite.com/showrecord.php?id=5
using location.href='http://www.yoursite.com/showrecord.php?id=5';
*/
});
});
Thanks in advance for any light shed.
I have a mysql database consisting of customers with some fields pertaining to each customer. currently running on one of my lamp servers. There is security risks with my code at the moment, but I plan to get the functionality i'm looking for and then reconfigure the code for a tighter security. At the moment I have an html index file that calls on php script to search mysql database by firstname or lastname. Upon this query it displays a list of users and allows me to modify the user. When I click modify it pulls the correct customer id number, but it is not displaying any current information, nor allowing me to update the info.
To summarize, I would like to search a customer, and it pull up selected fields and show the content and allow me to actively change the data and resend it to the database.
My search.html code:
<html>
<body>
<form action="scripts/search.php" method="post">
Firstname: <input type="text" name="firstname">
<input type="submit">
</form>
<form action="scripts/lastnamesearch.php" method="post">
Lastname: <input type="text" name="lastname">
<input type="submit">
</form>
<form action="scripts/phonenumbersearch.php" method="post">
Phone Number: <input type="text" name="phone">
<input type="submit">
</form>
</body>
</html>
MY search.PHP Script:
//this script allows me to search the database by filling out one of the forms and clicking submit. Each of the forms calls upon it's own individual script, I realize that this is probably cumbersome, due to my lack of coding knowledge.
<?php
$con=mysqli_connect("localhost","root","*****","*******");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM customers WHERE `firstname` LIKE '$_POST[firstname]'");
echo "<table border='1'>
<tr>
<th>id</th>
<th>firstname</th>
<th>lastname</th>
<th>phone</th>
<th>address</th>
<th>notes</th>
<th>additional notes</th>
<th>passwords</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" . $row['addnotes'] . "</td>";
echo "<td>" . $row['passwords'] . "</td>";
echo "Modify User";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
My modify.php script:
//this is where I believe one of my problems lie. when I click modify user on the search.php script it calls on this script and it loads the correct user/customer id in the address bar, but it doesn't show any existing data, nor does it update the data that I fill in the cells.
<?php
$con=mysqli_connect("localhost","root","crapola1","Computition");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$mysqli_query = "SELECT * FROM customers WHERE ID = $_get[id]";
$mysqli_result = mysqli_query($mysqli_query);
$customer = mysqli_fetch_array($mysqli_result);
?>
<h1> You are modifying a user</h1>
<form action="<?php echo $SERVER['PHP_SELF']; ?>" method="post">
Firstname<input type="text" name="inputFirstname" value="<?php echo $row['firstname']; ?>" /><br />
Notes<input type="text" name="inputNotes" value="<?php echo $row['notes']; ?>" />
<br />
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Modify" />
</form>
Thanks again,
I've been searching on this topic for about a week now and have pieced together this much, but can't seem to get over this "hump"
$_GET is a super global array . It should be in UPPERCASE.
Change the query on your modify.php here
SELECT * FROM customers WHERE ID = $_get[id] to upper case.
Must be..
SELECT * FROM customers WHERE ID = ".$_GET['id']
Also, It is strictly not advised to pass the $_GET or $_POST parameters directly to your query as it leads to SQL injection. You need to switch over to PreparedStatements
Is it possible to fetch data from db upon changing value of my drop down, I want to pull up the data that corresponds to the value of the drop down and show it to a text area, I got the codes that I've tried to create just don't know how to make it work to what I want.
I want my page to just initially show the drop down then after selecting a value it will show the data in a text area without refreshing the page, here is the code:
<div id="mainContent">
<table width="619" border="0" align="center">
<td align="center"><form id="form1" name="form1" method="post" action="" >
<fieldset>
<legend><strong>EA</strong></legend>
<p>
<select name="ea_name" id="ea_name">
<option value="" selected="selected">Please select...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</p>
</fieldset>
</form></td>
</table>
<div id="results"></div>
</div>
I am thinking an "onchange" could do it but just dont know how to implement it, also want to echo the resulting data to a text area within a fieldset.
Here is the code that I've tried that will pull up data and show to a text area:
<?php
require 'include/DB_Open.php';
$ea_name = $_POST['ea_name'];
$sql="SELECT * FROM ea_error WHERE ea_name = '" . $ea_name . "'";
$myData = mysql_query($sql);
//to count if there are any results
$numrow = mysql_num_rows($myData);
if($numrow == 0)
{
echo "No results found.";
}
else
{
echo "<fieldset><legend><strong>Information</strong></legend><p>
<table width="auto" height="172" border="0">
<tr><th scope="row">Error</th></tr>
<tr><th scope="row">Resolution</th></tr>
<tr><th scope="row">Contact/s</th></tr>;"
while($info = mysql_fetch_array($myData))
{
echo "<form action='retrieve.php' method='post'>";
echo"<tr>";
echo "<td align='center'>" . $info['error'] . "<input type=hidden name=error value=" . $info['error'] . " </td>";
echo "<td align='center'>" . $info['resolution'] . "<input type=hidden name=resolution value=" . $info['resolution'] . " size='11' maxlength='11' /> </td>";
echo "<td align='center'>" . $info['contacts'] . "<input type=hidden name=contacts value=" . $info['contacts'] . "' /> </td>";
echo "</tr>";
echo "</form>";
}
}
echo "</fieldset>";
include 'include/DB_Close.php';
?>
UPDATED CODE:
<?php
require 'include/DB_Open.php';
$ea_name = $_POST['ea_name'];
$sql="SELECT * FROM ea_error WHERE ea_name = '" . $ea_name . "'";
$myData = mysql_query($sql);
//to count if there are any results
$numrow = mysql_num_rows($myData);
if($numrow == 0)
{
echo "No results found.";
}
else
{
echo '<fieldset><legend><strong>Information</strong></legend><p>
<table width="auto" height="172" border="0">
<tr><th>Error</th></tr>
<tr><th>Resolution</th></tr>
<tr><th>Contact/s</th></tr>';
while($info = mysql_fetch_array($myData))
{
echo "<form action='retrieve.php' method='post'>";
echo"<tr>";
echo "<td align='center'>" . $info['error'] . "<input type=hidden name=error value=" . $info['error'] . " </td>";
echo "<td align='center'>" . $info['resolution'] . "<input type=hidden name=resolution value=" . $info['resolution'] . " size='11' maxlength='11' /> </td>";
echo "<td align='center'>" . $info['contacts'] . "<input type=hidden name=contacts value=" . $info['contacts'] . "' /> </td>";
echo "</tr>";
echo "</form>";
}
}
echo "</fieldset>";
include 'include/DB_Close.php';
?>
This should do the trick. Just put this javascript either in script tags in the head of your page, after the jQuery include, or in another js file and include that after jQuery...
$(function() { // document.ready
$("#ea_name").on("change", function() {
$.ajax({
url: "phpfile.php",
type: "POST",
data: {
ea_name: $(this).val()
},
success: function(data) {
$("#results").html(data);
}
});
});
});
It assigns an event handler to the change event of the drop down. When this is triggered it sends an ajax request to your php file (don't forget to put the correct filename for the url!) which then returns the html. This is then pushed into the results div.
Note: I fixed a typo that may or may not be in your php file. At the end of the line where you create the query, you'd missed a closing ". Just in case that was a copy and paste from the real file.
You have to implement ajax call to php file with ea_name as post parameter file. and place response to specific div.
I have a table populated from a mysql database. One of the fields is "status". I would like this cell to be a drop down box inside the table, so I can then update the particular field.
This code, correctly displays the table and currently it displays the "status" filed inside a text box that I can edit successfully. I would like this to be a drop down though.
<?php
require_once('db_connect.php');
$result = mysql_query("SELECT *
FROM queries
WHERE SR = '$_GET[SR]'
")
or die(mysql_error());
echo '<form name="Form" action="update.php" method="post">';
echo
"<table id='box-table-b'>
<thead>
<tr>
<th>SR</th>
<th>Product</th>
<th>Status</th>
</tr>
</thead>";
while($row = mysql_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['SR'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . '<input type="text" name="status" value="'.$row['status'].'" />' . "</td>";
echo "</tr>";
echo "</tbody>";
}
echo "</table>";
echo '<input type="submit" name="Save" value="Save" />';
echo '</form>';
?>
Can someone please show me how to do this ?
To answer the question, you should use the <select></select> tags. Example:
<select>
<option>Item 1</option>
<option>Item 2</option>
<option> ... </option>
<option selected="selected">Item N</option>
</select>
In this specific example, the dropdown would appear with "Item N" selected by default.
As a side note, isn't using mysql_* functions bad practice in general?
by drop down menu I guess you mean a select tag, anything more complicated that this requires a custom implementation.
This requires 2 steps: first you need to create the select tag and populate it with option tags, then you need to set the desired value as selected.
to create the select tag:
$myselect="<select id='status' name='status'>";
foreach($status_values as $e){
$myselect.="<option value='$e'>$e</option>";
}
$myselect.="</select>";
$status_value is a array, you can have in your code or get it from a query.
To select the correct one you can add the following if to the code above:
$myselect="<select id='status' name='status'>";
foreach($status_values as $e){
if($e == $row['status']){
$myselect.="<option value='$e' SELECTED>$e</option>";
}else
$myselect.="<option value='$e'>$e</option>";
}
}
$myselect.="</select>";
So I'm trying to understand this whole AJAX/jQuery thing. Right now, when I run this PHP script alone, I would have to wait and watch the wheel spin until it's done with the loop and then it will load.
while ( $row = mysql_fetch_array($res) ) {
postcode_to_storm( $row['Test'] );
$dom = new DOMDocument();
#$dom->loadHTML($result);
$xPath = new DOMXPath($dom);
$failInvite = 'Rejected';
$findFalse = strpos($result, $failInvite);
if ( $findFalse == true ) {
$array[$i] = $row['Test'];
$i++;
echo $array[$i]};
}
}
Now, how do I use AJAX/jQuery to show echo $array[$i]}; everytime it is invoked instead of waiting for the whole process to complete?
The way AJAX works is that with the first request you write the basic HTML of your web page, including some javascript that calls back to the server and asks for more data. Depending on how you plan to send your data, it may make one or more requests after the page is rendered to get more data. Using AJAX will require that you rethink about how you're delivering your data. For example, you'll need one "script" to load the page, then another "script" to get the data -- of course, they could be the same, just with different parameters. I'll add a simple example to demonstrate since refactoring your example would require more understanding of your data and how it's delivered. This example is from w3schools.com.
HTML:
<script type="text/javascript">
$(function() {
$('#users').change(function() {
// here's the AJAX bit
$.get( '/users/load.php?q=' + $(this).val(),
function(html) {
$('#txtHint').html(html);
});
});
});
</script>
</head>
<body>
<form>
<select name="users">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
PHP
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ajax_demo", $con);
$sql="SELECT * FROM user WHERE id = '" . mysql_real_escape_string( $q ) . "'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
This is similar to COMET. Take a look at the differences here: http://ajaxian.com/archives/comet-a-new-approach-to-ajax-applications
Here's a post that talks about the difficulties in PHP in particular with a LAMP stack: http://www.phpclasses.org/blog/post/58-Responsive-AJAX-applications-with-COMET.html - may be a little dated as it was posted in 2006, but you get the point.
Typically when you talk about vanilla "ajax," you are talking about a client initiated request for some finite information (the http request completes), without an explicit page reload.
So, you want server to send every piece of data separately, once it's produced? I'm afraid, there is no such feature with http requests. You have to close it and then response is sent to the browser.
You may consider sending several requests, with some kind of pagination.