Getting two values from a mysql table using a checkbox - php

So I have a mysql table for the charges of a hospital. My program currently only gets the price of the checked procedure. But now, I also want to get the procedure name when it is checked.
while($row = mysql_fetch_array($result)){
echo '
<tr>
<td>'.$row[0].'</td>
<td>'.$row[1].'</td>
<td>'.$row[2].'</td>';
$price=$row['price'];
echo '<td><input type="checkbox" name="er[]" value="'.$price.'"></td>';
echo "</tr>";
}
echo '</table>';
So basically I need to edit this part of the code:
$price=$row['price'];
echo '<td><input type="checkbox" name="er[]" value="'.$price.'">
It only gets the row for price. Can the value attribute have two values? I can't just make another checkbox cause that would be inappropriate.

change the code like this
<?
while($row = mysql_fetch_array($result)){
echo '
<tr>
<td>'.$row[0].'</td>
<td>'.$row[1].'</td>
<td>'.$row[2].'</td>';
$price=$row['price'];
$procedure_name=$row['$procedure_name'];
echo '<td><input type="checkbox" name="er" value="'.$price.','.$procedure_name.'"></td>';
echo "</tr>";
}
echo '</table>';
?>
Then use explode()

You could simply put the name in another attribute (like title) and whenever you need to get the name you can use some javascript to retreive it.
$price=$row['price'];
echo '<td><input type="checkbox" name="er[]" value="'.$price.'" title="'.$name.'">

Related

created a form in a loop, when posted does not show values

I'm recalling query values which are settings alongside their parameters. this includes the ID, value and description. As the fetch is in a form of a array i used a loop to display the different variables (this way if there are more settings to put in it is easier to expand). The loop consists of a different form allowing you to edit the value of the parameter of that setting. When you submit, it does not post the value, but it posts the ID attached to the button. What can I do?
$query = "SELECT * FROM metadata";
$db->DoQuery($query);
$num = $db->fetchAll(PDO::FETCH_NUM);
if (isset($_POST)){
echo "<pre>";
print_r($_POST);
echo $_POST['service_name'];
echo "</pre>";
}
echo "<table id='mytable' style='width:100%'>";
echo "<tr>
<th>Option</th>
<th>Value</th>
<th>Description</th>
</tr>";
foreach ($num as $row) {
echo '<form action="" method="post">';
echo '<tr>';
echo '<td>'.$row['rule'].'</td>';
echo '<td><input name="service_value" placeholder="value" value='.$row['value'].'></td>';
echo '<td>'.$row['description'].'</td>';
echo '<td><button value="'.$row[0].'" name="service_id"></button></td>';
echo '</tr>';
}
?>
</form>
</table>

How to give the checkbox a value for each data that called from database and how to post it

I don't know if this can be done in php. I have an equipment list that is call from database. User can select any equipment that they want to borrow by selecting the checkbox. I have checkbox named 'list'. The question is how to put checkbox value and post the checkbox value?? because I want to save the selected item(checked) into database.
<?php
//include("connect.php");
$sql = mysql_query("SELECT * FROM equipment WHERE equip_name_desc='$search'");
$row = mysql_num_rows($sql);
if ($row >= 1){
echo '<table border="1"><tr>';
echo '<td align="center" width="40">NO</td>';
echo '<td width="200">DESCRIPTION</td>';
echo '<td width="120">SERIAL NUM.</td>';
echo '<td width="120">REF. NUM.</td>';
echo '<td width="120">PRICE</td>';
echo '<td width="120">STATUS</td>';
echo '<td width="80">BORROW</td>';
echo '</tr>';
$index=1;
while($a = mysql_fetch_array($sql))
{
echo '<tr>';
echo '<td align="center">'.$index.'</td>';
echo '<td>'.$a['equip_desc'].'</td>';
echo '<td>'.$a['equip_sn'].'</td>';
echo '<td>'.$a['equip_ref'].'</td>';
echo '<td>'.$a['equip_unit_price'].'</td>';
echo '<td>'.$a['equip_status'].'</td>';
echo '<td>'?><input type="checkbox" name="list" /><?php
echo '</tr>';
$index++;
}
echo '</table>';
}
?>
Well, pretty weird thing, but ill try to understand. I suppose the serialnumber is the unique primary. So you could use that as a reference for all the items in the array:
echo '<td><input type="checkbox" name="list" value="$a[\'equip_sn\']></td>';
this is how you put the checkbox value
echo '<td>'?><input type="checkbox" name="list" value="'.$a['your field'].'" />
to post the check box value
assign it to a variable
$variablename=$_POST['list']=="$a[0]" //take the values stored in the array

