Passing PHP variable data onto another page after validation - php

While I found something similar to this question on here it didn't answer my question outright.
I have set up this php script to validate the form data, which works, after its validated I want it to then pass the info onto another script page to let the user then verify their input data and then mail the data. Its at this state that I'm having trouble. I've spent the last few days trying to find a solution to this and unfortunately coming up short.
<?php
$name_error = '';
$email_error = '';
$comments_error = '';
$error = false;
if (!empty($_POST['submitted']))
{ //if submitted, the validate.
$name = trim($_POST['name']);
if (empty($name))
{
$name_error='Name is required';
$error = true;
}
$email = trim($_POST['email']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
$email_error='E-mail address not valid';
$error = true;
}
$comments = trim($_POST['comments']);
if (empty($comments))
{
$comments_error='Comments are required';
$error = true;
}
if ($error == false)
{
$name_send = $name;
$email_send = $email;
$comments_send = $comments;
/* Redirect visitor to the thank you page */
header('Location: /mail.php');
exit();
}
}
The form this is attached to:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
<label>Your Name</label><br />
<input type="text" name="name" style="width:95%" class="text" value='<?php echo htmlentities($name) ?>' />
<br/>
<span class='error'><?php echo $name_error ?></span>
<br />
<label>Email</label><br />
<input type="email" name="email" style="width:95%" class="text" value='<?php echo htmlentities($email) ?>' />
<br/>
<span class='error'><?php echo $email_error ?></span>
<br />
<label for="comments" style="font-size:16px;">Feedback Comments</label><br />
<textarea name="comments" style="width:95%;" rows="8" value='<?php echo htmlentities($comments) ?>'></textarea>
<br />
<span class='error'><?php echo $comments_error ?></span>
<br />
<input type="checkbox" name="allowCommentPublish" checked="checked" />
<label for="allowCommentPublish" style="font-size:10px;">Allow these comments to be used on our website</label>
<fieldset class="optional">
<h2>[ OPTIONAL ]</h2>
<label>Company Name</label><br />
<input type="text" name="companyName" style="width:95%" class="text" />
<br/>
<label>Phone</label><br />
<input type="text" name="phone" style="width:95%" class="text" /><br/>
<div style="margin:5px 0px;">
<input type="checkbox" name="incmarketing" />
<label style="font-size:10px;"> Yes, you can email me specials and promotions.</label>
<br/>
</div>
</fieldset>
<fieldset>
<input type="submit" name="submitted" value="Send" />
</fieldset>
I will point out im focusing on the main data inputs: Name E-mail and comments.
I need the info from this form to be sent onward but i dont know exactly how to do this and any help will be appreciated greatly.

For passing the values to next page you will have to use either of the three methods.
1. Set cookies with the data.
2. Use global variable session.
3.Pass the data in the url.
For cookies u can set cookies with the values like
setcookie('name',$name);
in ur next page read those cookie data
For sessions:
$_SESSION['name']= $name;
for reading data from cookies & session:
$name = $_COOKIE['name'];
$name = $_SESSION['name'];
For using sessions you must add the line
session_start();
at the start of both the pages that send or receive(use) the data
and for urls
header('Location: /mail.php?name=$name&email=$email&comment=$comments');
Read more on using session

If you need to pass values from one script to another you can use $_SESSION variables. To start a session use: (at the top of the php script)
session_start();
$_SESSION['somename'] = $somevariable;
To access or get that same variable you can use this:
session_start();
$some_other_variable = $_SESSION['somename'];
or you can use hidden input fields.

You can use hidden fields and javascript to submit the form. However as this is the same php page as the original form you will need an if statement
echo '<form name="newForm" action="newpage.php" method="POST">';
echo '<input type="hidden" name="name2" value"' . $name . '">;
echo '<input type="hidden" name="email2" value"' . $email . '">;
echo '<input type="hidden" name="comments2" value"' . $comments . '"></form>;
echo '<script> if (document.getElementById("name2").value != ""){window.onload = function(){ window.document.newForm.submit(); }} </script>';

Related

