Form shows '/' in every field - php

My php script to send mail is as follows :
<?php
if (isset($_POST['submit'])) {
$to='info#animalswecare.com';
$fname=stripslashes($_POST['fname']);
$email=$_POST['email'];
if (get_magic_quotes_gpc())
$email = stripslashes($email);
//$email=trim($email, '/');
$msg=$_POST['msg'];
$msg=stripslashes($msg);
$message="Name: $fname\n" ."Message: $msg\n";
mail($to,$subject,$message,'From:'.$email) ;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contact us">
<label>Name:</label>
<input type="text" name="fname" value=<?php if(!empty($fname)) echo $fname; ?> /><br />
<label>Email:</label>
<input type="text" name="email" value=<?php if (!empty($email)) echo $email; ?> /><br />
<label>Message:</label>
<textarea name="msg" rows="5"><?php if (!empty($msg)) echo $msg; ?></textarea> <br />
<input type="submit" name="submit" value="Post" />
</form>
But when the form appears, there is a / added in every field.
I have tried using trim, rtim, get magic quotes and stripslashes but nothing is working.

It because your value='s are not being ended.
Try this: <input type="text" name="fname" value="<?php if(!empty($fname)) echo $fname; ?>" /> and apply the same learning's to all of the inputs.
Also I re-formatted your code and added label in the CSS..so you don't have use those ugly line-spaces.
<?php
if (isset($_POST['submit'])) {
$to = 'info#animalswecare.com';
$fname = stripslashes($_POST['fname']);
$email = $_POST['email'];
if (get_magic_quotes_gpc()) {
$email = stripslashes($email);
}
// $email = trim($email, '/');
$msg = $_POST['msg'];
$msg = stripslashes($msg);
$message = "Name: $fname\n" ."Message: $msg\n";
mail($to, $subject, $message, 'From:' . $email);
}
?>
<style>
label {width: 120px;}
</style>
<form action="" method="post">
<label>Name:</label>
<input type="text" name="fname" value="<?php if(!empty($fname)) echo $fname; ?>" /><br />
<label>Email:</label>
<input type="text" name="email" value="<?php if (!empty($email)) echo $email; ?>" /><br />
<label>Message:</label>
<textarea name="msg" rows="5"><?php if (!empty($msg)) echo $msg; ?></textarea><br />
<input type="submit" name="submit" value="Post" />
</form>

try adding " " around the value attribute
ie
<input type="text" name="email" value="<?php if (!empty($email)) echo $email; ?>" />

This will teach you to follow standards and always enclose tag parameter values in quotes

First of all, don't use stripslashes(). Simply disable magic_quotes_gpc completely, using the php.ini file or .htaccess. If you can't, view the accepted answer to this question: How to turn off magic quotes on shared hosting?
About your error...
value=<?php if (!empty($email)) echo $email; ?> />
If you look, you didn't put the quotes around the attribute "value".
value="<?php if (!empty($email)) echo $email; ?>" />
will fix it

Related

Update query for database in PHP not working

I am having trouble updating my database data. I put the data that I want to update but when I click on the "Update" button it does
nothing. I have called the file on another php file by using Update
It also shows this error
Notice: Undefined variable: fname in
C:\xampp\htdocs\project\change1.php on line 71
Can someone help me figure this issue, please?
<?php include("config.php"); ?>
<?php
if (isset($_GET['edit'])) {
$update = true;
$record = mysqli_query($con, "SELECT * FROM employee WHERE id='".$_GET['edit']."'");
$row = mysqli_fetch_array($record,MYSQLI_BOTH);
}
if (isset($_POST['update'])) {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$password = $_POST['password'];
$addr = $_POST['addr'];
$phone = $_POST['phone'];
$id=$_GET['edit'];
$query = "UPDATE employee SET fname='".$fname."',lname='".$lname."',password='".$password."',addr='".$addr."',phone='".$phone."' WHERE id='".$id."'";
$result = mysqli_query($con,$query) or die ("problem inserting new record into database");
if($result){
header('location: show_db.php');
}
else {echo "Update not successful"; }
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Update Data</title>
</head>
<body>
Home
<br/><br/>
<input type="hidden" name="id" value="<?php echo $id; ?>">
Name:<input type="text" name="fname" value="<?php echo $fname ; ?>">
Surname:<input type="text" name="lname" value="<?php echo $lname; ?>">
Password:<input type="text" name="password" value="<?php echo $password; ?>">
Address:<input type="text" name="addr" value="<?php echo $addr; ?>">
Contact:<input type="text" name="phone" value="<?php echo $phone; ?>">
<input type="submit" name="update" value="Update">
</body>
</html>
Put the html inputs inside a form
<form name ="form1" method ="get" action="">
<input type="hidden" name="id" value="<?php echo $id; ?>">
Name:<input type="text" name="fname" value="<?php echo $fname ; ?>">
Surname:<input type="text" name="lname" value="<?php echo $lname; ?>">
Password:<input type="text" name="password" value="<?php echo $password; ?>">
Address:<input type="text" name="addr" value="<?php echo $addr; ?>">
Contact:<input type="text" name="phone" value="<?php echo $phone; ?>">
<input type="submit" name="update" value="Update">
</form>

$_POST returning empty value

EDIT: The code seems to work if I place the PHP code in the same file at the HTML. I'll implement this method for now. Thank you for the help!
Whenever I try to echo the values in a PHP file from an input box the values appear to be empty.
Here is my HTML code
<form action="sendmail.php" method="post">
<label>Name:</label><br>
<input name="person" type="text" /><br>
<label>Email:</label><br>
<input name="email" type="email" /><br>
<label>Opportunity:</label><br>
<textarea name="message" rows="8"></textarea><br>
<input type="submit" value="Submit" class="button">
</form>
This is my sendmail.php file
$name = $_POST['person'];
$email = $_POST['email'];
$message = $_POST['message'];
echo $name;
echo $email;
echo $message;
You can get the value in Your current page itself
<?php
if(isset($_POST['person']))
{
$name = $_POST['person'];
$email = $_POST['email'];
$message = $_POST['message'];
echo "Name".$name;
echo "Email".$email;
echo "Message".$message;
}
?>
<form action="" method="post">
<label>Name:</label><br>
<input name="person" type="text" /><br>
<label>Email:</label><br>
<input name="email" type="email" /><br>
<label>Opportunity:</label><br>
<textarea name="message" rows="8"></textarea><br>
<input type="submit" value="Submit" class="button">
</form>
or if you want the data in another page
<form action="send.php" method="post">
<label>Name:</label><br>
<input name="person" type="text" /><br>
<label>Email:</label><br>
<input name="email" type="email" /><br>
<label>Opportunity:</label><br>
<textarea name="message" rows="8"></textarea><br>
<input type="submit" value="Submit" class="button">
send.php file
<?php
if(isset($_POST['person']))
{
$name = $_POST['person'];
$email = $_POST['email'];
$message = $_POST['message'];
echo "Name".$name;
echo "Email".$email;
echo "Message".$message;
}
?>
I tried this code and it worked fine, so whatever is wrong it is not with the code you have pasted here.
to make sure the post is successful, try this:
if(isset($_POST['person']))
echo "passed fine.";
check:
if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST'){
if(isset($_POST['person'])){
$name = $_POST['person'];
echo $name;
}
}
else{
echo"Your form submission is not correct";
}
try this code.
HTML
<form action="sendmail.php" method="post">
<label>Name:</label><br>
<input name="person" type="text" /><br>
<label>Email:</label><br>
<input name="email" type="email" /><br>
<label>Opportunity:</label><br>
<textarea name="message" rows="8"></textarea><br>
<input type="submit" value="Submit" class="button">
</form>
PHP
<?php
$name = $_POST['person'];
$email = $_POST['email'];
$message = $_POST['message'];
echo $name;
echo $email;
echo $message;
?>
<form action="456.php" method="post">
<label>Name:</label><br>
<input name="person" type="text" /><br>
<label>Email:</label><br>
<input name="email" type="email" /><br>
<label>Opportunity:</label><br>
<textarea name="message" rows="8"></textarea><br>
<input type="submit" value="Submit" class="button">
</form>
<?
print_r($_POST);
?>

Why is my form validation not working?

I'm having troubles validating my form. How do I validate the form using PHP? I've tried lots of different methods and nothing has worked. I can get the inputs to display (although check-box doesn't always display) but it just won't validate.
I also want to display the user's inputs (after it has been validated) onto another page, how do I do that?
Here is my code;
Form:
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<label for="name">Your Name:</label>
<input type="text" name="name" id="name" value="" required>
<br><br>
<label for="email">Your Email:</label>
<input type="text" name="email" id="email" value="" required>
<br>
<br>
<label for="subject">Subject:</label>
<input type="text" name="subject" id="subject" value="" required>
<br>
<br>
Recipient:
<div>
<label for="admin">
<input type="checkbox" name="recipient[]" id="admin" value="Administrator">
Administrator</label>
<br>
<label for="editor">
<input type="checkbox" name="recipient[]" id="editor" value="Content Editor">
Content Editor</label>
<br>
</div>
<br>
<label for="message">Message:</label>
<br>
<textarea name="message" id="message" cols="45" rows="5" required></textarea>
<input type="hidden" name="submitted" value="1">
<br>
<input type="submit" name="button" id="button" value="Send">
<br>
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if ($_POST['submitted']==1) {
if ($_POST['name']){
$name = $_POST['name'];
}
else{
echo "<p>Please enter a name.</p>" ;
}
if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email))
$email = $_POST['email'];
}
else{
echo "<p>Please enter a valid email.</p>";
}
if ($_POST['subject']){
$subject = $_POST['subject'];
}
else{
echo "<p>Please enter a subject.</p>";
if(empty($_POST['recipient'])){
echo "<p>Please select a recipient</p>";
}else{
for ($i=0; $i < count($_POST['recipient']);$i++) {
echo $_POST['recipient'][$i] . " ";
}
}
}
if ($_POST['message']){
$message = $_POST['message'];
}
/* go to form.php
display results
echo "<strong>Your Name:</strong> ".$name. "<br />";
echo "<strong>Your Email:</strong> ".$email. "<br />";
echo "<strong>Subject:</strong> ".$subject. "<br />";
echo "<strong>Recipient:</strong> ";
echo "<br />";
echo "<strong>Message:</strong> <br /> " .$message;
*/
?>
if (preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)#[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,3})$/i", $email)
You can't use email in this condition because $email is empty!

