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

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>

Related

PHP function argument not processed to output

<?php
Function runSearch($name)
{
If(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "Results for " .$name;
}
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Search String: <input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit"><br>
</form>
This code is suppose to display what is entered into the Search String text box. When I don't use a function it works fine. But as soon as I place the code into the function runSearch there is no output. I'm new to php can an argument be sent to a php function and then displayed on the screen?
you need to call your function, otherwise nothing will happen. Also you need to removed the $name-parameter:
<?php
function runSearch()
{
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "Results for " .$name;
}
}
runSearch();
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Search String: <input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit"><br>
</form>

How to display php form result in the html if php processing is not in the same page without using ajax?

Lets say i have this form in form.php file.
<form action="process.php" method="POST">
<input type="text" name="message">
<input type="submit" value="Submit">
</form>
<span class="result"></span>
process.php contains
<?php
if(isset($_POST['message'])){
$message = $_POST['message'];
if(!empty($message)){
echo 'Your message: '.$message;
}else{
echo 'Please enter some message.';
}
}
Now if i want to display the output of process.php inside the form.php's span tag of class result i either need to use ajax, or session/cookie or file handling. Is there any other way?
You can simply place the code in the process.php file inside the forms span tag.
<form action="form.php" method="POST">
<input type="text" name="message">
<input type="submit" value="Submit">
</form>
<span class="result">
<?php
if(isset($_POST['message']))
{
$message = $_POST['message'];
if(!empty($message))
{
echo 'Your message: '.$message;
}
else
{
echo 'Please enter some message.';
}
}
?>
</span>
Try this. It will post the form values in same page
<form action="form.php" method="POST">
<input type="text" name="message">
<input type="submit" value="Submit">
</form>
<span class="result"></span>
<?php
if(isset($_POST['message'])){
$message = $_POST['message'];
if(!empty($message)){
echo 'Your message: '.$message;
}else{
echo 'Please enter some message.';
}
}
OK this is one way of doing it: In form.php,
<form action="process.php" method="POST">
<input type="text" name="message">
<input type="submit" value="Submit">
</form>
<?php
$var = $_GET['text'];
echo "<span class=\"result\"> $var </span>";
?>
Then in process.php do this:
<?php
if(isset($_POST['message'])){
$message = $_POST['message'];
if(!empty($message)){
// echo 'Your message: '.$message;
header("location: form.php?text=$message")
}else{
echo 'Please enter some message.';
}
}
?>
There are a few drawbacks using this method:
Variable $message is passed through the URL and so should not be too long as URLs have length limits
Using $_GET[] makes message visible on the URL so passwords and other sensitive information should no be used as the message.
I hope this helps.

Passing PHP variable data onto another page after validation

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

Submit form and write to file more than one form field

I have a code of a form that writes the "email" of user into a .txt file in my server.
Here are some things I want to do: Make the form have more than one variable (Like, one line to "name" and another one to "email"), put the form writing in .txt prefixes before the "input" texts, and make the output .txt file have line breaks between the contents of each variable.
Here is my code currently
<?php
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$file = fopen("emaillist.txt","a+");
fwrite($file,$email);
fclose($file);
print_r(error_get_last());
}
?>
<form action= "" method="post" name="form">
Email:
<input type="email" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit">
<br>
</form>
Can you help me? Thank you all.
You can add another input to your form. In your code, you have <input type="email" name="email"> for the email address. Just add a <input type="text" name="name"> for the name. The field's name (name="xxx") is the key that you can use in PHP in $_POST['xxx'] to get the information for another line.
Did I understand your question "How can I put the form writting in .txt prefixes before the "input" texts?" correct that you want to have an output in your file like "Email: x#x.x"?
To add a line break, you can write "\n" (if you use a Windows System, use "\r\n") in your strings.
Instead of fopen(), fwrite() and fclose() you can use file_put_contents() which makes your code a bit easier to read.
You can try this code:
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$new_content = "\r\nName: " . $name;
$new_content .= "\r\nEmail: " . $email;
file_put_contents('emaillist.txt', $new_content, FILE_APPEND);
print_r(error_get_last());
}
?>
<form action="" method="post" name="form">
Name:
<input type="text" name="name"><br>
Email:
<input type="email" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>
Please try with following code .
<?php
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$name=$_POST['name'];
$variable = $name ." ". $email. PHP_EOL;
$file = fopen("emaillist.txt","a+");
fwrite($file,$variable);
fclose($file);
print_r(error_get_last());
}
?>
<form action= "" method="post" name="form">
Email:
<input type="email" name="email">
<br>
Name:
<input type="name" name="name">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>

Simple Validator in PHP not working

I created a little form validator with PHP and having some problems with it.
MY VIEW FILE is here :
<form action="" method="post">
<?php if( isset($status) ) : ?>
<p class="notice"><?php echo $status; ?> </p>
<?php endif; ?>
<ul>
<li>
<label for="name">Your Name : </label>
<input type="text" name="name">
</li>
<li>
<label for="email">Your Email : </label>
<input type="text" name="email">
</li>
<li>
<input type="submit" value="Sign Up">
</li>
</ul>
</form>
and here's my little controller :
<?php
require 'index.tmpl.php';
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
if (empty($name) || empty($email)) {
$status = "Please provide a name and a valid email address";
}
echo $name;
}
?>
Now what happens is that , when I open up the page and leave the form fields blank and submit it ,it just reloads ,does not echo anything.
You want to echo $status, not $name.
How about moving the require line to below the if?
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
if (empty($name) || empty($email)) {
$status = "Please provide a name and a valid email address";
}
echo $name;
}
require 'index.tmpl.php';
?>
The <form action="" points to the location where the form will be submitted. If blank, it submits the form back to itself.
<form action="yourLittleController.php" method="POST">
Edited with more info:
in php, post is not POST. Example here: https://eval.in/89002
make sure you have
method="POST">
and not
method="POST">
you should mention your second file name in your view file's form's action attribute like action = "controller.php"

Categories