How to display an option from a selectbox? - php

I'm trying to create a table that shows the time and the task a user has to do.
All this is saved in a database. For each row and column there is a selectbox(Usernames) and an <input>(for comments).
Here is a screenshot: http://snag.gy/NYwsJ.jpg
The name of each selectbox is the id of the time and task
echo "<select name='" . $x['idTask'] . "-" . $y['idTime'] . "'>
(e.g: name="1_23")
The problem is when I load the page it is supposed to display all the records that are already saved in the database.
For the <select> I tried using this an if:
if ($x["fiUser"] == $z["idUser"]){echo "selected";}
And here is the table:
<?php
echo"<div class='dv_Table'><table><tr><th></th>";
foreach (SelectTime() as $r) {
echo "<th>" . $r['dtTime'] . "</th>";
}
echo "</tr>";
echo "<form method='post'>";
$userselect = "";
foreach (SelectCalTask() as $x) {
echo "<tr><td>" . $x['dtTask'] . "</td>";
foreach (SelectTime() as $y) {
echo "<td>";
echo "<select name='" . $x['idTask'] . "-" . $y['idTime'] . "'>
<option value='0'> -- None -- </option> ";
foreach (SelectUser_Name() as $z) {
echo "<option value='" . $z["idUser"] . "'";
if ($x["fiUser"] == $z["idUser"]) {
echo "selected";
} echo" >" . $z["dtFirstName"] . " " . $z["dtLastName"] . "</option> ";
}
echo " </select>
<input type='text' name='" . $x['idTask'] . "_" . $y['idTime'] . "' value='" . "_" . $y['idTime'] . "'></td>";
}
echo "</tr>";
}
echo "</table></div>";
echo "<input type='submit' name='updateCalendar' value='Update'></form>";
?>
Better, would be if could send an Update query right after the "onchange" or right after finishing the comment in the input.
Is that possible ?
Thank you in advance

*
Better, would be if could send an Update query right after the
"onchange" or right after finishing the comment in the input. Is that
possible ?
*
Why not?
$('.yourValue').on('change', function (e) {
$.ajax({
type: "POST",
url: "/path-to-update-query",
success: function (data) {
}
});
});

Related

How do I maintain the status of the check box?

After selecting the check box, the check box values and the sum values are reset when the next page is passed. How should I modify it?
PHP code:
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td > <input name='amount' type='checkbox' value ='" . $row['amount'] . " ' onclick='totalIt()'></td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['DATE'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo " <input value='₩0' readonly='readonly' type='text' name='total' />";
// Free result set
mysqli_free_result($result);
JavaScript code:
function totalIt() {
var input = document.getElementsByName("amount");
var total = 0;
for (var i = 0; i < input.length; i++) {
if (input[i].checked) {
total += parseFloat(input[i].value);
}
}
document.getElementsByName("total")[0].value = "₩" + total;
}
and How can I check one more value in advance?

In the edit mode i need to show selected value from the dropdown in PHP

I have a dropdown in php the value is coming from database. In the edit mode i need to show selected value in to drop down. My php is below:
<?php
echo "<td width='5%'>";
$sql_currency = "SELECT * FROM currency1";
$result_currency = mysql_query($sql_currency);
echo "<select id='currency_change$i' >";
while ($row_currency = mysql_fetch_array($result_currency)) {
echo "<option value=" . $row_currency['currency'] . " data-price=" . $row_currency['rate'] .">" . $row_currency['currency'] ."</option>";
}
echo "</select>";
echo "</td>";
?>
Try this code...
<?php
echo "<td width='5%'>";
$sql_currency = "SELECT * FROM currency1";
$result_currency = mysql_query($sql_currency);
echo "<select id='currency_change$i' >";
/*
* selected value
*/
$selectedValue = ""; // assign that value to this variable
while ($row_currency = mysql_fetch_array($result_currency)) {
$selected = "";
if($row_currency['currency'] == $selectedValue){
$selected = ' selected="selected" ';
}
echo "<option ".$selected." value=" . $row_currency['currency'] . " data-price=" . $row_currency['rate'] . ">" . $row_currency['currency'] . "</option>";
}
echo "</select>";
echo "</td>";
?>

Looping through $_POST Variable

I have a PHP script that generates some form on the page I have. I also have a table and submit button within the form which all works fine and the data is sent to the next page as intended. Now on the next page I have a PHP code that reads the $_POST variable. Here is my code:
echo "<form name=\"myForm\" id=\"myForm\" method=\"post\" action=\"\">";
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "<th>Group</th>";
echo "</tr>";
while($row = mysql_fetch_array($qryRes))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td><input name=\"aName" . $row['id'] . "\" id=\"aName" . $row['id'] . "\" type=\"text\" value=\"" . $row['aName'] . "\" /></td>";
echo "<td><select name=\"gID" . $row['id'] . "\">";
while($gRow = mysql_fetch_array($grpsQry))
{
if($gRow['id'] == $row['groupID'])
echo "<option id=\"" . $gRow['id'] . "\" selected=\"selected\">" . $gRow['gName'] . "</option>";
else
echo "<option id=\"" . $gRow['id'] . "\">" . $gRow['gName'] . "</option>";
}
echo "</select></td>";
echo "</tr>";
mysql_data_seek( $grpsQry, 0 );
}
echo "</table>";
echo "<input type=\"submit\" name=\"submitForm\" value=\"Save\" />";
echo "</form>";
Then comes my PHP Code to read the $_POST:
if(isset($_POST['submitForm']))
{
print_r($_POST);
foreach($_POST as $x)
echo $x . "<br />";
}
Everything works fine, except that I need to read only the text field in the foreach loop as I will generate a query on them and will compare the values I will have with the value returned from the List ..
As yuo can see, the textfield is named aName then the id of the user. I need to read this only. But I will use it to generate a query which will be then compared to the value of the List gID.
try this:
foreach($_POST as $key => $value){
if(substr($key,0,5) == 'aName')
echo $value . "<br />";
}