Dropdown menu inside a table - then update

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>";

How to create submit form in a row and relate input to that row?

Hey Guys, I have a question for you.
Imagine that I wanted to be able to keep track of how many miles I've ran every week, so that I could
compare it to the goals I've set for each week. So i've created this table by the use of mysql_fetch_row.
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><form><input method='post' type='number'></form></td>";
echo "</tr>";
}
echo "</table>";
This piece of code resultet in a table with 10 weeks with 10 goals - and a column for the actual number of miles. This column should include 10 input forms where the actual number of miles can be submitted. But how do I relate the input from the submit form to the row in which the submit form is positioned?
The primary key is the week - so this would be the one to relate to.
Hope you understand what my problem is:)
To do this you would use a hidden input field.
When you echo each row, and the form in that row, you would simply add an extra line:
`<input type="hidden" name="row_id" value="' . $row['id_column'] . '" />';
In full, your code would be:
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>
<form>
<input method='post' type='number'>
<input type='hidden' name='row_id' value='" . $row['id_column'] . "' />
</form>
</td>";
echo "</tr>";
}
echo "</table>";
I think there should be some modifications that has to be done in loop.
echo "<td><form method='post'><input type='number' value='".$rows['col_name']."'><input type='submit' ></form></td>";
This code adds a submit button to each row. But, this shouldn't be what I think.
It should be rather this way,
echo "<form method='post'> ";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><input type='number' value='".$rows['col_name']."'></td>";
echo "</tr>";
}
echo "<input type='submit' ></form>";
Or make the input field look like this.
'<input type="text" name="row['.$row['id_column'].'][miles]" />';
It will return you an array when you post it.
foreach($_POST['row'] as $key => $value){
// $key is your primary key
// $value['miles'] is your input value
}

a array of php with multiple textboxes

basically i have a database, which contains a random amount of questions
i have then printed this out as a php table using a query. there is as many textboxes to input marks as there are questions in the database.
I want to create an array of my inputted data and then update the database with the marks in.
Here is my code
echo "<table border = '0' cellpadding ='10px'>";
echo "<tr>
<td> Question <td>Mark</td><td>Criteria</td>
<td>Feedback</td>
</tr>";
while ($row = mysql_fetch_array($result))
{
$question[]=$rows['question'];
echo "<tr>";
echo "<td>". $row['question']. "</td>";
echo "<td>" ."<input type = 'text' name = 'mark[]' size = '1' value = '0' id = 'mark'/>/". $row['maxMark'] . "</td>";
$maxMark[] = $row['maxMark'];
echo "<td>".$row['criteria']."</td>";
echo "<td>" . "<textarea name = 'feedback[]' id= 'feedback'>Enter Feedback here</textarea>". "</td>";
echo "</tr>";
}
echo "</table>";
echo "</tr>\n";
echo "</table>";
I am not sure how to create the array, with the marks you input. please help.
In short i want to populate an array with the marks i input
This is rather easy to be done. All you have to do is setting the attribute name of your input controls and add an index.
Example:
<input type="text" name="name[0]" /><input type="text" name="mark[0]" />
<input type="text" name="name[1]" /><input type="text" name="mark[1]" />
The post (or get) data your script receives will then contain an array instead of a single variable.

Categories