PHP form post- submitted message - php

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'];
?>

Related

php form image submit button - unable to submit

I am new to php. Though my doubt is very basic, but not understanding why my below form not able to submit? I am using actually image submit button.
Please tell me what wrong in below code:
<?php
function test(){
echo 'test';
}
var_dump($_POST['submit']); // here getting NULL, why?
if(isset($_POST['submit']))
{
test();
}
?>
<form action="." method=post name="loginform">
<table>
<tr>
<input type="password" style="display:none" />
<td width="130"><input type="password" name="password" size="15" maxlength="255" ></td>
<td width="136"><input type="image" src="button-log-in.gif" name="log in" alt=" log in" width="51" height="20" border="0"></td>
</tr>
</table>
</form>
There's no element which has 'submit' name. You're submitting two things, indicated by the name attributes: "password" and "log in". $_POST['...'] contains both of them.
If you'd like the function test() to be called upon submit, regardless of what is entered, you'd better add a hidden input field and check for its existence in PHP.
HTML:
...
<input type="hidden" name="some_name">
...
PHP:
if(isset($_POST['some_name']))
{
test();
}

How to send value of radio button to other page

I have a table with radio buttons to get the row values and 2 buttons
1 button.)For printing data , which moves to "notice.php"
2 button.)For row details,which stays on the same page.
<form action="" method="POST">
<table border="1" >
<tr>
<th>sourceID</th>
....
<th>Status</th>
</tr>
<tr>
<td><input type="radio" name="ID[]" value="<?php echo $tot; ?>" /></td>
<td>1</td>
....
<td>open</td>
</tr>
<input type="button" name="issue" value="Issue Notice" onClick="location.href='notice.php'" />
<input type="submit" name="details" value="details" />
<?php
if(isset($_POST['details']))
{
$n=$_POST['ID'];
$a=implode("</br>",$n);
echo$a;
}
Notice.php:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n=$_POST['ID'];
}?>
The problem here is: My code is working perfectly fine with the button details.
But it doesnt work with issue, i.e after selecting radio button and clicking on the issue notice button :it gives Undefined index: ID in D:\XAMPP\notice.php.
kindly help
Your details button is a submit button, so it submits the form. However your other button is just a regular button and you use javascript to send the browser to notice.php. As such, it does not post any data to notice.php.
You could include the data on the query string and send it that way, e.g.:
location.href="notice.php?id=<?=$tot?>"
Or you could also have the issue button post the page, and then have your receiving page check which submit button was used. If the issue button was used you could then have the php code post to notice.php.
Using the following code is the exact same as having a link:
<input type="button" name="issue" value="Issue Notice" onClick="location.href='notice.php'" />
As in, this will not change the form action and submit the POST data to your new page.
You would need something like:
<form method="post" action="" name="unique-form-name">
<input type="radio" name="ID[]" value="<?php echo $tot; ?>">
<input type="button" id="unique-btn-name" value="Issue Notice">
</form>
<script type="text/javascript">
document.getElementById('unique-btn-name').onclick = function(){
document['unique-form-name'].action='notice.php';
document['unique-form-name'].submit();
}
</script>
Then, once you get the data to notice.php, you'll have to use the data as an array (you won't be able to echo the data):
$IDs = $_POST['ID'];
echo '<pre>',print_r($IDs),'</pre>';
<input type="radio" name="ID" value="<?php echo $tot; ?>" />
Your error is the name attribute.
Also the other button is not related to the form at all. You may want to use ajax here.

Need to carry check-box data from one page to another using PHP

