PHP contact form won't send email, echoes back [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I'm having some issue with my PHP. I'm new to it, so it's all kind of confusing. But the big issue is trying to send email thru a contact form and failing miserably.
I get this error:
Parse error: syntax error, unexpected '$name' (T_VARIABLE) in C:\wamp\www\mtltechevents\contact_form.php on line 7
Line 7:
$name = $_POST['name1'];
I can't see a syntax error. When I remove the error message, it echoes back when I try to fill in the info, as well:
$name = $_POST['name1'];
$email = $_POST['email1'];
$message = $_POST['message1'];
$contact = $_POST['contact1'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
// After sanitization Validation is performed
if (filter_var($email1, FILTER_VALIDATE_EMAIL)) {
if (!preg_match("/^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]* (\d{4})(?: ​*x(\d+))?\s*​$/", $contact)) {
echo "<span>* Please Fill Valid Contact No. *</span>";
Any thoughts? I checked out some of the other issues with contact forms and none really answered what my problem was.

Your error would usually show up if you are missing a semi-colon or bracket above the stated line as it is expecting you to do something else. An example of this would be
$anothervariable = $_POST['anotherpost'] // No semi-colon
$name = $_POST['name1'];
$email = $_POST['email1'];
$message = $_POST['message1'];
$contact = $_POST['contact1'];

Is session started?? Can you post complete code. It sounds like kind of syntax error

Related

Warning: Cannot modify header information - headers already sent by( [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 3 years ago.
I want to pass a variable to a page, but cannot modify header... is always in the way
if(isset($_POST['send']))
{
$sender = $_SESSION['Email'];
$receiver = $_POST['email'];
$message = $_POST['textmessages'];
$date = date("Y-m-d h:i:sa");
$q = 'INSERT INTO `tblmessage`(`id`,`sender_name`,`receiver_name`,`message_text`,`date_time`,`userid`)
VALUES("","'.$sender.'","'.$receiver.'","'.$message.'","'.$date.'","'.$user_id.'")';
$r = mysqli_query($con, $q);
if($r)
{
header("location.href='messages.php?user=".$receiver);
}
else
{
echo $q;
}
}
I tried passing the user to the page, but:
output started at C:\xampp\htdocs\MBPH(Beta)\messages.php:79) in `C:\xampp\htdocs\MBPH(Beta)\newmessage.php on line 38
always pops up, that line 38 is
'header("location.href='messages.php?user=".$receiver);'
You could add ob_end_flush() before your header() function. It will clear out the current output buffer and your header() should be sent.
Reference: ob_end_flush() documentation
You can try this code
ob_start();
Your output here
ob_get_contents();

PHP Email $Subject Line Will Not Pull Variable

I've searched around with no luck here. I created a PHP email form that works great. Now I need to edit the email subject line to pull information from the contact form.
Here's what I'm looking to have as the email subject line:
"You have a $variable inquiry from $variable.
Here's my current code that is giving me an error on the $Subject line:
$EmailFrom = "Contact#xxx.com";
$EmailTo = "ryan#xxx.com";
$Subject = "You have a .$Subj inquiry from .$Name";
$Name = Trim(stripslashes($_POST['sender_name']));
$Email = Trim(stripslashes($_POST['sender_email']));
$Message = Trim(stripslashes($_POST['message']));
$System = Trim(stripslashes($_POST['system']));
$Item = Trim(stripslashes($_POST['item']));
$Subj = Trim(stripslashes($_POST['subj']));
So I have tried adding a <> around the variables in the $Subject. I have also tried with the periods as above, as well as with just the $xxx.
When I send the email, it will send as:
"You have a <> inquiry from <>"
or
"You have a . inquiry from ."
Depending on which characters I have around the $xxx variable.
I know this is probably some stupid error, but can someone hint me at what I need to do to have those variable pulled into the $Subject?
Thanks in advance,
Ryan
You have your variable declarations out of order.
You are using $Name in the first line before you have assigned a value to it. You need to put the lines that give values to $Name and $Subj before the line that uses them.

php email sender code dose not work properly [duplicate]

This question already has answers here:
Does page reload ever cause post?
(3 answers)
Closed 8 years ago.
can you please tell anybody what is the error this my code. this code working properly but first time i click the send button after send message to my email. but second time i don't need to click the send button only i do refresh my page then message send automatically to my email. what's the problem?
if(isset($_POST['send'])) {
$name = $_POST['fname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$interested = $_POST['interested'];
$message = $_POST['message'];
if(!empty($name) && !empty($email) && !empty($message) )
{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Regoora Message Center";
$message1 = "Customer Name :".$name."
Customer Email :".$email."
Customer Phone :".$phone."
Customer interested :".$interested ."
Customer message :".$message." ";
mail("email#example.com",$subject,$message1);
$mess = "Successfully sent your inquiry";
}
else{
$mess = 'We are sorry, but there appears to be a problem with the form you submitted.';
}}
If your code worked properly the first time, it is because it works fine. The second time you just refreshed the page and it sent another email because 'Refresh' will always repeat your last action. If you last action was 'Send Email', refreshing the page will try to resend the email.
What you could do to avoid that is, after sending, click on the address bar (http://localhost/xxxx) and press enter. It will reset the page.

Check Fields are filled otherwise display error in php [duplicate]

This question already has answers here:
What does ? ... : ... do? [duplicate]
(8 answers)
Closed 8 years ago.
in this i have to check if in fields data is fields then it submit otherwise it can display error with if statements
<?php
include("config.php");
$name=$_POST["name"];
$email=$_POST["email"];
$phone=$_POST["phone"];
$budget=$_POST['budget'];
$insert_query="insert into form(name,email,phone,budget) values ('$name','$email','$phone','$budget')";
$con=mysql_query($insert_query);
?>
take a look at empty() function
http://php.net/empty
if (empty($_POST["name"])) {
die('name is empty');
}
Consider the following...
thispage.php
<?php
$name = (empty($_GET['name'])) ? "Fred" : $_GET['name'];
echo $name;
?>
thispage.php = 'Fred'
thispage.php?Wilma = 'Wilma'
And usual caveats about sql injection, prepared statements, deprecated methods, etc.
First you MUST clean post fields before writing in db. Use function mysql_real_escape_string($var)
And second, you can check field data with function empty, for example:
if (!empty($_POST["name"])) {
$name = mysql_real_escape_string($_POST["name"]);
// your query
} else {
echo 'Name is empty!';
}

Why doesn't this work? :/ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I don't understand why this doesn't work? It's a register form checking if fields are filled in,password is equal to retype password, and if it doesn't already exist in database.
I get this error: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/a4550840/public_html/newreg.php on line 32
But I already put a ';' at line 32 ... I don't understand why this error occurs.
Any help is appreciated :).
EDIT: Fixed that error ^ and added the mysql_real_escape_string , but it doesn't register the information to the database for some reason?
EDIT: It works now :), took away the quotations from the query
<?php
include ('connect.php');
if ($_POST['submit']) {
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$repassword = mysql_real_escape_string($_POST['repassword']);
$email = mysql_real_escape_string($_POST['email']);
if ($username && $password && $repassword && $email){
if ($password == $repassword) {
$check = mysql_query("SELECT * FROM members WHERE username='$username' ");
$countrows = mysql_num_rows($check);
if ($countrows == 0){
mysql_query("INSERT INTO members ('username','password','email') VALUES ('$username','$password','$email') ");
} else {
echo 'Username already exists';
}
} else {
echo 'Passwords don'\t match';
}
} else {
echo 'Fill in the fields';
}
} else {
echo 'Register please';
}
?>
You have a problem here:
echo 'Passwords don't match';
You need scape single quote as:
echo 'Passwords don\'t match';
or
echo "Passwords don't match";
NOTE: Your code is vulnerable to sql injection, you should use mysql_real_scape_string() before to pass yours parameters as sql query.
I suggest:
$username = mysql_real_scape_string($_POST['username']);
$password = mysql_real_scape_string($_POST['password']);
$repassword = mysql_real_scape_string($_POST['repassword']);
$email = mysql_real_scape_string($_POST['email']);
TIP: When your are testing (in dev environment) mysql querys, you should combine die(mysql_error()) at the end of line to check if you has a problem like as:
mysql_query('your sql') or die(mysql_error()).
If you have an error, this cause your app die an show the mysql error.
See this reference.
This error shows the earliest time it encounters a problem. The problem is on that line, or on a previous line. In this case you didn't escape a quote, so the parser found the rest of your string while it expected a , or ;. If you look at the colouring of your code, you'll see that more easily. The correct line would be
echo 'Passwords don\'t match';

Categories