Form not posting - PHP - php

I want to make this form where people can send a email to me. I've done this before but this time it won't post... I won't get a error message or anything like that. Even my custom made error messages won't show up.
<?php
$err = "<br/>";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (!empty($name) && !empty($email) && !empty($message)) {
if (strstr($name, " ")) {
if(strlen($email) >=5 && strstr($email, "#") && strstr($email, ".")) {
} else {
$err = "<p style='color:#b30000'>Fill in a valid email!</p>";
}
} else {
$err = "<p style='color:#b30000'>Fill in a valid name!</p>";
}
} else {
$err = "<p style='color:#b30000'>Fill in all the fields!</p>";
}
} else {$name = ''; $email = ''; $message = '';}
echo $err . "
<form id='form' action='#form' method='post'>
<p>Full Name:</p>
<input class='input' type='text' name='name' placeholder=' Dirk Vondiek' value=". $name . ">
<p>E-Mail:</p>
<input class='input' type='text' name='email' placeholder=' example#domain.com' value=". $email . ">
<p>Your Message:</p>
<textarea class='textarea' name='message' placeholder=' Can you pls add me on steam?'>". $message ."</textarea>
<center><input class='submit' type='submit' name'submit' value='Send'></input></center>
</form>
"
?>

action='#form', it's invalid for an action syntax, so change that to action="" or action="handler.php" if you later decide to use two seperate files.
Edit for above: After testing for myself, that does indeed work, but the major culprit here is that you have a typo here name'submit' and your conditional statement
if (isset($_POST['submit'])) {...} will never happen because of it.
You forgot the equal sign.
So modify it to read as name='submit'.
You also have </input> you can safely get rid of it, it's not a valid closing tag.
And you forgot to close this statement (but in testing this didn't throw me an error and I find that rather a bit bizarre).
</form>
"
?>
So.
</form>
";
?>
That alone with error reporting would have thrown you something about it.
Nota:
However, if you have more code below the </form>" line, then you will get a parse error such as:
(Nota: I added an echo "Testing"; for this example error) but will throw you a parse error related to what you may be using:
Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';'
As you mention in your question that it's meant to send mail with, but that wasn't included in your original post.
If you do have more code below that line and inside the same <?php ...?> tags, then you will need to close off that statement as stated in my answer.
For example.
This will throw a parse error:
</form>
"
mail();
?>
This won't throw an error (valid) so it needs to be closed (mail() is just a fill-in example here).
</form>
";
mail();
?>
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.

Please check your form action. You need to mention a URL here like example.php.
In case your PHP and HTML script are in the same page, you can keep it blank.
<form id='form' action='' method='post'>
Hope this helps.

You should change
<form id='form' action='#form' method='post'>
to
<form id='form' action='' method='post'>

Related

PHP contact form keep input fields if error, otherwise clear

I want my contact form input fields to save the user's inputs if there's an error, otherwise have them cleared if the email goes through. Right now it works except when the email is sent the fields are still filled in. I could refresh the page but then it doesn't show the 'Your email has been sent' message.
Here's my form:
<form class="contact-form" action="" method="post">
<input type="text" name="name" value="<?php if(isset($_POST["name"])) echo $_POST["name"]; ?>" />
</form>
I tried adding something to my php code that handles the email business - if the message was sent, unset($_POST["name"]), and also adding to this form input's php else echo ''; but that didn't seem to work. It seems the variable was still set.
Let's assume that your page is contact.php.
You php code should be something like this:
// we will keep here error message
$error = '';
// if request is get and isset sent
if ($_SERVER["REQUEST_METHOD"] === "GET" and isset($_GET["sent"]))
echo '<p id="output-area">Message has been sent.</p>';
else {
// if request is post
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// then verify input data
if (!empty($_POST['msg'])) {
// if mail was sent, redirect to contact.php?sent
if (mail("someone#example.com", "My subject", $_POST['msg'])){
header('Location: contact.php?sent');
exit;
} else
$error = "Mail does not sent.";
} else
$error = 'Please fill in all inputs.';
}
}
if ($error != '')
echo '<p class="error">Error: ' . $error . '</p>';
// here goes your form
echo '<form class="contact-form" action="contact.php" method="post">
<textarea name="msg">' . (!empty($_POST["msg"]) ? $_POST["msg"] : '') . '</textarea>
<input type="submit" value="send">
</form>';
You should set error flag while error occurred. try this
$error=false;
if(isset($_POST['submit'])){
$msg = $_POST['msg'];
if(mail("someone#example.com","My subject",$msg)){
}else{
$error = "mail does not sent";
}
}
" />

PHP validation help. First and last name, validated as required fields

