Writing to a file in php - php

I am building a website in php. The contact us section has four different fields asking input from user. I want to save the data submitted by the user into a text file names contact.txt. The problem is that the submit button in the form does not work at all.
PHP CODE
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$mob=$_POST['mobile'];
$msg=$_POST['message'];
$txt=" Name=$name \n\r Email=$email \n\r Mobile=$mob \n\r Message=$msg \r\n\r\n\r";
$fh = fopen('contact.txt', 'a+');
fwrite($fh,$txt);
fclose($fh);
?>
HTML CODE
<div id="contact-section">
<h3>Fill the form to Register </h3>
<div class="status alert alert-success" style="display: none"></div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="text" name="mobile" class="form-control" required="required" placeholder="Mobile">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email ID">
</div>
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="4" placeholder="Institute/College/School"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary pull-right">Submit</button>
</div>
</form>
</div>
</div
Any help is highly appreciated. Thanks.

Test open success :
if($fh = fopen('contact.txt', 'a+') !== FALSE){
fwrite($fh,$txt);
fclose($fh);
}

Combining my ideas with Fky's here is the most complete code:
<?php
if(!empty($_POST) && array_key_exists('name', $_POST) && array_key_exists('email', $_POST) && array_key_exists('mobile', $_POST) && array_key_exists('message', $_POST)) {
$name=$_POST['name'];
$email=$_POST['email'];
$mob=$_POST['mobile'];
$msg=$_POST['message'];
$txt=" Name=$name \n\r Email=$email \n\r Mobile=$mob \n\r Message=$msg \r\n\r\n\r";
$fh = fopen('contact.txt', 'a');
if($fh = fopen('contact.txt', 'a+') !== FALSE){
fwrite($fh,$txt);
fclose($fh);
}
else {
echo "File can not be opened.";
}
}
else {
echo "Nothing Sent.";
}
?>

Related

Data not being posted. Please review my code