variable and string apparently not matching

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input onclick="this.value=''" type="text" name="first_name" value="First Name">
<input onclick="this.value=''" type="text" name="last_name" value="Last Name">
<input onclick="this.value=''" type="text" name="pwd" value="The Passcode">
<input name="submit" type="submit" value="Submit" class="submit_">
<textarea name="comments" id="comments" rows="4" cols="50"></textarea>
</form>
PHP :
if(isset($_POST['submit']))
{
$name = mysql_real_escape_string($_POST['first_name']);
$pwd = mysql_real_escape_string($_POST['pwd']);
$text = mysql_real_escape_string($_POST['comments']);
// print_r($_POST);
if(is_null($text)) {
debug_to_console('some fields empty');
echo "<p class=\"warning\">* Your Message Did Not Contain Any Characters.</p>";
}
else if($name === 'First Name'){
debug_to_console('name isnt set');
echo "<p class=\"warning\">* You Have Not Entered Your First Name Correctly.</p>";
}
else if($pwd !== 'XyZ'){
debug_to_console('password not set');
echo "<p class=\"warning\">* Passcodes Do Not Match.</p>";
} else {
Keeps returning 'password not set' even though the form input matches the variable. Used print_r and $pwd states XyZ.
tried removing onClick from the form input. I'm assuming this is a caps thing?
Please help, thank you.
Check $pwd with
var_dump($pwd);
I do believe $pwd is a string (because you use !== instead of !=) AND doesn't contain: XyZ
(if you didn't change the value in input field pwd, its value is still: The Passcode)

i keep getting Undefined index: name and Undefined index: email

this is my first form
<style>
.wrap-form{
width: 700px;
min-height: 20px;
background-color: lightblue;
margin: 0 auto;
}
</style>
<div class="wrap-form">
<form method="post" action="advanced-form-send"></form>
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</div>
And this is my form 2 where the data will send to
<?php
$name = $_POST["name"];
$email = $_POST["email"];
echo $name;
echo $email;
?>
How would I fix this?
That's how your form code should be, surrounding your submit button by <a href="..." is causing the form not to be submitted, at the place you're just telling the browser that when this button is clicked, take the user to the page advanced-form-search.php. What you should do is put the script name where the form should be submitted in the action of the form tag then just add a submit button. And don't forget to close your tags... you missed the </form>
<div><?php if(isset($_GET['err'])){ if($_GET['err']==1){ echo 'You didn\'t fill in your name'; } elseif($_GET['err']==2){ echo 'You didn\'t fill in a correct email address';}}?></div>
<div class="wrap-form">
<form method="post" action="advanced-form-send.php">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>
</div>
Here's the PHP code to send an email:
<?php
if(isset($_POST['name']) && !empty(trim($_POST['name']))){
$name = $_POST["name"];
} else {
header("Location: page_where_the_form_is.php?err=1");
die()
}
if(isset($_POST['email']) && filter_var(trim($_POST['email']), FILTER_VALIDATE_EMAIL)){
$email = $_POST["email"];
}
else {
header("Location: page_where_the_form_is.php?err=2");
die()
}
$subject='Form Submitted On The Website';
$message="Name: {$name}\nEmail: {$email}";
mail($to_email, $subject, $message);
?>
You've wrapped your <input type="submit" name="submit"... button with an <a href=.... What this does is prevents your form from being submitted as a POST request, and instead gets linked to as a GET request.
git rid of the <a href..., and and change your action=advanced-form-send to action="advanced-form-send.php".
You can improve your php code:
<?php
if (isset($_POST["name"])) {
$name = $_POST["name"];
echo $name;
} else {
echo 'Eror: Attribute "name" is missing!';
}
if (isset($_POST["email"])) {
$email = $_POST["email"];
echo $email;
} else {
echo 'Eror: Attribute "email" is missing!';
}
?>
And your form should be changed:
<form method="post" action="advanced-form-send.php">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>
You need to wrap your variables. The other answers are about the form. This answer is about your handling of variables. PHP will throw a notice if you use a variable that haven't been set. By using isset() you can determine if the variable is set and then use it.
<?php
$name = isset($_POST["name"]) ? $_POST["name"] : '';
$email = isset($_POST["email"]) ? $_POST["email"] : '';
echo $name;
echo $email;
?>
Doing it this way ensures that you wont get a notice if one of your fields haven't been posted. If they have you will have the value in $name and $email. If not then the variables will be empty strings.
Try Like this,
<div class="wrap-form">
<form method="post" action="advanced-form-send">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>
</div>
The submit button should be in the two "form" markups. Also the link is unnecessary because the input "submit" will send the form with the data to the page you set in the attribute "action" of the form markup.
<div class="wrap-form">
<form method="post" action="advanced-form-send.php">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email">
<input type="submit" name="submit" value="Submit">
</form>
</div>
Youre missing an enctype ... you ALL forgot it. Shame above you!
<form method="post" action="advanced-form-send.php" enctype="multipart/form-data"></form>

Echo in a form when user did not insert all info needed

This is a part of my registration form. I want to display back the input user inserted if they forgot to enter all the info needed. However, I get this on my textbox in register form where everyone including my user can see it.
Notice:Undefined variable: name in D:\XAMPP\htdocs\registration.php on line 113
I want it to echo back the input that user had inserted and display it again so that user does not have to enter the same input over again. Help ?
$myusername=($_POST['username']);
$name=($_POST['name']);
if(isset($_POST['username'])) {
echo $_POST['username'];
}
if(isset($_POST['name'])) {
echo $_POST['name'];
}
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
Assuming you send the user back to the page they were at previously if the form fails to validate, the POST array is emptied. POST will only carry the information to the page that the form is submitting to.
You can use sessions to save the data in an array, indexed by form field name. Then when the user is sent back to the form, if there are any entries in the array, you can iterate over them through to your form fields.
you can controll the variables with if clause; means you can write:
if ( isset($_POST['send']) && isset($myusername) ) {
echo $myusername;
}
else {
echo '<span style="color:red;">Please complete this field</span>';
}
and do the same in html value for textfiels...
$myusername = isset($_POST['username']) ? ($_POST['username']) : '';
$name = isset($_POST['name']) ? ($_POST['name']) : '';
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
Try this, this should remove your error?
if(isset($_POST['username'])) {
$myusername=($_POST['username']);
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
echo $_POST['username'];
}
if(isset($_POST['name'])) {
$name=($_POST['name']);
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
echo $_POST['name'];
}
Here we are checking for POST value first then we using it.
Write the isset function inside the value of each of your .
Exemple :
<input type="text" name="username" size="60" value="
<?php if( isset( $_POST["myusername"] ) )
echo $_POST["myusername"] ?> "/>
you can use this tutorial for validation of input fields in php
http://www.w3schools.com/php/php_form_validation.asp
or you can use this method
<?php
$myusername="";
$name="";
if(isset($_POST['submit'])){
$myusername = $_POST['username'];
$name = $_POST['name'];
}
?>
<form method="POST" action="">
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
<input type="submit" name="submit" size="60" value="submit"/>
</form>

Allow Submission of Form Multiple but Up to Allowed Times on PHP

I have a form that a user can enter in first name, last name and email. You can submit this form multiple times in case you would like to send to more users by simply clicking on submit (of course, so long as the fields are entered correctly using sanitation and validation).
if (isset($_POST['Submit'])) {
//sanitize if field (like email) is not empty, then validate it using a function
// such as FILTER_VALIDATE_EMAIL and FILTER_SANITIZE_STRING
//like if ($_POST['email'] != "") { do the sanitation and validation here }
//then if no error send the email
//then $_POST = array(); to clear up form for the next entry
}
<form> form here</form>
Do you guys have an idea by just laying out this concept? Or do you need a sample code? Thanks for your help.
Was trying to use what Joe suggested but didn't work. Any further help?
session_start();
if (isset($_POST['Submit'])) {
if (isset($_SESSION['submission_count'])) {
if ($_SESSION['submission_count'] > 10) {
echo "You can't submit so many!";
exit;
}
}
else {
$_SESSION['submission_count'] = 0;
}
$_SESSION['submission_count'] += 1;
// Form validation and sanitation
// Send email
}
?>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First Name:
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" size="50" /><br />
Last Name:
<input type="text" name="lastname" value="<?php echo $_POST['lastname']; ?>" size="50" /><br />
Email Address:
<input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="50"/> <br/><hr />
<br/>
<input type="submit" class="button" name="Submit" />
</form>
You can store a counter in $_SESSION.
http://www.php.net/manual/en/intro.session.php
<?php
// Starting the session
session_start();
if (isset($_POST['Submit'])) {
if (isset($_SESSION['submission_count'])) {
if ($_SESSION['submission_count'] > 5) {
echo "You can't submit so many!";
exit;
}
}
else {
$_SESSION['submission_count'] = 0;
}
$_SESSION['submission_count'] += 1;
// Do the rest of your form submission
}

Get variable from query string

I was trying to get variable in Query String from URL. But somehow, its just got one variable instead of getting all variables from querystring. I really don't know what goes wrong with my code. Here is the code I want to print out error from the invalidate form:
<?php
displayForm();
function displayForm(){
?>
<form action="./prod_add_action.php" method="post" name="addproductForm">
<fieldset>
<legend>Online Ordering System Setup</legend>
<label for="product_name">Product Name: </label><input type="text" name="product_name" value="" /><?php echo $_GET["name_error"]; ?>
<label for="product_date">Product Date: </label><input type="text" name="product_date" value="" /><?php echo $_GET["date_error"]; ?>
<label for="product_price">Product Price: </label><input type="text" name="product_price" value="" /><?php echo $_GET["price_error"]; ?>
<input name="add_button" type="submit" value="Add" />
<input name="reset_button" type="reset" value="Clear" />
</fieldset>
</form>
<?php
}
?>
And here is the code I created the querystring:
$query_string = "name_error=" .urlencode($name_error) ."&date_error=" .urlencode($date_error) ."&price_error=" .urlencode($price_error);
header("Location: ./prod_add.php?$query_string");
exit();
In the first code, the page only print the first $_GET['name_error'], while it should be include $_GET['date_error'] and $_GET['price_error. ']
This is the address:
http://example.com/prod_add.php?name_error=Product+name+must+be+characters+only&date_error=Product+date+must+be+input+as+this+formate+DD-MM-YYYY&price_error=Product+price+must+be+float+number+only
You should use & instead of &'s ?
$query_string = "name_error=" .urlencode($name_error) ."&date_error=" .urlencode($date_error) ."&price_error=" .urlencode($price_error);
header("Location: ./prod_add.php?$query_string");
exit();
Change & to & as:
$query_string = "name_error=" . urlencode($name_error) . "&date_error=" . urlencode($date_error) . "&price_error=" . urlencode($price_error);
header("Location: ./prod_add.php?$query_string");
exit();

Categories