Form in array doesn't show button after if-statement - php

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

Related

Refuses to get data

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>

Data from table to edit doesnt work mysqli and GET/POST

Im not a PHP pro i started php 3 weeks ago my knowledge is low.
My Problem:
In my outdex.php there is table with content read out of my database.
if i click the icon to edit a column i get to edit.php
But it doesnt shown my editable column.
My Code:
outdex.php
<?php
require('config/config.inc.php');
$sql = "SELECT id, name, Status, Strasse, Telefon, ProblemBeschreibung, Datum FROM probleme WHERE id = 1 OR id > 1";
$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) > 0) {
// Ausgabe der Daten in einer row
echo '<table class="bordered"><thead>';
echo '<tr>';
echo '<th style="display: none;">ID</th>';
echo '<th>Name</th>';
echo '<th>Status</th>';
echo '<th>Straße</th>';
echo '<th>Telefon</th>';
echo '<th>Problembeschreibung</th>';
echo '<th>Eintragsdatum</th>';
echo '<th width="36"></th>';
echo '</tr></thead>';
echo '<tbody>';
while($adr = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>" . $adr['name'] . "</td>
<td>" . $adr['Status'] . "</td>
<td>" . $adr['Strasse'] . "</td>
<td>" . $adr['Telefon'] . "</td>
<td>" . $adr['ProblemBeschreibung'] . "</td>
<td>" . $adr['Datum'] . "</td>
<td>
<div id=\"test\">
<div id=\"div1\"><form action='edit.php' method='GET'>
<input type='hidden' name='id' value='" .htmlspecialchars($adr['id'], ENT_COMPAT, 'UTF-8') . "'/>
<input type='hidden' name='name' value='".htmlspecialchars($adr['name'], ENT_COMPAT, 'UTF-8')."'/>
<input type='hidden' name='Status' value='".htmlspecialchars($adr['Status'], ENT_COMPAT, 'UTF-8')."'/>
<input type='hidden' name='Strasse' value='".htmlspecialchars($adr['Strasse'], ENT_COMPAT, 'UTF-8')."'/>
<input type='hidden' name='Telefon' value='".htmlspecialchars($adr['Telefon'], ENT_COMPAT, 'UTF-8')."'/>
<input type='hidden' name='ProblemBeschreibung' value='".htmlspecialchars($adr['ProblemBeschreibung'], ENT_COMPAT, 'UTF-8')."'/>
<input type='hidden' name='Datum' value='".htmlspecialchars($adr['Datum'], ENT_COMPAT, 'UTF-8')."'/>
<input type='image' src='edit.png' height='16' width='16' name='edit' value='Bearbeiten' onClick=\"self.location.href='edit.php?=\">
</form></div>
<div id=\"div2\">
<form action='delete.php' method='POST'>
<input type='hidden' name='id' value='" . htmlspecialchars($adr['id'], ENT_COMPAT, 'UTF-8') . "'/>
<input type='image' src='delete.png' height='16' width='16' name='delete' value='Löschen' onClick=\"self.location.href='delete.php?\">
</form></div>
</div>
</td>
</tr>\n";
}
echo "</tbody></table>";
} else {
echo "0 Einträge gefunden<br>Es konnten keine Einträge in der Datenbank gefunden werden.";
}
mysqli_close($db);
?>
My edit.php
<table class="bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Status</th>
<th>Strasse</th>
<th>Telefon</th>
<th>Problembeschreibung</th>
<th>Eintragsdatum</th>
</tr>
</thead>
<?php
include('config.inc.php');
if(isset($_GET['id'])){
$id = $_GET['id'];
} else {
die('Bitte eine ?id übergeben');
}
$statement = $pdo->prepare("SELECT * FROM probleme WHERE id = ?");
$statement->execute(array($id));
while($row = $statement->fetch()){
echo "<tr>
<form method='post' action='edit1.php'>
<td><input name='id' type='text' value='".$id."'</input></td>
<td><input name='name' type='text' value='" .$row['name']."'</input></td>
<td>
<select name='Status'>
<option>".$row['Status']."</option>
<option>FINISHED</option>
</select>
</td>
<td><input name='Strasse' type='text' value='".$row['Strasse']."'</input></td>
<td><input name='Telefon' type='number' value='".$row['Telefon']."'</input></td>
<td><input name='ProblemBeschreibung' type='text' value='".$row['ProblemBeschreibung']."'</input></td>
<td>".$row['Datum']."</td>
</tr>
</table>";
echo "
<input type='submit' value='Absenden' />
</form>";
}
?>
If I change GET to POST it doesnt work too.
hope you can help me.