I am new to php and am not quite clear on what to do to carry my information from the first page to the next and then submit it to my email when they are done filling out contact information.
I need the script to work as follows:
Step1: User clicks the input check-boxes for the field they want that is stored in an array
ex:
< input type="checkbox" name="Sound[]" value="item1" > item1
and clicks a button i have written as
< input type="image" name="Submit" class="" alt="" src="images/contact1.png" border="0" >
Step2: The information from the check-boxes they have clicked needs to be carried over to the next page where they will fill out their contact info. Name email phone etc.
ex:
<tr>
<td valign="top">
<label for="telephone">Telephone Number *</label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30" style="margin-bottom: 10px;">
</td>
</tr>
Step3. All of this information should be sent to my email upon button press for me to contact them :D
<tr>
<td colspan="2" style="text-align:center;">
<input type="image" name="Contact" class="contactbutton" alt="" src="images/contact.jpg"/>
</td>
</tr>
I can pull the information from my inputs but do not know how to carry to the next page!
Can I do it all in the same php script? or does each page need a different php script?
Please help!
Thanks Paul
you can do it with a form and send it to the page2.php. value will be stored in
$_POST['S'] for the checkbox
<form action="page2.php" method="post">
<input type="checkbox" name="S" value="item1" > item1
<input type="SUBMIT" >
</form>
------------------
page2.php
echo($_POST['S']); // will be item1
$_SESSION array is better. to use it you need to put session_start(); at start of
every page that will use your $_SESSION variable i.e
session_start();
if(isset($_POST['S'])){
$_SESSION['h'] = $_POST['S'];
echo($_SESSION['h']); } //output value in checkbox
?>
<html><body>
<form method="post">
<input type="checkbox" name="S" value="item1" > item1
<input type="SUBMIT" value="item1" >
Once this script is run you can accesS value in $_SESSION['h'] in other pages.
the data will be deleted when you close browser.
----------------------------------
page2.php
<?php
session_start();
if(isset($_SESSION['h'])){ //check if $_SESSION['h'] has been set a value
echo $_SESSION['h']; //output value stored in var
}
?>
You will ultimately still require the use of POST data to get the checkbox status from your page.
Page 1:
<?php
session_start();
// If postdata is received then redirect to next page
if(isset($_POST['Sound'])) {
$_SESSION['Sound'] = $_POST['Sound'];
header('Location: http://www.example.com/page2.php');
exit;
}
?>
<form method="post" action="page1.php">
Sound? <input type="checkbox" name="Sound" value="item1"><br>
<input type="submit">
</form>
Page 2:
<?php
session_start();
// If postdata is received then redirect to next page
if(isset($_POST['telephone']) && isset($_POST['email'])) {
$_SESSION['telephone'] = $_POST['telephone'];
$_SESSION['email'] = $_POST['email'];
header('Location: http://www.example.com/page3.php');
exit;
}
?>
<form method="post" action="page2.php">
<!-- If you want to output the previously saved data in a disabled item -->
Sound? <input type="checkbox" name="Sound" value="item1" disabled="disabled" <?php if($_SESSION['Sound'] == 'Yes') echo('checked="checked"'); ?>>
Telephone: <input type="text" name="telephone" value=""><br>
Email: <input type="email" name="email" value=""><br>
<input type="submit">
</form>
And so on and so forth for your next pages
This does not include the code for generating the e-mail via PHP but is intend to show you how you can take the form input/checkbox selections and store there values to a SESSION ARRAY. Note that in this example: The form is submitting to itself by leaving the action="" blank, but normal would submit to a external PHP file for parsing/handling.
Also, im choosing to create a random number to represent the visitor to the form if not specifically set by $_POST['user']
<?php session_start();
if (!isset($_SESSION['user'])) {$_SESSION['user']=rand(10,700);}
if (isset($_POST['user'])) {$id=$_POST['user'];} else {$id=$_SESSION['user'];}
?>
<form action="" method="post">
Sound 1:<input name="cb1" type="checkbox" value="sound1"><br>
Sound 2:<input name="cb2" type="checkbox" value="sound2"><br>
Sound 3:<input name="cb3" type="checkbox" value="sound3"><br>
<input type="submit" name="submit" value="submit"><br><br>
<?php
if (isset($_POST['submit']) && $_POST!=="") {
foreach($_POST as $key => $value) {
$_SESSION['visitor']['sounds'][$id]=array(
'selects'=>$_POST['cb1'].",".$_POST['cb2'].",".$_POST['cb3']
);
};
echo "For user ID:".$id." We echo the comma delimited stored SESSION array: ".$_SESSION['visitor']['sounds'][$id] ['selects'];
echo "<br><br>";
// Option 2 Explodes the comma delimited ['selects'] field to handle each choice seperately
$choice = explode(",",$_SESSION['visitor']['sounds'][$id] ['selects']);
echo "For an alternative, we EXPLODE the stored 'selects' field of the SESSION ARRAY and can then echo each out seperately"."<br><br>";
echo "User ".$id." Option 1 value was: ".$choice[0]."<br>";
echo "User ".$id." Option 2 value was: ".$choice[1]."<br>";
echo "User ".$id." Option 3 value was: ".$choice[2]."<br>";
echo "<br><br>";
echo "A last example we loop through the EXPLODED values and echo only those that were selected (ie: had a value)"."<br>";
foreach ($choice as $key => $value ) { if ($value!=="") {echo "Selection: ".$value."<br>";} }
}
?>

how to assign the email address for every form in a while loop

I am trying to create a website where a user can post anything he wants to sell and others can see it and send a message to him. I am using a while loop in to retrieve the information from the database. My question is I have a contact button for every post and when someone clicks on it, it opens a dialog box where users can fill the message and the form sends it to the seller's email address. I am confused on how to assign that particular seller's email from database to that particular item listing.
Here is my present code
while($row=mysql_fetch_array($eleclisting))
{ ?>
<div align="center">
<font size="6" color="4684fd"><? echo $row['title']; ?></font><br>
<? $selleremail=array($row['email']); ?>
<font size="3" color="908282"><? echo addslashes($row['description']); ?></font><br>
<font size="3"><? echo "Price: $".$row['price']; ?></font>
<a id="contactpopup" href="#"><button type="button" name="contactbutton"
id="contactbutton" value="">contact</button></a>
<hr>
<? } ?>
Below is the form when someone clicks on the contact button
<div id="contact" class="contact" style="width:600px;border:#fff medium solid; top:50%;
margin-top:-120px; display:none; height:300px; z-index:1001; position:fixed; left:50%;
margin-left:-250px; background:#ebebeb; overflow:auto;">
<form action="listingcontact.php" method="POST">
Message
<br><textarea name="message" style="min-width:500px; max-width:500px; max-height:200px;
min-height:200px;"></textarea> <br>
Email <input type="email" name="contactemail" id="contactemail" style="min-width:250px;
max-width:250px; min-height:25px; max-height:25px;"><br><br><input type="submit"
value="send"> <button type="button" id="cancel">close</button>
</form>
</div>
sorry if the code is confusing. Thanks in advance!
You just add an attribute data-email in the contact popup link as below:
<a id="contactpopup" class="opencontactpopup" href="#" data-email="<?php echo $row['email'];?>">....</a>
Then in your form add a hidden field for holding the email id like:
<form action="listingcontact.php" method="POST">
<input type="hidden" name="tomail" id="tomail" value="">
//your remaining code
</form>
When clicking on the popup link add the corresponding email to the hidden field as below:
<script>
$('.opencontactpopup').click(function(){
var tomail = $(this).attr('data-email');
$('#tomail').val(tomail);
//your remaining code
})
</script>
Now when submitting the form you can get the email id from the field $_POST['tomail']. Hope this will help you. But this is not the right way to use the email id as public.

Validating form input in PHP

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.

Categories