how to use a link to download a file in php

I am using the following code to fill the drop-down box and select a file and download it. Its working perfect.But i tried using every file to display as link and download it on clicking the link.
echo "<form id=\"form\" name=\"psform\" action=\"download_logic.php\" method=\"post\"><label>Select File:&nbsp</label><select name=\"file\" >";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['location'] . "'>" . $row['location'] . "</option>";
}
echo "</select></label>";
echo"<br>";
echo "<input id=\"submit\" type=\"submit\" name=\"filesubmit\" value=\"Download\" /> </form>";
its giving me errors ....any help please....
I am using following code:
while ($row = mysql_fetch_array($result)) {
echo "" . $row['fileshare'] . "";
}
Here is the error in the <a> tag*** Error (unexpected T_ENCAPSED_AND_WHITESPACE)*
this should work:
echo '' . $row['fileshare'] . '';
I'm really sorry I can't tell you why it happens, but if you change all of the \" to ' it should work for you.
echo "<form id='form' name='psform' action='download_logic.php' method='post'><label>Select File:&nbsp</label><select name='file' >";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['location'] . "'>" . $row['location'] . "</option>";
}
echo "</select></label>";
echo"<br>";
echo "<input id='submit' type='submit' name='filesubmit' value='Download' /> </form>";
As for the next part, you can't add a variable to a string just by writing
"variable: . $take['variable']"
If you wish to add variables to string close the writen part of the string and then add the variable, like this:
"variable: ". $take['variable']
Here is how it sould look like:
while ($row = mysql_fetch_array($result)) {
echo "<a href='download_logic.php?f=". $row['location'] . $row['fileshare'] ."'>" . $row['fileshare'] . "</a>";
}

Selecting values from the database and make the checkbox checked if it does exist

//query to check if part id number exists in table ATTEND where service id = ...
$result2 = mysql_query("SELECT * FROM attend WHERE SIDno='$SIDno' and ServiceID='$id");
//if exists $ok = true;
if (mysql_num_rows($result2)>0) {
$ok == true;
}
echo "<tr bgcolor=$bgcolor>";
echo "<td><a name=$row1[0] id=$row1[0]>$row1[0]</td>";
echo "<td>" . $row1[1] . "</td>";
echo "<td>" . $row1[5] . "</td>";
echo "<td>" . $row1[2] . "</td>";
echo "<td>" . $row1[3] . "</td>";
echo "<td><input type='checkbox' name='checkbox[]' value=" . $row1[0];
if ($ok == true) {
echo 'disabled="disabled" checked="checked"';
}
echo "></td>";
echo "<input type='hidden' name='ServiceID' value=" . $id . ">";
echo "<input type='hidden' name='Year' value=" . $Year . ">";
echo "<input type='hidden' name='Stype' value='Recollection'>";
echo "</tr>";
}
}
echo "<tr>
<td colspan='5' align='right' bgcolor='#FFFFFF'><input name='SUBMIT1' type='submit' id='SUBMIT'value='SUBMIT'></td>
</tr>";
How can implement that on the next load if the value of the checkbox is already available in the database it will now be checked. but if it is not yet existing i can check it and save it to the database.
I'm guessing the problem lies in this line:
if (mysql_num_rows($result2)>0) {
$ok == true;
}
It should be:
if (mysql_num_rows($result2)>0) {
$ok = true;
}
In the first snippet you are just testing if $ok is equal to true, while in the second example an actual assignment to the variable is performed.
Remember:
= != ==

Categories