This question already has answers here:
Form inside a table
(3 answers)
Closed 4 years ago.
Hi to all stackoverflow users place the basic method directly in order to transmit data from one page to another via method = "POST" in PHP:
form.php:
<?php
session_start();
include '../connessione.php';
$id = $_SESSION['id'];
$query_string = "select * from ca2_2ac_clienti";
$query = mysqli_query($connessione, $query_string);
?>
<?php
while($row = mysqli_fetch_assoc($query)){ ?>
<form action="test.php" method="post">
<input name="text" value="<?php echo $row['id_cliente'] ;?>">
<input type="submit" name="invia">
</form>
<?php } ?>
test.php:
<?php $text = $_POST['text']; ?>
<form action="test.php" method="post">
<input name="text" value="<?php echo $text;?>">
<input type="submit" name="invia">
</form>
and up to here no problem, the problem arises when I want to implement this inside a table inside a while loop, ie inside my form in the value I do not see anything like it?
place the code:
<?php
while($row = mysqli_fetch_array($result))
{
?>
<!--Table body-->
<tbody>
<tr>
<th scope="row">
<label class="form-check-label" for="checkbox1" class="label-table"></label>
</th>
<form action="test.php" method="post">
<td style="vertical-align: middle;">
<input name="text" value="<?php echo $row['codice_impianto'] ;?>">
</td>
<span class="glyphicon glyphicon-edit"></span> MODIFICA
<span class="glyphicon glyphicon-trash"></span> ELIMINA
<input type="submit" name="invia">
</form>
<?php include('modali.php'); ?>
</td>
</tr>
</tbody>
<?php
}
}
else
{
echo 'Nessun risultato corrisponde alla tua ricerca';
}
?>
The <form> element is not allowed to be a descendant of <table>, <tr> or <tbody> elements. You should place the <form> around the entire <table> element
You should replace the <tbody> tags with <table> tags. The <tbody> tag can only be used inside a <table> tag, together with <thead>. Using it like this breaks your table layout.
You can read up on the proper use of this element at MDN.
Related
I´m passing a value of a radio button in a page with a form and I want to get the value of the selected option in another php file, but I can´t get that value.
First page:
<form class="itemSelection" action="../php/itemSelection1Save.php"><br><br>
<h1>Recommended Item(s)</h1>
<table class="itemTableRecom">
<thead>
<tr>
<th>Selected Item Type</th>
<th>Recommended Item Type</th>
</tr>
</thead>
<tbody>
<?php
foreach ($_SESSION['itemsInfo'] as $row) {
?>
<tr>
<td><input type="radio" name="selectedItem" id="selectedItem" required="" value="<?php echo $row[0] ?>"></td>
<td><?php
echo row[0]</td>
</tr>
<?php
}
?>
</tbody>
</table><br>
<input type="submit" value="Next"/>
</form>
Second file:
$_SESSION['selectedItem1'] = filter_input(INPUT_POST, 'selectedItem');
filter_input(INPUT_POST, 'selectedItem')
returns nothing
You form is with method get (the default method if not specified):
<form class="itemSelection" action="../php/itemSelection1Save.php">
insert method='post':
<form class="itemSelection" action="../php/itemSelection1Save.php" method='post'>
Make the following changes in HTML part , add method="post"
<form class="itemSelection" action="../php/itemSelection1Save.php" method="post">
Make the following change in PHP part (optional):
Also add filter_option in filter_input. filter the ID or name of the filter to use. Default is FILTER_DEFAULT, which results in no filtering.
filter_input(INPUT_POST, 'selectedItem',filter_option)
You Need To Add The Form Method you can try my code i simply code for you hopefully it will work!
<form class="itemSelection" method="post" action="../php/itemSelection1Save.php">
<h1>Recommended Item(s)</h1>
<table class="itemTableRecom">
<thead>
<tr>
<th>Selected Item Type</th>
<th>Recommended Item Type</th>
</tr>
</thead>
<tbody>
<?php
foreach ($_SESSION['itemsInfo'] as $row) {
?>
<tr>
<td><input type="radio" name="selectedItem" value="<?php echo $row[0]; ?>"></td>
<td><?php echo $row[0]; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<input type="submit" name="submit" value="Next">
</form>
I am making an editing form.
There are 2 forms, one form called edit1.php and another form called edit.php. This second one should get the id generated from the edit1.php and use it to generate further queries. The following is the code for the 2 files:
edit1.php:
<table>
<tr>
<td align="center"><h4><b>EDIT DATA</b></h4></td>
</tr>
<tr>
<td>
<table border="3">
<form action="edit.php" method="get">
<?php
$vendors = "SELECT `vendor`.`v_id`,`vendor`.`v_name` FROM `stfood`.`vendor`";
$result = mysqli_query($con, $vendors);
while ($row = mysqli_fetch_array($result)){
echo ("<tr><td>'".$row["v_id"]."'</td>");
echo ("<td>'".$row["v_name"]."'</td>");
echo ("<td>Edit</td></tr>");
}
?>
</form>
</table>
edit.php:
<?php
require 'init.php';
require 'functions.php';
?>
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<table border=1>
<tr>
<td align=center>Form Edit vendor Data</td>
</tr>
<tr>
<td>
<table>
<?php
echo $_REQUEST['v_id'];
$vendor = "SELECT `vendor`.`v_name`, `vendor`.`v_email`,`vendor`.`v_desc`,`vendor`.`cont_id` FROM `stfood`.`vendor` WHERE `vendor`.`v_id` = '".$v_id."'";
$vendor_result = mysqli_query($con, $vendor);
$row = mysqli_fetch_array($vendor_result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<?php echo $row["v_id"];?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name" size="20" value="<?php echo $row["v_name"]; ?>">
</td>
</tr>
<tr>
<td>Email</td>
<td>
<input type="text" name="email" size="50" value="<?php echo $row["v_email"];?>">
</td>
</tr>
<tr>
<td>Vendor Description</td>
<td>
<input type="text" name="description" size="100" value="<?php echo $row["v_desc"];?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>
When I run the code, the first form displays all the relevant data, and when I click on the edit link the page gets redirected and I can see the v_id passed in the URL, but nothing comes into the edit.php file. When I do a var_dump($row['v_id']); I get NULL.
Why is v_id not set in edit.php?
Since your hyperlink looks like this:
Edit
Your edit.php should use
echo $_GET['id'];
// or something like this
$received_id = $_GET['id'];
// do validation whether you received a good id or not and then move on
...
...
<input type="hidden" name="id" value="<?php echo $received_id;?>">
on your edit.php page you have:
Edit
now on your edit1.php you should first have a isset() and then as follow:
if(isset($_GET['id']){
$id = $_GET['id'];
//carry on blah3 ...
In the edit1.php you have not used form elements. The value $row['v_Id'] should be the value of form element input like this inside form
<input type='hidden' name='v_I'd value ='<?php echo $row['v_id'] ?>'>
Try using $_GET['id'] instead of $_REQUEST['v_id'] on edit.php
See example #1 here
Also, have you defined the $v_id before using it in the query?
Like this.
I fetch SQL data with a while loop and insert the data into a table. With a form and a submit button i can save modified values.
My simplified code:
<table>
<tr>
<?php
//SQL QUERY
//...
while ($row = mysql_fetch_array($sql_header)) {
$var_ab = $row['ab'];
$var_id = $row['id'];
?>
<form id="form1" action="" method="POST">
<td>
<input type="text" value="<?php echo $var_ab; ?>"
</td>
<td>
<input type="text" value="<?php echo $var_id; ?>"
</td>
//PLACEHOLDER FOR SECOND FORM
<?php
}
?>
</tr>
<td>
<input type="submit" name="save" value="SAVE" class="classsubmit" />
</td>
</form>
</tr>
</table>
So far, so good. So, how can I insert a second form to delete an entry? I've tried to place this code (PLACEHOLDER FOR SECOND FORM - see above)
<td>
<form id="form2" action="" method="POST">
<input type="text" value="<?php echo $var_id;?>"
</form>
</td>
but it's not working and it's not allowed to nest forms.
Any suggestions?
If you only want to delete an entry on the page or in the database you could try a button or a span with an onclick function.
for example:
<span onclick="window.location='?delete=<?php echo $row[(unique-value-in-database-from-this-row)]; ?>'; return false">Delete entry</span>
Make sure you add return false or the first form will be submitted. If you use a button make sure it has type="button"
On this page could be a PHP code like this:
if(isset($_GET['delete']))
{
$item = $_GET['delete'];
//SQL connect
$result = mysql_query($conection ,"DELETE FROM table WHERE uniquevalue='$item'");
}
I hope gives an idea for a solution.
I have been working on this for probably 20 hours of research and trial/error with no solution in sight I figured I'd try here and see if someone can finally point me in the right direction and give me some solid advice.
Here's the setup. I have a page (customer_search.php) that has an HTML form, I want the user to be able to search the DB by $last_name and the results be displayed on a table on the same page.
First: IS THIS POSSIBLE? I have read so much over the past few days, I doubt myself but then I think it can be done without using Java and using purely PHP/HTML.
I also use MVC model.
THIS IS THE MAIN PAGE (customer_search.php)
<?php include '../view/header.php';
?>
<div id="main">
<h1>Customer Search</h1>
<div id="content">
<form action="" method="post" id="aligned"
<label>  Last Name</label>
<input type="text" name="last_name"/>
<br />
<label> <label>
<input type="submit" value="Search" />
</div>
<div id="content">
<h2>Results</h2>
<table>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>City</th>
<th> </th>
</tr>
<tr>
<td><?php echo $_POST['firstName'] .' '. $_POST['last_name']; ?></td>
<td><?php echo $_POST['email']; ?></td>
<td><?php echo $_POST['city']; ?></td>
<td><form action="customer_display.php" method="post">
<input type="hidden" name="action" value="get_customer" />
<input type="hidden" name="customer_id"
value="<?php echo $_POST['customer_ID']; ?>" />
<input type="submit" value="Select" />
</form>
</td>
</tr>
</table>
<br />
</div>
</div>
<?php include '../view/footer.php'; ?>
THIS IS customer_db.php in the MODEL folder which contains a function that I'd like to use, get_customers_by_last_name($last_name)
<?php
function get_customers() {
global $db;
$query = 'SELECT * FROM customers
ORDER BY lastName';
$customers = $db->query($query);
return $customers;
}
function get_customers_by_last_name($last_name) {
global $db;
$query = "SELECT * FROM customers
WHERE lastName = '$last_name'
ORDER BY lastName";
$customers = $db->query($query);
return $customers;
}
I apologize for the OVERLOAD of code, but this is my first post on here and I've tried everything I can think of. I just want to search the DB by last name on the html form and then have the results displayed on the table (on the same page) and I don't care if the page has to refresh. I know PHP is server-side and not client so it needs to refresh. I just can't seem to get the results to actually display in the table, if I could just get the results there, the next step is to SELECT the customer which passes it to the next page customer_display.php but I'll cross that bridge when there. Thanks for your help, and I really am begging for some help/lessons on this. I'm desperate!!!! :(
Hi you did your homework and are correct that the page HAS to be refreshed.
Unless you plan to use an Ajax call with javascrcipt (jQuery for exemple)
To do what you ask, you have to put the PHP at the beginning.
You put it in an If() statement that will only be valid if the form has been posted.
Here is what I would do with your code:
<?php
if(isset($_POST['last_name'])){
include_once 'customer_db.php';
$customers = get_customers_by_last_name($_POST['last_name']);
}
include '../view/header.php';
?>
<div id="main">
<h1>Customer Search</h1>
<div id="Search">
<form action="" method="post" id="aligned"
<label>  Last Name</label>
<input type="text" name="last_name"/>
<br />
<label> <label>
<input type="submit" value="Search" />
</form>
</div>
<?php if(isset($customers)){ ?>
<div id="Results">
<h2>Results</h2>
<table>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>City</th>
<th> </th>
</tr>
<tr>
<?php while ($a_customer = mysql_fetch_assoc($customer)) { ?>
<td><?php echo $a_customer['firstName'] .' '. $a_customer['last_name']; ?></td>
<td><?php echo $a_customer['email']; ?></td>
<td><?php echo $a_customer['city']; ?></td>
<td><form action="customer_display.php" method="post">
<input type="hidden" name="action" value="get_customer" />
<input type="hidden" name="customer_id"
value="<?php $a_customer['customer_ID']; ?>" />
<input type="submit" value="Select" />
<?php } ?>
</td>
</tr>
</table>
<br />
</div>
<?php }?>
</div>
<?php include '../view/footer.php'; ?>
Some crucial advices:
Use Mysqli (or PDO) library instead of Msql for php
you should not trust user input data on your search form you can use the janitor.class to sanitize the post.
When you have more time check these links concerning PHP security : Evil User, PHP security , PHP Secure coding
Oh! and don't give two elements in the same page the same id Value the purpose of the id is to give each element a unique identifier.
I have had success doing this same thing in the past using JQuery and jTable.
This uses AJAX calls which allows you to call PHP scripts without reloading the page.
You can find out everything you need to know here: http://www.jtable.org/
Use AJAX to make a call to a PHP object that instantiates your model and returns your results in a JSON string. Upon receiving the results, use a JavaScript loop to iterate through the JSON and output your table.
I recommend jQuery, which has its own .ajax() function, and it's easy to manipulate the DOM with your result set. Go read the jQuery docs.
This is what AJAX is for... asynchronous communication with the server (i.e. not on a page load). Your best bet would probably be to use JQuery or another JS framework, which will make the AJAX calls a snap: $.ajax();:
Something like:
$.ajax({
type: "POST",
url: '/directory/to/php_script_that_outputs_the_table_html.php',
data: { name: "Smith"},
success: function(data) {
$('.class_of_div_to_recieve_code').html(data);
}
});
My Plugin form code:
function pp_settings_page() {
$pp_options = get_option('pp_options');
?>
<div class="wrap">
<h2>Post Products Settings</h2>
<form action="options.php" method="post">
<?php settings_fields('pp_options_group'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Currency sign: </th>
<td><input type="text" name="pp_options[currency_sign]" value="<?php echo $pp_options['currency_sign']; ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="Save changes" />
</p>
</form>
</div>
<?php
}
I have tried to call it within the template files using:
<?php $pp_options=get_option('pp_options');?>
and
<?php get_option('pp_options');?>
What am I missing?
I don't see any code to handle the form submit, more specifically, code which extracts the post variable and saves it using update_option
Your action would need to be changed to the settings page URL so when it gets posted it runs the next bit of code, which you would put inside your pp_settings_page function.
if(isset($_POST['field_name'])) {
$field_value = $_POST['field_name'];
update_option('option_name', $field_value);
}