I've not done any coding in a while, but needed a quick way to send an email to a few people at a time with using two variables. Should be simple, but I have no idea why this isn't working.
Thanks in advance.
<?php
if(!empty($POST['update']))
{
echo 'it works!';
}
else
{
?>
<h1>Order Confirmation</h1>
<form method="post" action="order-confirmation.php" name="update">
<table>
<tr>
<td>Account Number</td>
<td>Consignment Number</td>
</tr>
<tr>
<td><input type="text" name="accno" value=""/></td>
<td><input type="text" name="conno" value=""/></td>
</tr>
<tr>
<td><input type="submit" name="submit" action="order-confirmation.php"/></td>
</tr>
</table>
</form>
<?php
}
?>
It should be
$_POST
Instead of
$POST
Also, you want it to be $_POST['submit'] instead of update.
You do not have input field called "update".
You must also replace $POST with $_POST and add an <input type="hidden" name="update" value="1" /> to your form.
type="submit" does not need action attribute because you already have defined action in your <form>.
I usually use something like this (anything with // before mean comment, not executable code)
//Request method detect that POST is used not GET which mean the form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST) {
// This condition detect that the input with name "submit" is pressed, you can
// add multiple submit buttons and each with different value, then just do
// equality check to specify the actions
if ($_POST['submit']) {
echo 'it works!';
}
}
Related
Admin side I submit form to admin-post.php. I want to print success message in the bottom of the form. Iam new in wordpress what i did shown below.
admin.php?page=add-products form code
<form action="<?php echo admin_url('admin-post.php') ?>" method="post">
<table>
<input type="hidden" name="action" value="add_product_from_admin">
<tr><td>Name</td><td><input type="text" name="pr_name" id="pr_name"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="pr_submit" id="pr_submit" value="Save Products"></td></tr>
</table>
</form>
add_action( 'admin_post_add_product_from_admin', 'add_product_into_data_base' );
function add_product_into_data_base() {
//some database operations
wp_redirect(admin_url('admin.php?page=add-products&message=success'));
}
This isn't so much a WordPress thing as a PHP thing. If you don't want to hide anything in the form, or submit/process the form with Ajax, you can just check to see if the &message parameter exists and echo it.
Also as an aside, you should be careful with your indentations - gotta clean that stuff up. It will make your life easier down the road. Here's a super basic example that will leave the form alone, unless &message is set as a query string parameter:
<form action="<?php echo admin_url('admin-post.php') ?>" method="post">
<table>
<input type="hidden" name="action" value="add_product_from_admin">
<input type="hidden" name="message" value="success">
<tr>
<td>Name</td>
<td><input type="text" name="pr_name" id="pr_name"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="pr_submit" id="pr_submit" value="Save Products"></td>
</tr>
</table>
<?= isset( $_POST['message'] ) ? $_POST['message'] : ''; ?>
</form>
This code will check if your hidden success action is present in data passed during page load (any time the form is submitted), and echo "Your Message".
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'success' ) {
echo 'Your Message';
}
is it possible to write a PHP page say form.php with a php function generalValidate($form) that is able to perform the following scenario :
user browse to form.php
user gets an html form
user fills form and the form is sent back with POST method to form.php
form.php activate generalValidate($form) where form is the just recived form filled by user
generalValidate($form) returns true if this form is valid (for exemple properly filled), false else.
i think that another way to describe my question will be - is there a general way to iterate over an HTML form sent by a client (perhaps a form that the php page itself sent to client in the first place) while activating some code over each of the form values?
dont eat me if its a bad question, im really just trying to learn :)
a code exemple to fill for your convinience :
<?php
function generalValidate($form) {
...
}
if (!isset($_SESSION))
session_start();
else if (generalValidate(...)) {
}
?>
<html>
<head>
</head>
<div>
<p>Register</p>
</div>
<form id="regfrm" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" align="center">
<table align="center">
<tr>
<td>First Name :</td>
<td><input name="fname" value="<?php if (isset($_POST["fname"])) {echo v($_POST["fname"]);}?>" required></input></td>
</tr>
<tr>
<td>Last Name :</td>
<td><input name="lname" value="<?php if (isset($_POST["lname"])) {echo v($_POST["lname"]);}?>" required></input></td>
</tr>
<tr>
<td>Email :</td>
<td><input id="email" name="email" value="<?php if (isset($_POST["email"])) {echo v($_POST["email"]);} else {echo "xo#xo.xo";}?>" required></input></td>
</tr>
<tr>
<td>Password :</td>
<td><input name="pw" type="password" value="e" required></input></td>
</tr>
<tr>
<td>Retype password :</td>
<td><input name="pw" type="password" value="e" required></input></td>
</tr>
</table>
<input type="submit" value="Register" ></input>
</form>
</body>
</html>
Yes. Although iterating over the fields gives you a little less clarity and makes it more messy when you want to determine how to validate said field (for example, to know if whether the value should be a name or a number or whatever), but you can do it this way:
In your PHP script you could have something like:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Determines if the form was sent through the POST method
foreach ($_POST as $fieldName => $formValue) {
// Validate $formValue
}
}
What I think you want to ask is if form data can be manipulated without knowing the form's variable names. I say this because you want to have a general purpose function where the code can be reused for any form. Since this may be any form that currently exists or a form you will create in the future you will not know the name of the variables in the form.
This code captures the form's input. All you have to do now is create a function that does whatever to the $name and $item values as they are looped through.
<?php
foreach($_POST as $name => $item) {
print "name::$name item::$item<br>";
}
?>
<html><title>test</title>
<form method="post">
field one: <input type="text" name="one">
<br>
field two: <input type="text" name="two">
<input type="submit" value="go!">
</form>
</html>
Of course, it is possible to have the page in which the original form resides as the recipient of the form dialog. Through the session variables, but mainly through the contents of the button variables you can determine which state your form is currently in (after having clicked a submit button you will get a $_REQUEST array element with the name of the button holding the value of the button).
Take a look at the answer here.
This is actually a canonical question for receiving form data in PHP. There are lots of ways to do it.
I've made the form below. Is it possible to make it that when user enters the number of fields, for example 6, that the table below has 6 rows. It would be great if it would be possible to make it without any submit button (so that the trigger for this action is exiting from the text input box).
Here is the html code of this form:
<fieldset>
<legend>Student Information</legend>
Number of fields: <input type="text"><br />
Total number of characters: <input type="text">
<br>
<br>
<table border="1">
<th></th>
<th>field</th>
<th>number of characters</th>
<tr>
<td>1</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
<tr>
<td>2</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
</table>
</fieldset>
If this is not possible (without submit button), than in which way would you accomplish the same result? Thank you for any help and/or suggestions.
PHP is server side, it runs only once, when the page is loading. HTML is not a programming language. You could generate the table with PHP, but only if you had a submit button that reloaded the page. If it has to happen because of a user event, it always needs to be done with Javascript.
That means, you will need Javascript to make this work without reloading the page. Ideally, you would use Jquery (Javascript's most popular plugin) to manipulate the DOM.
If you had this input :
<input id="field" type="text">
You could call the on-leave event like this :
$("p").focusout(function()
{
// Delete the previous table, and create a new one, here
});
As for creating the actual table, it isn't complicated, but it is a bit of work. You should read the following reference to start you up :
http://www.tutorialspoint.com/jquery/jquery-dom.htm
You will need to "install" JQuery before-hand, you can simple insert this at the top of your code :
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
Okay here is the post only script you require
<?php
$rows=2;
if(isset($_POST['submit']))
{
if($_POST['submit']=='Update')
{
if(isset($_POST['rows'])) $rows=max($rows, intval($_POST['rows'])); // minimum 2 rows
}
else
{
// process posted data here
// reset post or jump to another page
$_POST=array();
//header("Location:index.php");
//exit();
}
}
?>
<form method="post">
<fieldset>
<legend>Student Information</legend>
Number of fields: <input type="text" name="rows" value="<?php echo $rows; ?>"><br />
Total number of characters: <input type="text">
<input type="submit" name="submit" value="Update"/>
<br>
<br>
<table border="1">
<th></th>
<th>field</th>
<th>number of characters</th>
<?php
for($loop=1;$loop<=$rows;$loop++)
{
echo '<tr>';
echo '<td>'.$loop.'</td>';
echo '<td><input name="field['.$loop.']" value="'.$_POST['field'][$loop].'" /></td>';
echo '<td><input name="chars['.$loop.']" value="'.$_POST['chars'][$loop].'" /></td>';
echo '</tr>';
}
?>
</table>
<input type="submit" name="submit" value="Submit"/>
</fieldset>
</form>
It will default to 2 rows (minimum), and retain the data when you update the rows.
If the rows get reduced, then the end ones disappear
It certainly would be doable with just PHP.
So for example, if you typed in '6' rows you could catch the form post and do something like (template form for within the HTML):
<?php for($i=0; $<=$_POST['rows'];$i++): ?>
<!-- This being your whatever html for the table -->
<tr><td></td></tr>
<?php endfor; ?>
So Im relatively new to PHP from ASP. And After converting alot of ASP code into PHP I have come across a problem where my PHP code seems unable to find the hidden variable I have set. It worked fine in ASP and was just wondering the best way to resolve this.
Start of the Form:
<form name="LogIn" action="login.php" method="post">
<td bgColor=#ffffff>
<table align="center" cellSpacing="2" cellPadding="2" border="0">
<tr>
<td> </td>
<td align="right"><font color="#4d71a1">User name:</font> </td>
<td><input name="UserName" size="25" type="Text" autocomplete="OFF"></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td align="right"><font color="#4d71a1">Password:</font> </td>
<td><input name="Password" size="25" type="Password" autocomplete="OFF"></td>
<td> </td>
</tr>
PHP script:
<?
if ($_POST["BtnPress"]=="Pressed")
{
if ($_POST["Username"]=="*****" && $_POST["password"]=="*********")
{
$_SESSION['AdminID']="1";
header("Location: "."index.php");
}
else
{
print "<font color=#ff0000>Sorry you cannot access this part of the site.</font>";
}
}
?>
then the rest of the form:
<tr>
<td align="center" colspan="4">
<input type="hidden" name="BtnPress" value="Pressed">
<input type="Submit" value="Log In" class="mybutton" onclick="return CheckForm();">
</td>
</tr>
</table>
</td>
</form>
The PHP seems unable to find the variable BtnPress, its a similar problem throughout alot of my translated ASP to PHP script. Sorry if it is a simple solution but can anyone tell me where I am going wrong?
You have name="Password" and $_POST["password"]
Password != password
Watch your case.
<form name="LogIn" action="login.php" method="post">
<td bgColor=#ffffff>
That is invalid HTML. A <td> element cannot be a child element of a <form>. Browsers are likely to error recover in ways that break your HTML (e.g. by moving the form, but not its contents, outside the table). Do use a validator.
And stuff that isn't likely to be the cause of the problem, but is likely to be the cause of other problems.
Don't use layout tables
Do use the label element
Do use CSS for presentation (you can style your label elements instead of using the obsolete font element
Do not use a hidden input to test if a form is submitted.
use:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// processing of $_POST
}
The name of the submit button should be "BtnPressed" not the hidden field.
What I would do would be to set the form action to ?id=submit or something like that, and then check "if $_GET['id'] == "submit" then process data.
<form action="login.php?do=submit">
.... <input .....
</form>
<?php
if($_GET['do'] == "submit"){
//process data
} ?>
Hai.. i already added javascript validation for my form.Its working fine.But mean while i ve to validate the same form using PHP also.
This is the form code:
class airportclass{
function add_airport_frm(){
$errmsg=0;
<table>
<form action="" method="post" name="airport" >
<tr> <td colspan="2" align="center"><?php echo $errmsg; ?></td></tr>
<tr>
<td><strong>Code :</strong></td>
<td><input type="text" name="code" id="code" /></td>
</tr>
<tr>
<td><strong>Name:</strong></td>
<td><input type="text" name="name" id="name" /></td>
</tr>
</form>
</table>
}
This is the PHP script in another file :
if(isset($_POST['postcode']) && $_POST['postcode']!="")
{
$code=$_POST['code'];
$name=$_POST['name'];
$postcode=$_POST['postcode'];
$auth=$_POST['auth'];
if(trim($code)=='')
{
$errmsg = 'Please enter code';
}
}
But its not working.
Only javascript validation is working.PHP validation is not working.
Can any one suggest me????
As the form action="" is blank, the form is being posted to the same file. You indicated that you have your validation script in another file!
The $errmsg variable you're using is not global. It needs to be global if you intent to use it inside multiple functions.
You are checking against 'postcode', but postcode is nowhere set in your form.