This link leads to where my code is ran.
http://erobi022.pairserver.com/phpvalidate1.php
<!DOC TYPE html>
<html>
<body>
This is a simple form
<form method="post" action="send_phpvalidate1.php">
Please enter your first name: <input type="text" name="First"></p>
Please enter your last name: <input type="text" name="Last"></p>
<button type="submit">Submit</button>
</form>
</body>
</html>
Once the submit button is hit, all data is sent and posted here. I can get first and last name to output fine, but if i leave them blank it will only say that i left my first name field blank.
http://erobi022.pairserver.com/send_phpvalidate1.php
<!DOCTYPE html>
<html>
<body>
Welcome,
</P>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name1 = $_POST["First"];
if (empty($name1)) {
echo "You've entered nothing for first name";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your first name is $name1 ";
}
}
echo "<br>";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name2 = $_POST["Last"];
if (empty($name2)) {
echo "You've entered nothing for last name";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your last name is $name2 ";
}
}
?>
<?php // can have multiple php sections
echo "<a href='phpvalidate1.php'>Return to form</a></p>";
//have to use a simple qoute within html to make it work
?> </p>
Return to home page
</body>
</html>
You can just use that using HTML 5, simple
<!DOC TYPE html>
<html>
<body>
This is a simple form
<form method="post" action="send_phpvalidate1.php">
Please enter your first name: <input required="required" type="text" name="First"></p>
Please enter your last name: <input required="required" type="text" name="Last"></p>
<button type="submit">Submit</button>
</form>
</body>
</html>
No need to use two big ifs, just replace your PHP code with this :
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name1 = $_POST["First"];
$name2 = $_POST["Last"];
if (empty($name1) || empty($name2)) {
echo "Please complete both the fields.";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your name is $name1 $name2";
}
}
?>
First, whenever there is a die() statement,only the codes that appear before that run, the ones which come after don't get to run(in relation to your code.)
you can also trim your codes down this way and it will still work
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name1 = $_POST["First"];
$name2 = $_POST["Last"];
if (empty($name1) || empty($name2)) {
echo "Please complete both the fields.";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your name is $name1 $name2";
}
}
?>

PHP AND IF statement

im tring to call php page from a simple html page, and i cant find the syntax error
this is the html
<html>
<body style="background-color:#990000;">
<h5 align="center" style="font-family:tahoma;color:white;font-size:50px;"> Welcome! </h5>
<form align="center" method="post" action="php_site.php">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br>
<input type="submit" value="Search!">
</form>
</body>
</html>
and this is the php
<?php
if (isset($_POST['firstname']) && (isset($_POST['lastname'])))
{
echo "Welcome $_POST['firstname']";
echo "This is your last name $_POST['lastname']";
}
else
{
echo "This is Empty!";
}
?>
thanks!
The syntax of the embedded references to $_POST inside the strings is not quite right. Some ways to fix it are:
Wrap them in {} brackets:
echo "Welcome {$_POST['firstname']}";
echo "This is your last name {$_POST['lastname']}";
Or, remove the single quotes:
echo "Welcome $_POST[firstname]";
echo "This is your last name $_POST[lastname]";
Or, use string concatentation with . instead of embedding:
echo "Welcome " . $_POST['firstname'];
echo "This is your last name " . $_POST['lastname'];
And/or, pull the values into plain-named variables first:
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
echo "Welcome $firstname";
echo "This is your last name $lastname";
See the doc on variable parsing in strings.
Try this:
<?php
if (isset($_POST['firstname']) && isset($_POST['lastname']))
{
echo "Welcome ".$_POST['firstname'];
echo "This is your last name ".$_POST['lastname'];
}
else
{
echo "This is Empty!";
}
?>
<?php
if (isset($_POST['firstname']) && isset($_POST['lastname']) )
{
echo "Welcome ".$_POST['firstname'];
echo "This is your last name $_POST['lastname']";
}
else
{
echo "This is Empty!";
}
?>
First off, you should set your set your $_POST values to variables.
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
Then, ensure that you're splitting the strings from the variables by using the concatenation punctuation .:
echo "Welcome" . $firstname; instead of echo "Welcome $_POST['firstname']";
Also, please post the syntax error you're getting. If PHP is breaking (white screen of death), then please add ini_set("display_errors", "1"); in order to write errors to the screen, and then post the error output.

$outputstring error with html class