I am trying to post the values as shown in the script from an HTML form. I have confirmed that the form is successfully submitting the data. However, when I try to test the values being submitted using the isset() script, I get nothing at all. I am also posting my function to see if I am making an error there.
if(isset($_POST['submit']) && !empty($_POST['submit'])) {
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// require_once('mystuff/functions.php');
echo $to_account = test_input($_POST['to_account']);
echo $to_email = test_input($_POST['to_email']);
echo $amount = test_input($_POST['amount']);
echo $pin = test_input($_POST['pin']);
} else {
echo "Probably the data was not submitted";
}
<div class="container">
<h2>Transfer:</h2>
<form action="transCtrl.php" method="POST">
<div class="form-group">
<label for="to_account">To Account</label>
<input type="text" class="form-control" id="to_account" name="to_account" required pattern="[A-Z0-9]{13}">
</div>
<div class="form-group">
<label for="to_email">To Email:</label>
<input type="email" class="form-control" id="to_email" name="to_email" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,}$" required>
</div>
<div class="form-group">
<label for="amount">Amount</label>
<input type="number" class="form-control" id="amount" name="amount" required min="100">
</div>
<div class="form-group">
<label for="pin">8 Digit PIN</label>
<input type="text" class="form-control" id="pin" name="pin" pattern="[0-9]{8}" required>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
Instead this
if(isset($_POST['submit']) && !empty($_POST['submit'])) {
Use this:
if($_SERVER["REQUEST_METHOD"] == "POST") {
You don't have a $_POST['submit'] variable.

PHP still not reading local file

After several attempts, I have no idea why the log in is not working. I'm trying to log the user in (user password and email stored on a .txt file, each line has count,useremail,password) and then redirect them to index2.php for only users to access. I'm not sure what I'm doing wrong.
log in form located in navbar in index.php
<form class="form" role="form" method="POST" action="login.php" accept-charset="UTF-8" id="login-nav">
<div class="form-group">
<label class="sr-only" for="email">Email address</label>
<input type="email" class="form-control" id="email" placeholder="Email address" required>
</div>
<div class="form-group">
<label class="sr-only" for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Password" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block" id="submit">Sign in</button>
</div>
</form>
login.php code
<?php session_start();?>
<?php
$email=$_POST['$email'];
$pass=$_POST['$password'];
echo $email;
$fileName = "Data/users.txt";
$target = fopen($fileName, "r");
//find user
$users=fgetcsv($target,200,",");
while(!feof($target)){
if($email==$users[1]&&$pass==$users[2]){
$_SESSION['email']=$users[1];
$_SESSION['id']=$users[0];
//if found redirect
$redirect= "index2.php";
header("Location:$redirect");
exit();
}
else{
unset($_SESSION['email']);
unset($_SESSION['id']);
echo "<script>";
echo "alert(Invalid login.);";
echo "location.href='index.php?error=login';";
echo "</script>";
}
}
fclose($target);
?>
You can't echo $email; and then do a header() redirect. You will have to enable output buffering:
ob_start();
echo $email;
// other code
header("Location:$redirect");
ob_end_flush();
And enable error reporting to see what is going wrong.

Get email id on another page

After submit in signup page my signUp page redirects to info.php where I want to collect additional info of user using email id he gives on signup page but when I tried to get the email id of user through sessions, session return empty value.
THIS IS MY SIGNUP CODE
<?php
session_start();
if(isset($_POST['submit'])){
$name= $_POST['_user'];
$email = $_POST['_email'];
$pass = $_POST['_password'];
//Insert Data
$sql = "INSERT INTO signup(name,email,password)
VALUES('$name','$email','$pass')";
//Data Validation
if(mysqli_query($conn,$sql)){
echo "<script>alert('SignUp Successfull')</script>";
$_SESSION['user_email'] = $email;
header('Location: info.php');
}
else{
echo "<script>window.alert('You are already a user.')</script>";
}
}
mysqli_close($conn);
?>
AND THIS MY INFO.PHP CODE
<?php
session_start();
if(isset($_POST['_submit'])){
if(empty($_POST['_address']) || empty($_POST['_country']) || empty($_POST['_number']) || empty($_POST['_cnic']) || empty($_POST['_passport'])){
echo "<script>window.alert('All fields are required')</script>";
}
else{
$address = $_POST['_address'];
$country = $_POST['_country'];
$number = $_POST['_number'];
$cnic = $_POST['_cnic'];
$passport = $_POST['_passport'];
$email=$_SESSION['user_email'];
$query = "INSERT INTO info(email,address,country,mobile,cnic,passport)
VALUES('$email','$address','$country','$number','$cnic','$passport')";
if(mysqli_query($conn,$query)){
header('Location: ../index.php');
}
else{
echo "<script>window.alert('Error While Entering the data!.')</script>";
}
}
}
mysqli_close($conn);
?>
In addition I use this global session variable for login page and it works fine.
UPDATE
SIGNUP HTML CODE
<div class="outside">
<form class="form-horizontal" role="form" method="post">
<div class="form-group">
<label class="control-label col-sm-3 glyphicon glyphicon-user" for="name"></label>
<div class="control-label col-sm-8">
<input type="text" name="_user" class="form-control" id="name" placeholder="Full Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="email">
<img class="glyphicon1" src="../assests/at-sign.png">
</label>
<div class="control-label col-sm-8">
<input type="email" name="_email" class="form-control" id="email" placeholder="Enter Email">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3 glyphicon glyphicon-lock" for="password"></label>
<div class="control-label col-sm-8">
<input type="password" name="_password" class="form-control" id="password" placeholder="Enter Password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-3">
<button name="submit" id="submit" value="Upload" type="submit" class="btn btn-default">Confirm SignUp</button>
</div>
</div>
<p>Already a User? LogIn</p>
</form>
</div>
Use this for Returns the auto generated id used in the last query
mysqli_insert_id($link)

Data not entered in database

Why am I unable to enter data in to db?
<?php include ( './includes/header.php' );
$error = "";
if (#$_POST['send']) {
$name = mysql_real_escape_string(strip_tags($_POST['name']));
$email = mysql_real_escape_string(strip_tags($_POST['email']));
$message = mysql_real_escape_string(strip_tags($_POST['message']));
if ($name == "") {
$error = "Name cannot be left empty.";
}
else if ($email == "") {
$error = "Enter valid email id";
}
else if ($message == "") {
$error = "Message cannot be left empty.";
}
else{
//send message
$sendmessage = mysql_query("INSERT INTO contact VALUES('','$name','$email','$message')",$db1) or die(mysql_error());
$error = "Message sent!!";
}
}
?>
<meta property="og:title" content="Contact Us" />
<meta property="og:description" content="For any help, drop us a mail" />
<meta property="og:image" content="http://studyfoyer.org/images/contactus.jpg" />
<title>Contact Us</title>
</head>
<?php include('includes/navigation.php');?>
<div class="container">
<div class="row">
<form class="log-page" action="contact.php" method="POST">
<h2 class="form-signin-heading">Get in touch</h2>
<div class="input-prepend">
<label for="InputUsername">Name</label>
<input type="text" class="form-control" name='name' placeholder="Name" required autofocus>
</div>
<div class="input-prepend">
<label for="InputEmail">Email</label>
<input type="email" class="form-control" name='email' placeholder="Email address" required>
</div>
<div class="input-prepend">
<label for="InputMessage">Message</label>
<textarea class="form-control" rows="3" name="message" placeholder="Your message" required></textarea>
</div>
<div class="controls form-inline">
<button class="btn btn-primary" name='send' type="submit">Send</button>
</div>
<?php echo $error; ?>
</div>
</form>
</div>
</div>
Db connection is done through header.php
I've two websites(both on localhost) using same db, for contact info. can this affect?
As the code seems to run fine on other one.
It should be if (isset($_POST['send'])) instead of if (#$_POST['send']). isset function will return true is $_POST['send'] is "set". Same files for db connection will not affect anything.
Your code is vulnerable to SQL injection. You must use prepared statements to safely sanitize user's input.

How to Get Contact Form Working

Okay so I am fairly new to web designing. How do I get the contact form on my current theme to work? This is the current html.
I need to know how to code the PHP file; is this correct?
<div class="form row-fluid clearfix">
<div class="field span5">
<label>Your name:</label>
<input type="text" value="" class="req" placeholder="Placeholder text..." />
</div>
<div class="field span5">
<label>Your email:</label>
<input type="email" value="" class="req" />
</div>
<div class="clearfix"> </div>
<div class="field full">
<label>Your comment:</label>
<textarea class="span12" cols="2" rows="7"></textarea>
</div>
<button class="extruded"><span>Submit</span></button>
</div>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: iclear';
$to = 'sales#tangledindesign.com';
$subject = 'Hello';
?>
And how do I link the PHP file for that contact form?
Step 1
Wrap your fields with a form HTML element that has its action property set to your php processing page
Step 2
Name the form fields according to what the php file expects
Step 3
Add some validation
Step 4
Submit and test
Example
HTML
<form action="process.php" method="post">
First Name: <input type="text" name="first_name">
<input type="submit">
</form>
PHP
<?php
$first_name=$_POST["first_name"];
if($first_name=="John")
{
echo "Hi John!";
}
else
{
echo "Sorry Buddy, Don't really know you";
}
?>
Note
The reason why i did not provide you a full solution is that you mentioned you are a newbie in that programming, and it would be injustice to just solve your problem and not guide you how to do it
You need to wrap your HTML with the tag, and don't forget to get include the submit button:
<form action="process.php" method="post">
<div class="form row-fluid clearfix">
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="message">
<input type="submit" name="submit">
</div>
</form>
Then here is the php (process.php) file to get all values from your HTML form:
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: iclear';
$to = 'sales#tangledindesign.com';
$subject = 'Hello';
Hope this help.
Try this code
<?php
$toaddress ="youremail#domain.com" //change to your email address
$error ="";
if($_SERVER['REQUEST_METHOD']=="POST") {
$name=$_POST['name'] ;
$email=$_POST['email'] ;
$comment=$_POST['comment'] ;
if(!isset($name) || $name==""){
$error .="Please Enter your name <br/>";
}elseif (!isset($email) || $email==""){
$error .="Please Enter your email Address.<br/>";
}elseif(!isset($comment) || $comment==""){
$error .="Please Enter your Comments.<br/>";
}
if ($error ==""){
mail($toaddress,"Contact form",$comment)
}
}
?>
<?php echo $error ;?>
<form method='post' action='' enctype='multipart/form-data' id='news_form' name='post_form' >
<div class="form row-fluid clearfix">
<div class="field span5">
<label>Your name:</label>
<input name="name" type="text" value="" class="req" placeholder="Placeholder text..." />
</div>
<div class="field span5">
<label>Your email:</label>
<input type="email" value="" class="req" name="email" />
</div>
<div class="clearfix"> </div>
<div class="field full">
<label>Your comment:</label>
<textarea class="span12" cols="2" rows="7" name="comment"></textarea>
</div>
<input type="submit" value="submit" />
</div>
</form>

Categories