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);
}
Related
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.
I'm trying to echo a value from a HTML form to another page where I have a table, using PHP. Firstly, would I need any code before the table, e.g.
<?php
$_GET[hello.htm]
?>
<table>
etc...
Secondly, where would I put the PHP code? Inside each
<td></td> ?
Thirdly, how would the PHP be in order to retrieve the value from say the third box (you can type in them) in the form? Would it be:
$_GET[hello.htm,2]?
You need an action that get values as POST type and of course a SUBMIT trigger.
PHP codes can be put anywhere.
You can get each value from inputs by inputs' IDs or NAMEs.
you could show all sended values like so:
<?php
var_dump($_GET);
var_dump($_POST);
?>
Then when you know what exactpy you wana show (for example if this is in $_GET superglobal:
<?= $_GET['key_name']; ?>
or
<?php echo $_GET['key_name']; ?>
For example
<form action="other_file.php" method="POST">
<input type="text" name="my_input_name" />
<input type="submit" value="GO!" />
</form>
Will generate
$_POST['my_input_name']
So you could use it as:
<?php echo $_POST['my_input_name']; ?>
Maybe we need 2 PHP files
index.php
result.php
index.php
<html>
<body>
<form action="result.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
result.php
<html>
<body>
<h3> Result: </h3>
<table border="1">
<tr>
<td> Name </td>
<td> Email </td>
</tr>
<tr>
<td> <?php echo $_POST["name"]; ?> </td>
<td> <?php echo $_POST["email"] ?> </td>
</tr>
</table>
</body>
</html>
in index.php we use form action to send data to result.php
in result.php we use $_POST['form_input_name'] to get data from form input
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);
}
});
I've a form in which I send the values to be calculated to another PHP page which should do the calculation and return the values to this form page. If there is an error, it should return the error to this main page. How would I do that?
This is the basic form page: index.php:
<form id="deps" name="deps" action="calc.php" method="POST">
<div id="er"> <span class="er" > </span></div>
<table class="table">
<tr>
<td class="field head"> </td>
<td class="field cv"><label for="met">Metric</label></td>
</tr>
<tr>
<td class="field head"><label for="bd">Bore Dia.</label></td>
<td class="field"><input type="text" name="bdmm" id="bdmm" /><label for="bdmm">MM</label></td>
<tr>
<td class="field head"><label for="tw">Total Weight</label></td>
<td class="field"><input type="text" name="twkg" id="twkg" /> <label for="twkg">KG</label></td>
</tr>
</tr>
</table>
<input type="submit" id="submit" value="Calculate" />
</form>
</div>
Calc.php
<?php
$bdmm = $_POST['bdmm'];
if(!$bdmm){
$error = "Please enter a value!";
echo $error;
}
else {
echo $bdmm ." success";
}
?>
If there is an error it should display the error in the span tag. And if there is no error and it calculates, it should return the result and display it in the textbox.
Is this possible without Javascript?
Sure:
index.php:
<?php
$err = (isset($_GET['error'])) ? $_GET['error'] : ' ';
?>
<form id="deps" name="deps" action="calc.php" method="POST">
<div id="er"> <span class="er" ><?php echo $err; ?></span></div>
....rest of form
calc.php:
if(empty($_POST['bdmm'])){
header('Location: index.php?error=Please enter a value!');
exit();
}
else {
echo $_POST['bdmm'] ." success";
}
While this works just fine, I'd recommend you simply post the form to the same page and avoid the url redirection. If you use an MVC framework such as CodeIgniter or Zend, you can take advantage of unique urls within the same file and tell the controller to load the appropriate view while keeping the request on the same file.