So I'm not that great at php and I have a basic comment system that I'm trying to implement that calls and writes to a comments.php file.
everything works all well an good until I try and style the $outputstring a bit.
this is the code I have
$outputstring = "<br><p><span class="label">Name:</span> " .$name. "</p><br> <p><span class="label">Comment:</span>" .$message. "</p><br>";
I know whats causing it is the
<span class="label"></span>
but can anyone tell me why?
the script I got was just one off youtube while im experimenting with site building.
the full script is like this.
<?php
$act = $_POST['act'];
if($act == "post") {
$name = $_POST['name'];
$message = $_POST ['message'];
#$fp = fopen("comments.php", 'a');
if (!$fp) {
//The file could not be opened
echo "There was an error! Please try again later!";
exit;
} else {
//The file was successfully opened, lets write the comment to it.
$outputstring = "<br><p><span class="label">Name:</span> " .$name. "</p><br> <p><span class="label">Comment:</span>" .$message. "</p><br>";
//Write to the file
fwrite($fp, $outputstring, strlen($outputstring));
//We are finished writing, close the file for security / memory management purposes
fclose($fp);
//Post the success message
echo "Your post was successfully entered. Click <a href='index.php'>here</a> to continue.";
}
} else {
//We are not trying to post a comment, show the form.
?>
<h3>comments:</h3>
<hr/>
<?php include("comments.php"); ?>
<br><br>
<h3>Post a comment:</h3>
<form action="index.php" method="post">
<label>Name:<label>
<input type="text" name="name" value=""></input>
<br/>
<label>Comment:</label>
<textarea name="messages"></textarea>
<input type="hidden" name="act" value="post"></input>
<br/>
<input type="submit" name="submit" value="Submit"></input>
</form>
<?php
}
?>
If anyone could tell me what I would need to do to be able to add the span with a class that would be swell.
thanks.
$outputstring = "<br><p><span class="label">Name:</span> " .$name. "</p><br> <p><span class="label">Comment:</span>" .$message. "</p><br>";
This line is wrong because you break your line delimiters.
Use simple quotes instead of double quotes for the class attribute :
$outputstring = "<br><p><span class='label'>Name:</span> " .$name. "</p><br> <p><span class='label'>Comment:</span>" .$message. "</p><br>";
Or, as stated by chandresh_cool, escape your double quotes.
You cannot use double quotes inside double quotes until you escape them with backslashes, in this case use single quotes instead like this
$outputstring = "<br><p><span class='label'>Name:</span> " .$name. "</p><br> <p><span class='label'>Comment:</span>" .$message. "</p><br>";
$outputstring = '<br><p><span class="label">Name:</span> ' .$name. '</p><br> <p><span class="label">Comment:</span>' .$message. '</p><br>'; This should solve your problem use '' when you don't have dynamic values.

PHP form error issue

There is probably a simple solution for this but i'm not very proficient in php! Basically I want to submit a form and the user be returned with a thank you overlay image without refresh. I've managed to get this to work BUT now the form validating isn't working properly...
I need to make my overlay only appear after the form validating is successful, if it isn't successful I need to display the error instead of the thank you overlay...
I know I could use ajax for this form but I don't want to rely on javascript!
At the minute the validating is working, but the image is being overlayed on top of it...
This is php code:
<?php
if($_POST['formSubmit'] == "Submit")
{
$errorMessage = "";
if(empty($_POST['formName']))
{
$errorMessage .= "<li>You forgot to enter your name</li>";
}
if(empty($_POST['formTown']))
{
$errorMessage .= "<li>You forgot to enter your town</li>";
}
$varName = $_POST['formName'];
$varTown = $_POST['formTown'];
$varAge = $_POST['formAge'];
$varEmail = $_POST['formEmail'];
$varOne = $_POST['hidden-one'];
$varTwo = $_POST['hidden-two'];
$varThree = $_POST['hidden-three'];
$varFour = $_POST['hidden-four'];
$varFive = $_POST['hidden-five'];
if(empty($errorMessage))
{
$fs = fopen("mydata.csv","a");
fwrite($fs,"\n" . $varName . ", " . $varTown . ", " . $varAge . ", " . $varEmail . ", " . $varOne . $varTwo . $varThree . $varFour . $varFive);
fclose($fs);
}
}
?>
This is my html (with the php code):
<?php
if (isset($_POST['formSubmit'])) {
print "<div class=\"thank-you\"><a href='enter.php'><img src='images/thankyou-overlay.png'/></a></div>\n";
}
?>
<div id="mainContainer">
<p>Just complete your entry details below.</p>
<?php
if(!empty($errorMessage)) {
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
}
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" target="_self">
<div class="inputContainer">
<label class="text" name="name">Full Name:</label>
<input type="text" class="box" name="formName" value="<?=$varName;?>">
</div>
... more html inputs...
</form>
Whatever you are going to do, you have to use Javascript. You can choose AJAX either using an iframe where you direct your post to, and reading it in a javascript to check status of posting.
Edit:
Like this you can post it:
<form action="do_stuff.aspx" method="post" target="my_iframe">
<input type="submit" value="Do Stuff!" />
</form>
<!-- when the form is submitted, the server response will appear in this iframe -->
<iframe name="my_iframe" src="not_submitted_yet.aspx"></iframe>
So after the post, you have to read status from this iframe, (in other words de HTML output from it).
First , i am having difficulty comprehending what you are trying to do : But still i can point out a few things that have better alternates ;
You should put this code
if($_POST['formSubmit'] == "Submit")
{
...
}
above the form for the functionality you want
and also the above if should have an else to show the form when there are errors.
like
else
{
---form---
}
try this and c if it helps

Categories