I am using this code to get the value of checked box in PHP but it doesn't work in the second code. The first one is just a list for testing. The second one will echo data from a database:
When applying the first code on the test list I will get the value of the box I checked but when applying it on the second part (one that gets data from database it returns Empty)
Get the checked boxes:
<?php
if(isset($_POST['DeleteCon']) )
{
if(!empty($_POST['lang']))
{
foreach($_POST['lang'] as $value)
{
echo "value : ".$value.'<br/>';
}
}
else
{
echo "value : Empty <br/>";
}
}
?>
Test list: it works on this section of the code:
<form method="post">
<?php
echo "<span>Select languages</span><br/>
<input type='checkbox' name='lang[]' value='PHP' class='table-row'> PHP <br/>
<input type='checkbox' name='lang[]' value='JavaScript'> JavaScript <br/>
<input type='checkbox' name='lang[]' value='jQuery'> jQuery <br/>
<input type='checkbox' name='lang[]' value='Angular JS'> Angular JS <br/>"
?>
</form>
But doesn't work on this one:
<form method="post">
<?php
include('database.php');
$sql = "SELECT id, ContactID ,FirstName, LastName, Phone FROM Contact WHERE ID='1'";
$result = $conn->query($sql);
if ( !empty($result->num_rows) && $result->num_rows > 0) { // ...
// output data of each row
echo "<form method='post'>";
while($row = $result->fetch_assoc()) {
echo "<tr>
<td id='delete'>
<input type='checkbox' name='lang[]' value='PHP' class='table-row'>
</td>
<td>". $row["FirstName"]. "</td>
<td>". $row["LastName"]. "</td>
<td>". $row["Phone"] ."</td></tr>";
}
echo "</form>";
} else {
echo "<tr>
<td id='delete'>
<input type='checkbox' id='row1' class='table-row'>
</td>
<td> 0 results </tr>";
}
$conn->close();
?>
</form>
Related
I've been looking for hours trying to find a solution. It has to be a simple one, yet, I can't find it.
All works well without the if-statement. Each row gets its button. But whenever I put the if-statement back in, the buttons dissappear. What am I doing wrong? Am I overlooking the obvious?
foreach($result as $row) {
echo "<tr>
<td>".date('d F Y', strtotime($row['DATE']))."</td>
<td>".date('H:i ', strtotime($row['TIME']))."</td>
<td>".$row['NAME']."</td>
<td>";
if ($row['availability'] == 0) { echo "FULL"; }
else if ($row['availability'] != 0) { echo $row['availability']; };
"</td>
<td> <form method='post' action='res2.php'>
<input type='submit' name='action' value='Next'>
<input type='hidden' name='ID' value='".$row['ID']."'>
</form> </td>
</tr>";
}
You can use the conditional operator to concatenate different strings depending on the availability.
foreach($result as $row) {
echo "<tr>
<td>".date('d F Y', strtotime($row['DATE']))."</td>
<td>".date('H:i ', strtotime($row['TIME']))."</td>
<td>".$row['NAME']."</td>
<td>" . ($row['availability'] == 0 ? "FULL" : $row['availability']) . "</td>
<td> <form method='post' action='res2.php'>
<input type='submit' name='action' value='Next'>
<input type='hidden' name='ID' value='".$row['ID']."'>
</form>
</td>
</tr>";
}
i have made a form in php and inside form there is a loop that extracts multiple values from database(img,name,availability,..) for multiple books i have made a table to display those values to user and after displaying these data in table i have made an issue button inside loop so that every book has its issue button.
My problem is that i have to only retrieve id of that book for which user click issue button. i tried storing it in cookie but it send the id of the first book displayed then i tried get method but that results in sending the last book that is displayed on screen id. but i want is that it should send the id of book which is selected by user
display books
echo "<form action='issue.php' method='get'>";
while ($row= mysqli_fetch_array($result)) {
echo "<div id='img_div' style='background-color:#fff;'> ";
echo "<img src='books/".$row['image']."'>";
// echo "</div>";<div id='text'>
$isbn=$row['isbn'];
echo "<input type='hidden' name='isb' value='$isbn' />";
echo " <table>";
echo "<tr><td> NAME</td><td> ".$row['name']."</td></tr>";
echo "<tr><td> AVAILABILITY</td><td> ".$row['availabilty']."</td></tr>";
echo "<tr><td> CATEGORY</td><td> ".$row['category']."</td></tr>";
echo "<tr><td colspan='2'>
<button type='submit' name='issue'>issue</button></td></tr>";
echo "</table>";
echo "</div><br/>";
if (isset($_GET['issue'])) {
# code...
$bookid=$isbn;
setcookie("bid",$bookid);
if(!isset($_COOKIE['bid'])){
echo "COOKIE NOT SET";
}
else{
echo "COOKIE SET SUCCESSFULLY";
}
}
issue.php(in which i want to send id)
if(isset($_GET['issue'])){
$bookid=$_GET['isb'];
$dbser="localhost";
$use="[redacted]";
$pasw="[redacted]";
$db="[redacted]";
$con=mysqli_connect($dbser,$use,$pasw,$db);
mysqli_select_db($con,$db)or die("db not connected");
$userid=$_COOKIE['id'];
$id=$_SESSION['user']['username'];
$query = "select id from user_account where username='$id'";
$result=mysqli_query($con,$query);
$row= mysqli_fetch_assoc($result);
$uid=$row['id'];
echo "$uid";
echo "<br/>";
echo "$bookid";
$query = "INSERT INTO issue (bookid, userid)
VALUES ('$bookid', '$uid')";
mysqli_query($con, $query)or die(mysqli_error($con));
What you need is a way to select a single row then submit that with the form. That can be accomplished by adding a Radio button to your table inside the form. The user will check the radio button for the item they want then click the submit button.
Here is an example of what that code could look like for your page.
display books
<form action='issue.php' method='get'>
while ($row= mysqli_fetch_array($result)) {
$isbn=$row['isbn'];
echo " <table>";
echo "<tr>";
echo "<td><input type=\"radio\" name=\"optradio\" value=\"".$isbn."\"></td>";
echo "<td> NAME</td><td> ".$row['name']."</td>";
echo "<td> AVAILABILITY</td><td> ".$row['availabilty']."</td>";
echo "<td> CATEGORY</td><td> ".$row['category']."</td>";
echo "</tr>";
echo "</table>";
}
echo "<button type='submit' name='issue'>issue</button>";
echo "</form>";
Here is a HTML snippet so you can see what that PHP code would output in HTML. Click "Run snippet code" below to see the preview.
<form action='issue.php' method='get'>
<table>
<tr>
<td><input type="radio" name="optradio" value="1"></td>
<td> NAME</td>
<td> name1</td>
<td> AVAILABILITY</td>
<td> availability1</td>
<td> CATEGORY</td>
<td> category1</td>
</tr>
<tr>
<td><input type="radio" name="optradio" value="2"></td>
<td> NAME</td>
<td> name2</td>
<td> AVAILABILITY</td>
<td> availability2</td>
<td> CATEGORY</td>
<td> category2</td>
</tr>
</table>
<button type='submit' name='issue'>issue</button>
</form>
Then in issue.php you would look for $_GET['optradio'] to get the selected value.
if(isset($_GET['issue'])){
$bookid=$_GET['optradio'];
$dbser="localhost";
...
...
...
function LoadProtocols(){
?>
<?php
$result=mysql_query("SELECT id,name,Destination_port, Destination_port_range_start, Destination_port_range_stop FROM protocols;");
echo '<table class="tftable" border="1">';
echo '<tr><th>protocol Name</th><th>Port</th><th></th></tr>';
echo "<form method='POST' action=''>";
if( $row=mysql_fetch_array($result)){
mysql_data_seek($result,0);
while( $row=mysql_fetch_array($result))
{
echo "<tr>
<td>".$row['name']."</td>
<td> "?><input type="hidden" id="id" name="id[]" value=" <?php echo $row['id']?>"> <input id="txt_port" name="txt_port[]" type="text" class="required" title="Port. This is a required field" value=" <?php echo $row['Destination_port'] ?> "></td>
<?php
echo "<td><a onclick='return show_confirm();'><input name='update' class='button' type='submit' value='Update'></a></td></tr>";
if(ISSET($_POST["update"]) and $_SERVER['REQUEST_METHOD'] == "POST"){
$id=$_REQUEST['id'];
$port=$_REQUEST['txt_port'];
$menu=$_GET['Menu'];
$result=mysql_query("UPDATE protocols SET Destination_port=".$port." WHERE id=".$id.";");
header("Location: Overview.php?Menu=".$menu."&Overview=1");
}
echo "</form>";
}
echo '<table>';
}
I made a simple table which as got 3 columns, the first column is a name, second one has a textbox which contains a number (with a hidden id that I need) and last one is a button to update that specific row.
The big problem I have that it only updates the last row, therefore I read that you needed to add [] to ur names and loop through it to update that specific row.
It's still not working and I can't figure out how to just let one specific row update with that [] array.
Thank you for reading and helping.
$_request['id'] is nothing but array ,also header should not be in loop
so Write This
mysql_data_seek($result,0);
$counter=0;
while( $row=mysql_fetch_array($result))
{
echo "<tr>
<td>".$row['name']."</td>
<td> "?><input type="hidden" id="id" name="id[]" value=" <?php echo $row['id']?>"> <input id="txt_port" name="txt_port[]" type="text" class="required" title="Port. This is a required field" value=" <?php echo $row['Destination_port'] ?> "></td>
<?php
echo "<td><a onclick='return show_confirm();'><input name='update' class='button' type='submit' value='Update'></a></td></tr>";
if(ISSET($_POST["update"]) and $_SERVER['REQUEST_METHOD'] == "POST"){
$id=$_REQUEST['id'];
$port=$_REQUEST['txt_port'];
$menu=$_GET['Menu'];
$result=mysql_query("UPDATE protocols SET Destination_port=".$port." WHERE id=".$id[$counter].";");
$counter++;
// header("Location: Overview.php?Menu=".$menu."&Overview=1");
}
Instead of this
mysql_data_seek($result,0);
while( $row=mysql_fetch_array($result))
{
echo "<tr>
<td>".$row['name']."</td>
<td> "?><input type="hidden" id="id" name="id[]" value=" <?php echo $row['id']?>"> <input id="txt_port" name="txt_port[]" type="text" class="required" title="Port. This is a required field" value=" <?php echo $row['Destination_port'] ?> "></td>
<?php
echo "<td><a onclick='return show_confirm();'><input name='update' class='button' type='submit' value='Update'></a></td></tr>";
if(ISSET($_POST["update"]) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$id=$_REQUEST['id'];
$port=$_REQUEST['txt_port'];
$menu=$_GET['Menu'];
$result=mysql_query("UPDATE protocols SET Destination_port=".$port." WHERE id=".$id.";");
header("Location: Overview.php?Menu=".$menu."&Overview=1");
}
So i guess i don't understand this i am a very newbie to coding in general. I have searched and can't find a good enogh explanation to get it to work in my situatation. I need to fill a table from a fetch command then update each result with an input of information into a new column. Here is the code i have:
This fills the table:
echo "<table border='1'>
<tr>
<th>Envelope</th>
<th>Budget</th>
<th>Amount</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount"></td><?php;
echo "</tr>";
}
echo "</table>";
?>
<input type="submit">
And this writes the input for budgetname into the column budgetname:
$paycheckname = mysqli_real_escape_string($con, $_POST['paycheckname']);
$budgetamount = mysqli_real_escape_string($con, $_POST['budgetamount']);
$envelopename = mysqli_real_escape_string($con, $_POST['envelopename']);
}
$sql="UPDATE envelopes SET $paycheckname='$budgetamount' WHERE envelopename ='$envelopename'";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
Right now it just doesn't write anything. I have used echo var_dump($envelopename)."<br>"; and echo var_dump($budgetamount)."<br>"; to try and see what it is doing but $envelopename is always blank. Thanks for any help you can provide.
only <input> , <textarea>, <select> and <button> are submitted to the server in a form
If you want to re-submit some static values create some hidden inputs
while($row = mysqli_fetch_array($result)) {
echo "<input type='hidden' value='$row[envelopename]' name='envelopename'/>";
echo "<input type='hidden' value='$row[envelopebudget]' name='envelopebudget'/>";
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount"></td><?php;
echo "</tr>";
}
echo "</table>";
But your next issue will be that you're creating the elements in a loop so you'll have multiple inputs with the same name
<tr>
<input type='hidden' value='envName1' name='envelopename'/>
<input type='hidden' value='envBudget1' name='envelopename'/>
<td>envName1</td>
<td>envBudget1</td>
<td><input type="text" name="budgetamount"></td>
</tr>
<tr>
<input type='hidden' value='envName2' name='envelopename'/>
<input type='hidden' value='envBudget2' name='envelopename'/>
<td>envName2</td>
<td>envBudget2</td>
<td><input type="text" name="budgetamount"></td>
</tr>
so you must submit as an array
while($row = mysqli_fetch_array($result)) {
echo "<input type='hidden' value='$row[envelopename]' name='envelopename[]'/>";
echo "<input type='hidden' value='$row[envelopebudget]' name='envelopebudget[]'/>";
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount[]"></td><?php;
echo "</tr>";
}
echo "</table>";
and at the server end process as an array
foreach ($_POST['budgetamount'] as $budgetamount){
echo $budgetamount. '<br>';
}
suppose the form rendered looks like this:
<tr>
<input type='hidden' value='envName1' name='envelopename[]'/>
<input type='hidden' value='envBudget1' name='envelopebudget[]'/>
<td>envName1</td>
<td>envBudget1</td>
<td><input type="text" name="budgetamount[]"></td>
</tr>
<tr>
<input type='hidden' value='envName2' name='envelopename[]'/>
<input type='hidden' value='envBudget2' name='envelopebudget[]'/>
<td>envName2</td>
<td>envBudget2</td>
<td><input type="text" name="budgetamount[]"></td>
</tr>
<tr>
<input type='hidden' value='envName3' name='envelopename[]'/>
<input type='hidden' value='envBudget3' name='envelopebudget[]'/>
<td>envName3</td>
<td>envBudget3</td>
<td><input type="text" name="budgetamount[]"></td>
</tr>
when the user hits submit the $_POST that arrives at newpaycheck.php will look like:
$_POST
['envelopename']{
[0]=>'envName1',
[1]=>'envName2',
[2]=>'envName3'
},['envelopebudget']{
[0]=>'envBudget1',
[1]=>'envBudget2',
[2]=>'envBudget3'
},['budgetamount']{
[0]=>'someValueEnteredByUser',
[1]=>'anotherValueEnteredByUser',
[2]=>'yetAnotherValueEnteredByUser'
}
so you can do something like this:
foreach ($_POST['envelopename'] as $envelopename){
$arrayIndex = array_search($envelopename,$_POST['envelopename']);
$envelopebudget = $_POST['envelopebudget'][$arrayIndex];
$budgetamount= $_POST['budgetamount'][$arrayIndex];
$paycheckname = mysqli_real_escape_string($con, $envelopebudget);
$budgetamount = mysqli_real_escape_string($con,$budgetamount);
$envelopename = mysqli_real_escape_string($con,$envelopename);
$sql="UPDATE envelopes SET $paycheckname='$budgetamount' WHERE envelopename ='$envelopename'";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
}
//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;