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.
Related
I have code that search for my field name(familycode) then displays a table. I have the code enclosed in the table to update table and database.
My issue - after updating - My display information does't update and won't refresh or ever show correct data from mysql database. Database is updated.
I am new and this is first post. I tried many many different ways no luck. I would appreciate some thoughts!! Thank you!
my code for main page d7.php
<?php
// connection
$dbcon= NEW Mysqli("localhost", "root", "", "xxx");
if (!$dbcon) {
echo " ----------Error connecting to database--------------";
}
else {
echo " ----------Connected to Database Successfully----------- <br>" ;
}
include 'updatedata.php';
?>
<html>
<head>
<title> View_Update Family</title>
<link rel="stylesheet" type="text/css" href="d7css.css">
</head>
<body>
<form action=d7.php method=post>
<input type="text" name="valueToSearchfamily" placeholder="Family To
Search"><br><br>
<input type="submit" name="searchfamily" value="Search Family"><br>
<br>
</form>
<?php
if(isset($_POST['searchfamily']))
{
$valueToSearchfamily=$_POST['valueToSearchfamily'];
// search in all table columns
$select = "SELECT * FROM families WHERE Familycode LIKE
'%".$valueToSearchfamily."%' ";
$mydata=mysqli_query($dbcon, $select);
}
else {
$notselect = "SELECT * FROM families ORDER BY Familycode ";
$mydata=mysqli_query($dbcon, $notselect);
}
echo "<table class='updatetable' >";
echo "<tr>
<th>Id</th>
<th>Familycode</th>
<th>Name</th>
<th>Street</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
</tr>";
while($record = mysqli_fetch_array($mydata)){
echo "<form action=updatedata.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name=aid value=" . $record['Aid'] .
" </td>";
echo "<td>" . "<input type=text name=familycode value=" .
$record['Familycode'] . " </td>";
echo "<td>" . "<input type=text name=name_mailing value=" .
$record['Name_mailing'] . " </td>";
echo "<td>" . "<input type=text name=street_mailing value=" .
$record['Street_mailing'] . " </td>";
echo "<td>" . "<input type=text name=city_mailing value=" .
$record['City_mailing'] . " </td>";
echo "<td>" . "<input type=text name=st_mailing value=" .
$record['St_mailing'] . " </td>";
echo "<td>" . "<input type=text name=zip_mailing value=" .
$record['Zip_mailing'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" .
$record['Aid'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . "
</td>";
echo "</form>";
};
?>
</body>
</html>
my other file as I tried separating them is updatedata.php
<?php
// connection
$dbcon= NEW Mysqli("localhost", "root", "", "xxx");
if (!$dbcon) {
echo " ----------Error connecting to database--------------";
}
else {
echo " ----------Connected to Database Successfully----------- <br>" ;
}
// ==========================================================
if(isset($_POST['update'])) {
$updatequery="UPDATE families SET Aid='$_POST[aid]',
Familycode='$_POST[familycode]', Name_mailing='$_POST[name_mailing]',
Street_mailing='$_POST[street_mailing]',
City_mailing='$_POST[city_mailing]', St_mailing='$_POST[st_mailing]',
Zip_mailing='$_POST[zip_mailing]' WHERE Aid='$_POST[hidden]'";
mysqli_query($dbcon, $updatequery);
header("location: index_dir.php");
};
?>
Welcome Don T to stackoverflow! It seems like your echoing onto the page in an instance, recall that when you Echo something out, it doesn't change when it changes inside your database because you aren't making the call again, its on the same page. To fix this, you need to use Javascript + Ajax, I recommend picking up jQuery and harnessing its basics for POST'ing and GET'ing data from a PHP page.
Here is the link for jQuery Ajax: http://api.jquery.com/jquery.ajax/
Simple example using jQuery ajax:
$.ajax({
url: 'PHP SCRIPT PAGE URL',
method: 'POST', // Your sending data, use POST
data: 'Your Data!', // This can be also be a form object
success: function(response) { // Response is your PHP scripts answer to this request.
console.log(response); // You may Echo or use JSON format as a response.
} // If you use JSON, add dataType: 'JSON' in the ajax call to the left.
})
Remember if this answer has helped you resolve your issue, select it as the Answer!
Thanks!
let me explain the code I'm getting data from the db and display the data in text input then when the user make changes I get the user input and update the table however when it update it update to empty data
<?php
echo "<form action='' method='get'>";
echo "<td class='data'><input type='product_name' id='product_name' value=" . $row['product_name'] . "> </td>";
echo "<td class='data'><input type='products_price' id='products_price' value=" . $row['products_price'] . "> </td>";
echo "<td class='data'><input type='products_desc' id='products_desc' value=" . $row['products_desc'] . "> </td>";
echo "</form>";
echo "</tr>";
echo "</table>";
echo "<form action='' method='POST'>
<input name='update' type='submit' value='Update' style='margin-left: 720px'>
</form>";
$product_name = isset($_GET['product_name']) ? $_GET['product_name'] : '';
$products_desc = isset($_GET['products_desc']) ? $_GET['products_desc'] : '';
$products_price = isset($_GET['products_price']) ? $_GET['products_price'] : '';
if (isset($_POST["update"])) {
$sql1 = "UPDATE tbl_products SET products_desc='" . $products_desc . "',product_name='" . $product_name . "',products_price='" . $products_price . "' WHERE products_id='" . $product_id . "'";
mysqli_query($conn, $sql1) or die(mysqli_error($conn));
echo "yess";
}
?>
Your input elements have no name attribute, so things like $_GET['product_name'] will always be empty. The name attribute is the key in the key/value pair sent to the server.
Additionally, the type attributes are all broken. (Though I suspect the browser is automatically "correcting" that by defaulting to a text input.)
Add the name attribute (and fix type):
<input type='text' name='product_name' id='product_name' ...
Additionally, you have two forms. So when you click your button, that form doesn't submit any of the inputs. Because they're in a different form.
Put them all into the same form, and decide whether you want to use GET or POST (since your server-side code is going to need to know that).
You defined your data form in one form tag and submit form in the another form tag:
<form action='' method='POST'>
<input name='update' type='submit' value='Update' style='margin-left: 720px'>
</form>
echo "<form action='' method='get'>";
echo "<td class='data'><input type='product_name' id='product_name' value=" . $row['product_name'] . "> </td>";
echo "<td class='data'><input type='products_price' id='products_price' value=" . $row['products_price'] . "> </td>";
echo "<td class='data'><input type='products_desc' id='products_desc' value=" . $row['products_desc'] . "> </td>";
echo "</form>";
When you submit your first form but your data is on another form
this is what is wrong
I'm trying to make a HTML table as a frontend to a mySQL database. The table displays fine and I can type in the edits I want to make to each row of the table but when I press the submit button the changes aren't actually made. Can anyone see where I'm going wrong?
<?php
include("db.php");
$sql = "SELECT * FROM `artist`";
$result = mysqli_query($conn, $sql);
if (isset($_POST['update'])){
$artID = $_POST['artID'];
$artName = $_POST['artName'];
$key = $_POST['hidden'];
$UpdateQuery = "UPDATE `artist` SET `artID` = '$artID', `artName` = '$artName' WHERE `artist`.`artID` = '$key'";
mysqli_query($conn,$UpdateQuery);
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
};
echo "<table border='1'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "</tr>";
if ($result->num_rows > 0) {
echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='submit' name ='update'" . " </td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
The db.php file simply includes the connection info to the mySQL database and I'm 100% sure there's nothing wrong with it as it retrieves the table correctly it just doesn't update.
You are putting form tag inside tr which is not allowed td are only allowed
so you have to remove that tr from there.
You have to use jquery or you can replace the table with some other grid structure so that it can look the same and the form can be placed there as well
One more suggestion Don't mix the php and html together separate them for the clean code
If you do all these you code will be like this
Your form is being constructed with multiple elements with the same name. When you submit the form it is using the last elements as the values so regardless of the record you want updated the last record is being updated (or throwing an error because of string encapsulation). You should use parameterized queries as well.
So instead of:
if ($result->num_rows > 0) {
echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='submit' name ='update'" . " </td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
Use:
if ($result->num_rows > 0) {
// output data of each row
while($row = mysqli_fetch_array($result)) {?>
<form class='artisttable' action ='getartiststable.php' method ='post'>
<tr>
<td><input type='text' name ='artID' value ='<?php echo $row['artID'];?>' /></td>
<td><input type='text' name ='artName' value ='<?php echo $row["artName"];?>' /></td>
<td><input type = 'hidden' name ='hidden' value='<?php echo $row['artID'];?>' /></td>
<td><input type='submit' name ='update'" . " </td>
</tr>
</form>
<?php } ?>
</table>
So you get a form for each data set. Here's a link on prepared statements with mysqli: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php. You also should update your mark up. Tables for formatting aren't the best approach. Your inputs also weren't closed missing >.
Also this changed artisttable from an id to class because there will be multiples. Update CSS/JS accordingly.
So I already have a drop down list generated from a MySQL db.
The problem I can't solve is how to display a "preview" of a selected option.
It's a part of a game I'm building, and you can go to the "arena".
In the arena, there will be a drop down list of opponents to choose from, all of them have different values attached to them like, name, level, strength and so on.
So if you select "opponent 1" from the list, I want a opponent preview to show, and if I change the drop down to "opponent 2" I want the preview to update.
When the player has decided to fight that opponent, a submit can be made by clicking my "fight" button.
Here is the drop down list I'm using:
EDIT: See my answear below with updated code.
After like 8h of pulling my hair, google and ALOT of reading.. i manage to put this together, and it is working!
HTML code:
<div id='load_npc'>
<table width='720' border='0' align='center'>
<td align='center'>
<form id='form1' name='form1' method='post' action=''>
<fieldset>
<legend><strong>Pick your opponent!</strong></legend>
<p>
<select name='npc_name' id='npc_name'>
<option value='' selected='selected'> Select an opponent.. </option>
<option name='row[name]' value='row[name]'><php_row></php_row> </option>
</select>
<br>
<input type='submit' value='Fight!' \>
</p>
</fieldset>
</form>
</td>
</table>
<div id='results'></div>
</div>
Javascript code, placed in the header:
$(function() { // document.ready
$("#npc_name").on("change", function() {
$.ajax({
url: "system/npc_load.php",
type: "POST",
data: {
npc_name: $(this).val()
},
success: function(data) {
$("#results").html(data);
}
});
});
});
And here is the php file referd to by my script:
<?php
require ("database.php");
$npc_name = $_POST['npc_name'];
$sql="SELECT * FROM npc WHERE name = '" . $npc_name . "'";
$myData = mysqli_query($connection, $sql);
//count if there are any results
$numrow = mysqli_num_rows($myData) or die(mysqli_error($connection));
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>Name</th></tr>
<tr><th>Type</th></tr>
<tr><th>Level</th></tr>';
while($info = mysql_fetch_array($myData))
{
echo "<form action='system/npc_load.php' method='post'>";
echo"<tr>";
echo "<td align='center'>" . $info['name'] . "<input type=hidden name=name value=" . $info['name'] . " </td>";
echo "<td align='center'>" . $info['type'] . "<input type=hidden name=type value=" . $info['type'] . " size='11' maxlength='11' /> </td>";
echo "<td align='center'>" . $info['level'] . "<input type=hidden name=level value=" . $info['level'] . "' /> </td>";
echo "</tr>";
echo "</form>";
}
}
echo "</fieldset>";
?>
//this is saved in president.php
if ($result = $mysqli->query("SELECT * FROM candidate_info WHERE position= 'president'"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table border='1' cellpadding='10'>";
// set table headers
echo "<tr><th>Student ID</td><th>Course</th><th>Name</th><th></th></tr>";
while ($row = $result->fetch_object())
{
// set up a row for each record
echo "<tr>";
echo "<td>" . $row->studid . "</td>";
echo "<td>" . $row->course . "</td>";
echo "<td>" . $row->fname . " ". $row->mname ." ". $row->lname ."</td>";
echo "<td><input type ='radio' name='radio' value='". $row->studid ."'> </td>";
echo "</tr>";
}
echo "<td> <input type='button' name='button' value='select' onclick='". get_radio_value() ."'></td>";
echo "</table>";
}
else {
echo "No results to display!";
}
}
//this my javascript to read and get the value of my radio
function get_radio_value()
{
for (var i=0; i < document.orderform.radio.length; i++)
{
if (document.orderform.radio[i].checked)
{
var rad_val = document.orderform.radio[i].value;
}
}
}
//here i would show the value of my selected radio as i click the select button on the //textbox where inside my form
" />
//but as i load my page, i got an error
// like this Fatal error: Call to undefined function get_radio_value()
//this is the code for my voting.php
<div id="TabbedPanels1" class="TabbedPanels">
<ul class="TabbedPanelsTabGroup">
<li class="TabbedPanelsTab" tabindex="0">President</li>
<li class="TabbedPanelsTab" tabindex="0">Secretary</li>
</ul>
<div class="TabbedPanelsContentGroup">
<div class="TabbedPanelsContent"><p> </p>
<p><?php require_once('candidate/president.php'); ?></p>
<p> </p></div>
<td width="292" valign="top">
<form method="post" action="voting.php" >
<table width="289" height="76" border="0" cellspacing="1">
<tr>
<td width="131" height="24" ><div align="right">President</div></td>
<td width="151"><div align="left">
<input type="text" name="president" id="radio_value"/>
</div></td>
</tr>
</table>
</form></td>
change this
echo "<td> <input type='button' name='button' value='select' onclick='". get_radio_value() ."'></td>";
In this it is calling php's get_radio_value() function, but the function is written in javascript, so use below code.
echo "<td> <input type='button' name='button' value='select' onclick='get_radio_value()'></td>";
for passing the value of radio button in textbox
<input type="text" name="president" value="WHAT CODE SHOULD I PUT HERE?" id="radio_value"/>
change this line
var rad_val = document.orderform.radio[i].value;
to
return document.getElementById('radio_value').innerHTML = document.orderform.radio[i].value;