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.
Related
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; ?>
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!';
}
}
I would like to update the choice of poll to update every time that I've submitted.
but It seemed not working. Can anyone advice me about keeping state with hidden field?
and how to clear all the content in current page to display the poll result table?
<?php
if ($_POST['choice']==0)
$a_count = $_POST['a_count']+1;
if ($_POST['choice']==1)
$b_count = $_POST['b_count']+1;
if ($_POST['choice']==2)
$c_count = $_POST['c_count']+1;
if ($_POST['choice']==3)
$d_count = $_POST['d_count']+1;
?>
<html>
<head>
</head>
<body>
<form action="index.php" method="POST">
<table align="center">
<tr><td>Please select</td></tr>
<tr><td><input type="radio" name="choice" value="0">aaaa</td></tr>
<input type="hidden" name="a_count" value="<?php print $a_count ?>">
<tr><td><input type="radio" name="choice" value="1">bbbb</td></tr>
<input type="hidden" name="b_count" value="<?php print $b_count ?>">
<tr><td><input type="radio" name="choice" value="2">cccc</td></tr>
<input type="hidden" name="c_count" value="<?php print $c_count ?>">
<tr><td><input type="radio" name="choice" value="3">dddd</td></tr>
<input type="hidden" name="d_count" value="<?php print $d_count ?>">
<tr><td><input type="submit" value="submit"></td></tr>
</table>
<table align="center" border="1" cellspacing="0">
<tr align="center"><th>Member Name</th><th>Vote</th></tr>
<tr><td>aaaa</td><td><?php echo"$a_count";?></td></tr>
<tr><td>bbbb</td><td><?php echo"$b_count";?></td></tr>
<tr><td>cccc</td><td><?php echo"$c_count";?></td></tr>
<tr><td>dddd</td><td><?php echo"$d_count";?></td></tr>
</table>
</form>
</body>
</html>
The code you submitted looks like it should keep the user's selection/state during their active session (as long as they do not leave that page) - but as soon as they leave it's lost. Also, their "vote" cannot be shared between any other users.
To keep the user's state, explore the use of PHP sessions or storing results in a file/database.
To share the user's vote(s) with other users, explore the use of files or a database.
To show the results table without the form, after the user has "cast their vote", you can do one of two things. You could put an if-statement around the form to check if an answer has already been selected - if so, don't display the form. The other way would be to have the results-table on a separate page that doesn't have the form (just have the form POST to the separate page, or have the page with the form redirect to the separate page).
why not use the database to store values?? And you can put the table that you are using for the input in an if condition so that it does not render when the form is submitted. Hope this helps!
I am using a PHP script to submit an email to the database,
after the user submit, I am doing a small validation and submit it.
everything is working just fine, but instead of postback the user to the same page with a blank textbox, I want to add a label says "Submitted successfully".
I managed to do so, but the problem is when I just refresh the page- without really pressing the "submit" button, I still get to see the message- submitted successfully...
this is a small part of my code:
<form action="<?php echo $editFormAction;?>" method="post" name="form1" id="form1">
<table align="center">
<tr valign="baseline">
<td nowrap align="right">Email:</td>
<td><span id="sprytextfield1">
<input type="text" name="Email" id="Email" value="" size="32">
<input type="submit" value="Submit"><br/>
<div id="confirm">
<?php
if(isset($_POST['Email']))
echo "<font color='green' size='5'><b>Submited Successfuly!</b></font><br/>";
?>
</div>
<span class="textfieldRequiredMsg"><font size="+2"><b>Insert an Email Address</b></font></span>
<span class="textfieldInvalidFormatMsg"><font size="+2"><b>Invalid Email Address!</b></font></span>
</span>
</td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>
There are 2 ways to do it.
Send your form using AJAX. A page wouldn't be reloaded upon submit.
Use sessions to store this message, then reload page using Location: header, then display message and delete it from session.
Try
if(!empty($_POST['Email'])) {
//successful submit
}
empty will check if the value is an empty string.
You need to unset your post variable after message diaplay
Submited Successfuly!";
unset($_POST['email'];
?>
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.