populate values of a row from the id selected - php

I'm using PHP to populate names into an option list based on values from a database.
Once a name is selected I want to populate the values of a row based on whatever name has been selected.
I had no problem populating the option list with the name values, but once I select a name the records of that row will not display in the input boxes.
Here is the code I'm working with:
$query = "SELECT name, id
FROM artists
ORDER BY name";
$mysql_stuff = mysql_query($query, $mysql_link);
while($row = mysql_fetch_row($mysql_stuff)) {
$artists_name = htmlspecialchars(stripslashes($row[0]));
$artists_id = stripslashes($row[1]);
// we compare the id of the record returned and the one we have selected
if ($artists_id) {
$selected = $artists_id;
} else {
$selected = "";
}
print("<option value=\"$artists_id\">$artists_name</option>
");
}
print("</select>
<input type=\"Submit\" name=\"submit\" value=\"Update\">
<input type=\"Submit\" name=\"delete\" value=\"Delete\">
<br >
<br />
Edit Biography:
</td>
</tr>
");
if (isset($_POST['submit'])) {
print("<tr valign=\"top\">
<td valign=\"top\" width=\"150\">Name</td>
<td valign=\"top\">Artist Biography</td>
</tr>
");
print("<tr valign=\"top\" bgcolor=\"$colour\">
<td valign=\"top\">
<input type=\"text\" name=\"artists_name[$selected]\" value=\"$artists_name\" size=\"40\" />
</td>
<td valign=\"top\">
<textarea name=\"artists_Biography[$selected]\" cols=\"40\" rows=\"10\">$artists_Biography</textarea>
</td>
</tr>
");
}
print("</table>
</form>
");
Can I please get some assistance with populating the values of the selected name into the input boxes.

So your submitting artist ID to the post. The post would then refresh or change the page. Assuming the action is that page you have the ID in your post. How do you get Artist Name from the ID? Do you run another sql query on the id to find the name? Might need some clarification on the order of your code to help. Thanks

I'm not sure how to interpret this code. Use Ajax. Use Jquery on change() method to take the value of the select and send it via ajax() to a php script that gets the info from the DB and returns what you want. Then use the Success part of the .ajax() method to put that data into you input on the form.
$(function(){
$("select#artist").change(function()
{
var artist = $("select#artist").val();
$.ajax({
type: "POST",
url: "ajax-find-artist.php",
data: "artistID=" + artist,
cache: false,
success: function(html){
var newElems = html;
$("input#artistRestult").html(newElems);
}
});
});
});

Related

Get the value from many radiobuttons inside a table

First I create a table with n number of rows and each one of them contains two radio buttons. I need to get all the id from the table row selected and the option selected. I mean if the user selected yes or no.
<table border='1' style='width:100%'>
<tr>
<th>Date</th>
<th>Time</th>
<th>Confirm Order?</th>
</tr>
<tr id='56'>
<td>".$date." </td>
<td>".time." </td>
<td>".$prog." </td>
<td>
<input type='radio' name='prog1' />NO
<input type='radio' name='prog1' />SI</td>
</tr>
</table>
Now what I got so far is getting the ids from the rows checked. Like this:
var idss = [];
$(':radio:checked').each(function(index) {
var closestTr = $(this).closest('tr').attr('id');
idss.push($(this).closest('tr').attr('id'));
});
$(function test() {
$.ajax({
type: 'POST',
url: 'test.php',
data: {
idss: idss
}
});
How can I get an array with all ids where 'no' was selected, and one array with all the ids where 'yes' was selected.
I made a fiddle here: https://jsfiddle.net/7w2df640/
This is the js:
function getArray(){
ar = {};
$(".datarow").each(function(i,o){
ar[$(o).attr("id")] = $(o).find("input[type='radio']:checked").val()
});
console.log(ar);
}
For the html, you have to add a class to the rows:
<tr class="datarow" id='56'>
and also add a value to the radio:
<input type='radio' name='prog1' checked value="no" />
In the end you will get an array exactly as you wanted:
Object {56: "si", 57: "no"}
Does that make sense?

reload multiple table cells with jQuery

I have many forms in a page, each form with several inputs. When you click Submit on a form (say Form0), inputs are send to Reassign.php,that perform a search in a DB according to user inputs, and then a cell with div id="Cell0" in a table of the page is reloaded with data echoed by Reassign.php.
<script type="text/javascript">
$(document).ready(function(){
$("#Form0").submit(function(){
var MyVariables = $(this).serialize();
$('#Cell00').load('Reassign.php?MyVariables=' + MyVariables);
return false;
});
});
$(document).ready(function(){
$("#Form1").submit(function(){
var MyVariables = $(this).serialize();
$('#Cell10').load('Reassign.php?MyVariables=' + MyVariables);
return false;
});
});
</script>
A stripped down version of the table:
<table>
<tr>
<td><div id="Cell00"><img src="Image0.jpg"/></div></td>
<td><div id="Cell01">Text1</div></td>
<td><div id="Cell02">Price1</div></td>
<td><div>
<form name="Form0" id="Form0">
<input type="hidden" name="Qst" value="0">
<input type="image" src="ReloadRed.gif" alt="Submit"/>
<select name="cmb1"><option>cmb1Option1</option><option>cmb1Option2</option></select>
<select name="cmb2"><option>cmb2Option1</option><option>cmb2Option1</option></select>
</form>
</div>
</td>
</tr>
<tr>
<td><div id="Cell10"><img src="Image0.jpg"/></div></td>
<td><div id="Cell11">Text1</div></td>
<td><div id="Cell12">Price1</div></td>
<td><div>
<form name="Form1" id="Form1">
<input type="hidden" name="Qst" value="0">
<input type="image" src="ReloadRed.gif" alt="Submit"/>
<select name="cmb1"><option>cmb1Option1</option><option>cmb1Option2</option></select>
<select name="cmb2"><option>cmb2Option1</option><option>cmb2Option1</option></select>
</form>
</div>
</td>
</tr>
</table>
A sample Reassign.php:
<?php
session_start();
$MyVariables = $_GET['MyVariables '];
$cmb1 = $_GET['cmb1'];
$cmb2 = $_GET['cmb2'];
$cmb3 = $_GET['cmb3'];
parse_str($MyVariables);
$Preg=$MyVariables;
$link = mysql_connect("localhost", usr, pswrd) or die(mysql_error());
#mysql_select_db("MyDB") or die( "Unable to select database");
mysql_query ("SET NAMES 'utf8'");
$query = "SELECT Img FROM MyTable WHERE Column1=$cmb1 AND Column2=cmb2";
if($success = mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$image="../ImageFolder/".$row[0].".png";
}
}
echo "<img src=\"".$image."\" />";
?>
This is OK.
Now I want other cells in the table to change as well, but with different (and related) data. Indeed, in the DB seach by Reassign.php, $row[0] is echoed (and thus loaded) in Cell00, and I want $row[1] to be send to Cell01, $row[2] to Cell02 and so on.
My problem is that right now I'm loading Cell0 echoing their content. I think i could do the job calling different php (Reassign0.php to echo data to Cell00, Reassign1.php to echo data to Cell01) but seems rather odd. Any ideas?
Here is a better approach... You can echo JSON instead of HTML from you PHP script, with all fields of the row. (Other code must be put in place for this to work for you, I'll let you research it on your own)
echo json_encode($row);
In your JS, get that json with $.getJSON(url, callback) or $.get(url, callback) depending on your server configuration
Then, loop through the cells and inject the data in your callback
$.getJSON('Reassign.php?MyVariables='+MyVariables, function(row){
for(i=0;i<row.length;i++){
$("#Cell0"+i).html(row[i]);
}
});
EDIT: if you data is not already formatted as HTML (like for images), be sure to do it in your callback. For example, if the first element is always an image URL and the rest is plain data, you can do :
$.getJSON('Reassign.php?MyVariables='+MyVariables, function(row){
for(i=0;i<row.length;i++){
var content = ( i == 0 ) ? '<img src="'+row[i]+'" alt="" />' : row[i];
$("#Cell0"+i).html(content);
}
});

How to Get the table name in database with select box?

Recently,I'm creating a school library Ser with three table in database(Newlibrary).
Three table is - Econ books , Geo books ,Chem books
In each table, it have (Student name) and (Tel No.).
I want to create a poor search engine with( A drop down list - Use select table) and input (Tel in
which selected table)
Here is the code ,How to use A drop down list to select table( Econ books or Geo books or Chem books )and input tel to search in table which selected at the same time ?
((index.html))
<form method="GET" action="searchtitle">
<select name="booktype" value="<?=$['booktype']?>">
<option value="" selected="selected"></option>
<option value="a">Econbooks</option>
<option value="b">Geobooks</option>
<option value="c">Chembooks</option>
</select>
<input id="inputkeyword" type="text" name="keyword" placeholder="Please enter a keyword...."/>
<input type="submit" class="keyword" value="search" />
</form>
((searchtitle.php))
<?php
$keyword = $_GET['keyword'];
$con = mysql_connect("localhost","root","password");
mysql_select_db("newlibrary");
$sql = "Select * from borrower
where Telp='$keyword' ";
$results = mysql_query($sql,$con);
if(mysql_num_rows($results) == 0) {echo "<h1>No Record Has Been Found!!</h1> ";} else {
echo "<table id='borrowertable' >
<tr>
<th>ID</th>
<th>Name</th>
<th>Tel</th>
</tr>" ;
while($row = mysql_fetch_array($results)) {
$bid = $row[0];
$Names = $row[1];
$Tel = $row[2];
echo " <tr>
<td>$bid</td>
<td>$Names</td>
<td>$Tel</td>
</tr>";
}
}
mysql_close($con);
echo "<p> <a href='index'>Back Home</a> ";
?>
How to change it?
If I got you right, you need to be able to search one of the three table as per user's selection using a phone number. I am not sure if you want to use Javascript, hope this can be helpful
First you need to pass both the Select and textbox values to searchtitle.php
function get_User() {
var user_data = {};
user_data['Book'] = document.getElementById("DropDownID").value; // get dropdown selected value
user_data['phone'] = document.getElementById("inputkeywor").value; // get dropdown selected value
$.ajax({
type: "POST",
url: "searchtitle.php",
beforeSend: function () {
$( "#LoadingDiv" ).html("Searching User...");
},
data: user_data,
success: function (data) {
Here Data is the result of searchTtitle.php.....
}}});
Then on searchtitle.php you get the values from
$keyword = $_POST['phone'];
$TableToQuery = $_POST['Book'];
call get_User on submit
<input type="button" class="keyword" value="search" onclick="get_User()" />

sending checked box values to php script to process via jquery

I have a table with four columns with a checkbox at the end of each row. I would like to know if there is a way of writing a jquery function that will select all the values of each row when the check box has been ticked and the submit button has been clicked.. and then pass the values to .php file to process/display. The reason i want to use the .php file is because i want to run a query to show all registered users on my system and then send the checked rows (id and name) to those users.
Sounds very complicated but i really need some help. thanks in advance.
here is the html code:
<div class="valuesOfObs">
<table>
<thead><tr><th>Valuation Name</th><th>Valuation Goal</th><th>Valuation Description</th><th>Options</th><th>Invite</th></tr></thead>
<tbody>
<?php
$sql = 'SELECT * FROM valuation';
$results = $db->query($sql);
$rows = $results->fetchAll();
foreach ($rows as $row) {
echo '<tr id="'.$row['ValuationID'].'">';
echo '<td class="crsDesc">'.$row['ValuationName'].'</td>
<td >'.$row['ValuationGoal'].'</td>
<td >'.$row['ValuationDescription'].'</td>
<td ><a href =values.php?action=invite&id=aValue> xValue </a></td>
<td > <input type="checkbox" name="selectValue">
</td>';
echo '</tr>';
}?>
</tbody>
</table>
</div>
<input type="button" name="addrow" id="addrow" value="Add row">
<input type="button" name="inviteObs" id="inviteObs" value="Invite Obstacle";>
Here is what I have written for the jquery so far.. I am trying to spilt the values so i can choose specifically which ones to process and then later on user the .join() method. Please help out if on the wrong track
$("input[type=checkbox], input[type=button]").on("click", function () {
$( ":checked" ).map(function() {
return $(this).closest("tr").text();
}).split("</td><td>");
var compName = $("#id").val();
$.ajax({
url : "inviteObstacles.php",
type : "POST",
data : {"compID" : compName},
success : function (n){
//do something
}
})
});
Use .map() function to pass each element in the current matched set through a function, which will produce new jQuery object containing the return values.
Here is a code:
$("input[type=checkbox]").on("click", function () {
$( ":checked" ).map(function() { return $(this).closest("tr").text();
}).get().join(); //Use join to split the fetched row using separator "," on server-side
});
Demo: http://jsfiddle.net/tSeau/1/

how to update and post the value of checkbox from ajax call

i retrieved data from mysql table into html page i.e
$query="select * from student";
$result=mysql_query($query)or die(mysql_error());
while($rs=mysql_fetch_array($result))
{
?>
<tr>
<td align="center"><?php echo $rs['st_id']; ?></td>
<td align="center"><?php echo $rs['name']"; ?></td>
<td align="center"><input type="checkbox" name="checked[]" <?php if($rs['checked']==1){echo "checked"; } ?> /></td>
<td align="center"><img src="images/delete_icon.png" alt="Delete" /></td>
<td align="center"><img src="images/update.png" alt="Update" /></td>
this is gives me the output
assume that checkbox of id 1 is checked and retrieved from mysql table.
Now what i want to do is that when i checked the remaing two checkbox into this table and a function is called to ajax which goes to php page and update the value of checked field according to the given id (i.e 2 and 3 in this case). Not refreshing the whole page nor updating the whole record just the value of check box. i am new to ajax so
any help would be greatly appreciated.
Yes, exploring jqGrid isn't a waste of time at all. However, since you want to update only on change of checkbox value. Here's what you can do..
<input type="checkbox" name="check1" />
And..
$().ready(function(){
var st_id = <?php echo $rs['st_id']; ?>;
//Or you could use a hidden field in the form
$(':checkbox').click(function(){
var chkName = $(this).attr('name');
var checkVal = $(':checkbox[name='+chkName+']').attr('checked');//true or false
$.ajax({
url: 'MyApp/update.php?checboxName=' + checkVal,//Do update on server-side
success: function(data) {
alert('Updated successful.');
}
});
});
});
Also, you can do a $('formID').serialize() wihch returns name=value pairs of all form elements that can then be appended in the call to your php script.

Categories