submitted my php form, but none of the values were sent

For some reason, the options are not showing up in my email. I can get the email to send just fine. I can see the body and all its comments, but none of the entries that the user made. I know I am doing something wrong, but I cannot determine what it is.
Also, feel free to mock me if it looks horrible. :)
$ToEmail = 'dmandrade1978#gmail.com';
$EmailSubject = 'Message from web page!!';
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$urphone = $_POST['urphone'];
$event = $_POST['event'];
$date = $_POST['date'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$comment = $_POST['comment'];
$hearaboutus = $_POST['hearaboutus'];
$body = <<<EMAIL
Email: $email <br />
Name: $name <br />
Comment: $comment <br />
Phone: $urphone <br />
Date: $urdate <br />
Comment: $comment <br />
How did you hear?: $hearaboutus <br />
Mail optiom: $mail <br />
EMAIL;
$header = "Content-type: text/html\r\n";
mail("$ToEmail", "$EmailSubject", "$body", "$header");
echo ("Message Sent!");
?>
<td class = "form">
<form action="?" method="get" enctype="text/plain">
<p class = "form">Name:<br />
<input type="text" name="name" id="name" /></p>
<p class = "form">E-mail:<br />
<input type="text" name="email" id="email" /></p>
<p class = "form">Phone #:<br />
<input type="text" name="urphone" id="urphone" /></p>
<p class = "form">Event type:<br />
<input type="text" name="event" id="event" /></p>
<p class = "form">Date of event:<br />
<input type="text" name="date" id="date" /></p>
<p class = "form" >Prefered method of contact:<br />
<span class = "contact">
<input type="radio" name="phone" id="phone" /> Phone<br />
<input type="radio" name="mail" id="mail" /> E-mail<br />
</span></p>
<p class = "form">How did you hear about us?:<br />
<select name="hearaboutus" id="hearaboutus" />
<option value="internet">Internet</option>
<option value="word of mouth">Friend/Family</option>
<option value="magazine">Magazine</option>
<option value="other">Other</option>
</select></p>
<p class = "form">Message, questions, or availability:<br />
<textarea rows="10" cols="30" name="comment" id="comment">
</textarea></p>
<input type="submit" value="Send email to us" id="submit">
<input type="reset" value="Reset and start over">
</form>
Change method="get"
to
method="post"
and remove enctype . It's not required in this case.
Also, why is there a ? in your action? You can keep action blank as it is posting to the same page.
To make things even simpler for you, copy paste the below code and run the file again
if(isset($_POST['submit']))
{
$ToEmail = 'dmandrade1978#gmail.com';
$EmailSubject = 'Message from web page!!';
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$urphone = $_POST['urphone'];
$event = $_POST['event'];
$date = $_POST['date'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$comment = $_POST['comment'];
$hearaboutus = $_POST['hearaboutus'];
$body = <<<EMAIL
Email: $email <br />
Name: $name <br />
Comment: $comment <br />
Phone: $urphone <br />
Date: $urdate <br />
Comment: $comment <br />
How did you hear?: $hearaboutus <br />
Mail optiom: $mail <br />
EMAIL;
$header = "Content-type: text/html\r\n";
mail("$ToEmail", "$EmailSubject", "$body", "$header");
echo ("Message Sent!");
}
?>
<td class = "form">
<form action="" method="post">
<p class = "form">Name:<br />
<input type="text" name="name" id="name" /></p>
<p class = "form">E-mail:<br />
<input type="text" name="email" id="email" /></p>
<p class = "form">Phone #:<br />
<input type="text" name="urphone" id="urphone" /></p>
<p class = "form">Event type:<br />
<input type="text" name="event" id="event" /></p>
<p class = "form">Date of event:<br />
<input type="text" name="date" id="date" /></p>
<p class = "form" >Prefered method of contact:<br />
<span class = "contact">
<input type="radio" name="phone" id="phone" /> Phone<br />
<input type="radio" name="mail" id="mail" /> E-mail<br />
</span></p>
<p class = "form">How did you hear about us?:<br />
<select name="hearaboutus" id="hearaboutus" />
<option value="internet">Internet</option>
<option value="word of mouth">Friend/Family</option>
<option value="magazine">Magazine</option>
<option value="other">Other</option>
</select></p>
<p class = "form">Message, questions, or availability:<br />
<textarea rows="10" cols="30" name="comment" id="comment">
</textarea></p>
<input type="submit" name="submit" value="Send email to us" id="submit">
<input type="reset" value="Reset and start over">
</form>

PHP Mail stops working without me editing the code

I have a problem with my PHP Mail. It stops working from time to time without me even touching the code. I have a script which checks if required forms are empty. This script works. But if I fill in all the required fields I should get a text which says "Your mail was successfully sent" but I don't. But if I edit my code by just moving a bit of text some lines down and back to the original position again it works, for a while. It's often under the night it stops working. Can it be the server that is causing trouble or is it my code that I posted below?
<?php
if(isset($_POST['submit'])){
$namn = strip_tags($_POST['namn']);
$foretag = strip_tags($_POST['foretag']);
$adress = strip_tags($_POST['adress']);
$postnr = strip_tags($_POST['postnr']);
$ort = strip_tags($_POST['ort']);
$telefon = strip_tags($_POST['telefon']);
$epost = strip_tags($_POST['epost']);
$meddelande = strip_tags($_POST['meddelande']);
function check_required_fields($required_array){
$field_errors = array();
foreach($required_array as $fieldname){
if ((!isset($_POST[$fieldname])) || (empty($_POST[$fieldname]))){
if($_POST[$fieldname] != '0'){
$field_errors[] = $fieldname;
}
}
}
return $field_errors;
}
$errors = array();
$required_fields = array('namn', 'telefon', 'meddelande');
$errors = array_merge($errors, check_required_fields($required_fields));
if(empty($errors)){
$meddelande=nl2br($meddelande);
if(empty($foretag)){ $foretag='-'; }
if(empty($adress)){ $adress='-'; }
if(empty($postnr)){ $postnr='-'; }
if(empty($ort)){ $ort='-'; }
if(empty($epost)){ $epost='-'; }
$body =
"
Namn: <b>". $namn ."</b><br />
Företag: <b>". $foretag ."</b><br />
Adress: <b>". $adress ."</b><br />
Postnr: <b>". $postnr ."</b><br />
Ort: <b>". $ort ."</b><br />
Telefon: <b>". $telefon ."</b><br />
E-post: <b>". $epost ."</b><br /><br />
Meddelande: <b><br />". $meddelande
;
$headers = "From: $namn <webmaster#allflytt.com>\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "X-Mailer: PHP v".phpversion();
$success = mail('info#allflytt.com', 'Meddelande', $body, $headers);
}
}
?>
<form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<?php
if(!empty($errors)){
echo "<p class=\"field_error\">De rödmarkerade fälten måste fyllas i.</p>";
}
?>
<?php
if(empty($errors) && $success){
echo "<p class=\"p_success\">Tack för ditt meddelande! Vi kommer att besvara det inom kort.</p>";
}
?>
<p>
<label for="namn">
<?php if(!empty($errors)){if(in_array("namn", $errors)){echo "<span class=\"field_error\">";}}?>Namn: *<?php if(!empty($errors)){if(in_array("namn", $errors)){echo "</span>";}} ?>
</label><br />
<input type="text" name="namn" id="namn" class="text" tabindex="15" value="<?php if(!empty($errors)){ echo $namn; } ?>" />
<br />
<label for="foretag">Företag:</label><br />
<input type="text" name="foretag" id="foretag" class="text" tabindex="20" value="<?php if(!empty($errors)){ echo $foretag; }?>" />
<br />
<label for="adress">Adress:</label><br />
<input type="text" name="adress" id="adress" class="text" tabindex="30" value="<?php if(!empty($errors)){ echo $adress; } ?>" />
<br />
<label for="postnr">Postnummer:</label><br />
<input type="text" name="postnr" id="postnr" class="text_medium" tabindex="40" value="<?php if(!empty($errors)){ echo $postnr; } ?>" />
<br />
<label for="ort">Ort:</label><br />
<input type="text" name="ort" id="ort" class="text" tabindex="50" value="<?php if(!empty($errors)){ echo $ort; } ?>" />
<br />
<label for="telefon">
<?php if(!empty($errors)){if(in_array("telefon", $errors)){echo "<span class=\"field_error\">";}}?>Telefon: *<?php if(!empty($errors)){if(in_array("telefon", $errors)){echo "</span>";}} ?>
</label><br />
<input type="text" name="telefon" id="telefon" class="text" tabindex="60" value="<?php if(!empty($errors)){ echo $telefon; } ?>" />
<br />
<label for="epost">E-post:</label><br />
<input type="text" name="epost" id="epost" class="text" tabindex="70" value="<?php if(!empty($errors)){ echo $epost; } ?>" />
<br />
<label for="meddelande">
<?php if(!empty($errors)){if(in_array("meddelande", $errors)){echo "<span class=\"field_error\">";}}?>Meddelande: *<?php if(!empty($errors)){if(in_array("meddelande", $errors)){echo "</span>";}} ?>
</label><br />
<textarea name="meddelande" id="meddelande" class="textarea" tabindex="80"><?php if(!empty($errors)){ echo $meddelande; } ?></textarea>
<br />
<input type="submit" name="submit" value="Skicka" class="submit" />
</p>
</form>
could not find if anything wrong in your code.
check if APC cache is enabled in your server.
sometimes it creates problem in taking updated code.
if you are using SMTP server then mail() function may not work.
you can use PEARS for this.
I have experienced a similar problem. Turned out the browser, by error, sometimes double-posts your form; One time with content and another time without content.
If no content is submitted to your script, obviously there's nothing to act upon. There are a number of ways to check on this, but they way I discovered it was a desperate last resort measure where I sent emails to myself with each run of the script. And I showed that I often got two mails where the script had run only once.
It's worht trying :)
Problem solved. Changed to a new server host. No issues any more!

Categories