Php loop thourgh array of Request ids

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

not understanding mysqli_fetch_array

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));
}
}

problem with enable/disable button using javascript

Im am trying to add a check box that will enable/disable the edit button. Im retrieving a price list and displaying it inside a table. When i add the javascript into the php code it doesn't work. Below is my code
<table border="1">
<tr>
<td width="100">Fee % </td>
<td width="100">Price</td>
<td width="100">Total</td>
<td width="102"> </td>
</tr>
<tr>
<?php
$sql1="select * from pricelist";
$result1=mysql_query($sql1) or die(mysql_error());
while ($row=mysql_fetch_array($result1)) {
$id=$row['id'];
$price=$row['h_price'];
$a=0;
print "<form id='form1' name='$a+' method='post' action=''>";
print "<td><input name='fees' value ='$fees' type='text' size='4' /></td>";
print "<td><input name='price' value ='$price' type='text' size='15' /></td>";
echo "<td><input type='checkbox' onclick='this.$a+.disabled = !this.checked;'><td>";
print"<td><input type='submit' name='$a+' value='Submit' disabled='disabled' /></td>";
print "</tr>";
print "</form>";
}
?>
</table>
Can someone please tell me what am i doing wrong?
Thanks
take out the following code out of loop... it creates multiple form tag...
print "<form id='form1' name='$a+' method='post' action=''>";
Place "<tr></tr>" inside the loop...
final code looks like:
<form id='form1' name='fm1' method='post' action=''> <tr>
<?php
$sql1="select * from pricelist";
$result1=mysql_query($sql1) or die(mysql_error());
while ($row=mysql_fetch_array($result1)) {
$id=$row['id'];
$price=$row['h_price'];
$a=0;
print "<tr>";
print "<td><input name='fees' value ='$fees' type='text' size='4' /></td>";
print "<td><input name='price' value ='$price' type='text' size='15' /></td>";
echo "<td><input type='checkbox' onclick='this.$a+.disabled = !this.checked;'><td>";
print"<td><input type='submit' name='$a+' value='Submit' disabled='disabled' /></td>";
print "</tr>";
}
print "</form>";
?>
Next if u want to control the button only then
<input type='checkbox' onclick='document.$a.submit.disabled = !this.checked;' />
and make sure of the following things:
1.) form name should be $a
i.e <form name='$a' ...>
2.) submit buttons name should be submit
i.e <input type='submit' name='submit'...>
3.) increase the $a variable only at the end of loop
i.e
$a++;
}
i think $a+ will cause syntax error. you need to increment $a at the end of loop like $a++. Also see page source and see what is coming from server.
I would recommend using following format for html + php output
<form id='form1' name='<?=$a++?>' method='post' action=''>
<tr>
<td><input name='fees' value ='<?=$fees?>' type='text' size='4' /></td>
<td><input name='price' value ='<?=$price?>' type='text' size='15' /></td>
<td><input type='checkbox' onclick='this.disabled = !this.checked;'><td>
<td><input type='submit' name='<?=$a++?>' value='Submit' disabled='disabled' /></td>
</tr>
</form>
<?
you also need to have < tr > to be in the while loop.
In your html part where you set up the formular you have to use " " to escape the php code. example:
print "<form id='form1' name='".$a+."' method='post' action=''>";
But as Adeel said already, what is $a+ ?
And you have to edit all prints and echoes in the way, that php expressions are excaped from the HTML source.

Categories