I have a table with some information from my database. I have buttons next to each row in the table. If the button is clicked it should send an email to the user. I can give each button a unique value but can not have this value in the if isset.... How should i get the value of the button name to be variable and how could i get the value at the end of my url.
echo "<table>";
echo "<tr><td>Naam</td><td>EmailAdress</td><td>Datum</td><td>Type</td><td>Producent</td><td>SerieNummer</td><td> imei</td><td>Manager</td><td>bevestigd</td></tr>";
while($row = mysqli_fetch_row($result)){
$sqlid = "SELECT Bevestigd FROM tbl_bevestiging WHERE IDbewijs = " . $row[0];
$resultid = mysqli_query($conn, $sqlid);
$row2 = mysqli_fetch_row($resultid);
echo "<tr><td>" . $row[1] . "</td><td>" . $row[2] . "</td><td>" . $row[3] . "</td><td>" . $row[4] . "</td><td>" . $row[5] . "</td><td>" . $row [6] . "</td><td>" . $row[7] . "</td><td>" . $row[8] . "</td><td>" . $row2 [0] . "</td><td><form method=\"post\"><input name=\"" . $row[0] . "\" type=\"submit\" value=\"" . $row[0] . "\"</form></td></tr>";
}
echo "</table>";
if (isset($_POST[$row[0]])){
$admin_email = "adminmail#mail.com," . $row[2];
mail($admin_email, "Ontvangstbewijs geleverde hardware herverstuurd", "url?id=" . $row [0] , "From:" . "example#email.com");
mysqli_close($conn);
}
?>
Theres a few things I think are worth pointing out firstly.
Table header tags should be in a THEAD tag. See example here.
Try and avoid SQLs within a loop. If possible create a SQL that does everything for you to reduce the number of queries sent to MySQL.
Ok back to your question. Personally, what I would do is have a FORM tag in a TD tag. This way each button can have it's own unique id set in a hidden INPUT tag.
For example:
<tr>
<td>
<form action="[[YOUR-PHP-FILE]]" method="post">
<input type="hidden" name="id" value="[[ID]]" />
<input type="submit" value="Submit" />
</form>
</td>
</tr>
That way you don't need to focus on the submit button but rather the id.
Related
You are going to send the full line value using form tag. However, if you send a value, only the value in the last row is sent. How should it be modified?
in this table code
echo "<form action='../verification/medical_bills_check.php' method='post'>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td> <input type='hidden' name='id' value ='" . $row['id'] . " '>" . $row['id'] . "</td>";
echo "<td> <input type='hidden' name='storename' value ='" . $row['storename'] . " '>" . $row['storename'] . "</td>";
echo "<td> <input type='hidden' name='corporate_num' value ='" . $row['corporate_num'] . " '>" . $row['corporate_num'] . "</td>";
echo "<td> <input type='hidden' name='store_mbrno' value ='" . $row['store_mbrno'] . " '>" . $row['store_mbrno'] . "</td>";
echo "<td> <input type='hidden' name='store_mbrno_van' value ='" . $row['store_mbrno_van'] . " '>" . $row['store_mbrno_van'] . "</td>";
echo "<td ><input type='hidden' name='past_cash_receipts' value ='" . $row['past_cash_receipts'] . " '>" . $row['past_cash_receipts'] . "</td>";
echo "<td> <input type='hidden' name='past_card' value ='" . $row['past_card'] . " '>" . $row['past_card'] . "</td>";
echo "<td> <input type='hidden' name='pg_select' value ='" . $row['pg_select'] . " '>" . $row['pg_select'] . "</td>";
echo "<td> <input type='hidden' name='program_select' value ='" . $row['program_select'] . " '>" . $row['program_select'] . "</td>";
echo "<td> <input type='hidden' name='authority' value ='" . $row['authority'] . " '>" . $row['authority'] . "</td>";
echo "<td> <input type='hidden' name='apikey' value ='" . $row['apikey'] . " '>" . $row['apikey'] . "</td>";
echo "<td > <input type='submit'> </td>";
echo "</tr>";
}
echo "</form>";
and receive page code
$_SESSION["corporate_num"] = $_POST["corporate_num"];
$_SESSION["store_mbrno"] = $_POST["store_mbrno"];
$_SESSION["store_mbrno_van"] = $_POST["store_mbrno_van"];
$_SESSION["program_select"] = $_POST["program_select"];
$_SESSION["authority"] = $_POST["authority"];
$_SESSION["apikey"] =$_POST["apikey"];
I want result
id | corporate_num | store_mbrno | submit
1 1 1 submit_button
2 2 2 submit_button
3 3 3 submit_button
4 4 4 submit_button
When I submit id 1 rows.
Received Page receive value id:1 / corporate_num:1/ store_mbrno:1
When I submit id 2 rows.
Received Page receive value id:2 / corporate_num:2/ store_mbrno:2
If I understood your question correctly, the issue is that you have a lot of input fields with the same name within one form. What happens when you click one of the buttons, is that all the data in the whole form gets sent. And, because every row has fields with the same name, the values get overwritten by the next row. Therefore only the data of the last row is sent.
If you only want to send the data of one row, you need to have the data in a separate form. So, one form for each row. Since, technically, you can't have a form element directly inside a table or tr element, I suggest putting the form element and all hidden inputs in the last table cell.
Furthermore, since the contents of the hidden inputs can be modified (you can't trust your visitors not to use the inspector), you should not trust the information that is sent. Instead, if possible, only send the id, and fetch the other information anew from the database.
Shoots me to the /index.php with a error 404. I don't want it to navigate to another page, just display the results in a table.
I am not really sure what I am doing wrong, I just started php and have been using w3 to navigate the syntax. I managed to make a plugin that would display the a entire table but now I am trying to use a form to search a table and display the results.
...
if( isset($_GET['submit']) ){
$ssterm = $_POST["name"];
$query = "SELECT ID, Retailer, City, State, Contact, Address1, Address2, Country, Zip, Email, Website FROM Stores WHERE Retailer LIKE '%" . $rssterm . "%' OR City LIKE '%" . $ssterm ."%'";
$result = $conn->query($query);
if($result->num_rows > 0){
echo "<table>
<tr>
<th>ID</th>
<th>Retailer</th>
<th>City</th>
<th>State</th>
<th>Contact</th>
<th>Address1</th>
<th>Address2</th>
<th>Country</th>
<th>Zip</th>
<th>Email</th>
<th>Website</th>
</tr>";
while($row = $result->fetch_assoc()){ //Creates a loop to loop through results
echo "<tr><td>" . $row['ID'] . "</td><td>" . $row['Retailer'] . "</td><td>" . $row['City'] . "</td><td>" . $row['State'] . "</td><td>" .
$row['Contact'] . "</td><td>" . $row['Address1'] . "</td><td>" . $row['Address2'] . "</td><td>" . $row['Country'] . "</td><td>" . $row['Zip'] .
"</td><td>" . $row['Email'] . "</td><td>" . $row['Website'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
}
else {
echo "No Results Found...";
}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="search">
</form>
<?php
$conn->close(); //Make sure to close out the database connection
} // end of a shortcode brace
?>
I thought this would for sure work, but it just shoots me to the /index.php with a error 404. I don't want it to navigate to another page, just display the results in a table. I am using this on a wordpress site and as a plugin using a shortcode to implement it into a page.
How can I create an SQL statement that will only show the options based upon one of the three options selected (C/E/W). At the moment, it just shows every result.
Also, how do i do this so the result will drop below once a C/E/W has been selected without the need for a submit button?
Any help would be appreciated.
<select id="combobox">
<option value="Central" id="C">C</option>
<option value="East" id="E">E</option>
<option value="West" id="W">W</option>
</select>
</p>
</div>
<div>
<?php
$query = "SELECT * FROM ROOMS";
$result = mysql_query($query);
if ($result == FALSE) die ("could not execute statement $query<br />");
echo "<table>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['roomCode'] . "</td><td>" . $row['Park'] . "</td><td>" . $row['Capacity'] . "</td><td>" . $row['Style'] . "</td><td>" . $row['dataProjector'] . "</td><td>" . $row['Whiteboard'] . "</td><td>" . $row['OHP'] . "</td><td>" . $row['wheelchairAccess'] . "</td><td>" . $row['lectureCapture'] . "</td></tr>";
}
echo "</table>";
mysql_close();
?>
Trying:
take info from my database to texfields (infinite database data so cant name fields in hardcore mode each by each), then change data and put it back to database.
Problem:
Can't name my textfields in order to check if data was changed.
Code:
<html>
<head>
<title>Data</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
$conn = mysql_connect('localhost', 'root', '');
$base = 'CREATE DATABASE itemsbase';
$select = mysql_select_db("itemsbase",$conn);
$table_items = 'CREATE TABLE `items` (
`items_id` int UNIQUE NOT NULL,
`shop_id` int,
`itemname` varchar(40),
`price` varchar(50),
PRIMARY KEY(items_id))';
$query = "SELECT * FROM items";
$result = mysql_query($query);
if(!$select){
if(mysql_query($base, $conn)){
echo "base created";
}else{
echo 'error: ' . mysql_error() . "\n";
}}
if(mysql_query($table_items, $conn)){
echo "table created";
}else{
echo 'error: ' . mysql_error() . "\n";
}
echo "<form method='post'><table>";
echo "<tr><td>" . 'item id' . "</td><td>" . 'shop id' . "</td><td>" . 'item name' . "</td><td>" . 'price' . "</td></tr>";
while($row = mysql_fetch_array($result)){
echo "<tr><td><input type='text' name='itemid" . $row[1] . "' value='" . $row['items_id'] . "'></td><td><input type='text' name='shop_id" . $row[1] . "' value='" . $row['shop_id'] . "'></td><td><input type='text' name='itemname" . $row[1] . "' value='" . $row['itemname'] . "'></td><td><input type='text' name='price" . $row[1] . "' value='" . $row['price'] . "'></td></tr>";
//and here trying take info from field and check if it mach with info in database
$fieldinfo = $_POST['itemid' . $row[1]];
if($fieldinfo=$row['items_id']){
}else{
$row['items_id']=$fieldinfo;}
//but facing error undefined index
}
echo "</table></form>";
mysql_close ();
?>
</body>
</html>
name the text field as an array with the record ID so you will be able to relate that back to the data base.
<input type='text' name='item[<?=$row['id']?>]' value='<?=$row['value']'>
or you can add a custom attribute to the text feild with the table record ID.
<input type='text' name='item' rowid='<?=$row['id']?>' value='<?=$row['value']?>'>
either way can be reference by jquery to check for update and update database.
I have a form embedded in table that is created on the initial page load:
echo "<form id='showbooking' action='calendar.php' method='post'>";
echo "<td name='clickedcalid' value='" . $resarr[$k]['calid'];
echo "' title='" . $resarr[$k]['coms'] . "' class='hols'>";
echo $resarr[$k]['typ'] . "</td></form>";
I submit the form when a cell in the table is clicked and want to pass the value of the cell that was clicked.
<script>
$(".hols").click(function() {
$("#showbooking").submit();
})
</script>
After the reload I am testing with this:
echo "<p> ID of clicked booking was: " . $_POST['clickedcalid']; . "</p>";
but the value is not being passed and I'm not sure why.
Possibly because the form is embedded in a table?
Firstly, the form element needs to go inside the td. Secondly, only the values of form elements are sent in a form submission, ie input, select, textarea etc. Try this:
echo '<td class="hols" title="' . $resarr[$k]['coms'] . '">';
echo '<form id="showbooking" action="calendar.php" method="post">';
echo '<input type="text" name="clickedcalid" value="' . $resarr[$k]['calid'] . '" />';
echo $resarr[$k]['typ'] . '</form></td>';
You can't put <form> inside a tr element, wrapping td. <tr> specification says
Permitted content: Zero or more <td> or <th> elements, or a mix of them
Also value attribute is not valid for td element. You need to restructure your markup.
echo '<td class="hols" title="' . $resarr[$k]['coms'] . '">' .
'<form id="showbooking" action="calendar.php" method="post">' .
'<input type="hidden" name="clickedcalid" value="' . $resarr[$k]['calid'] . '">' .
$resarr[$k]['typ'] .
'</form>' .
'</td>';
To get value from $_POST you need to have input tag
echo "<form id='showbooking' action='calendar.php' method='post'>";
echo "<td><input type='text' name='clickedcalid' value='" . $resarr[$k]['calid'];
echo "' title='" . $resarr[$k]['coms'] . "' class='hols' />";
echo $resarr[$k]['typ'] . "</td></form>";
Thanks,
Naumche