Saving user Input? - php

How do I save my user input to a simple .txt?
I am really new to php so please help me out here.
My code:
<?php
if(isset($_POST['email']) && isset($_POST['password'])) {
$data = $_POST['email'] . '-' . $_POST['password'] . "\n";
$ret = file_put_contents('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
?>
Don't think I need to include this but I found a really cool looking login form and changed the names of course:
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Material Login Form</title>
<link rel="stylesheet" href="css/reset.css">
<link rel='stylesheet prefetch' href='http://fonts.googleapis.com/css?family=Roboto:400,100,300,500,700,900|RobotoDraft:400,100,300,500,700,900'>
<link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css'>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<!-- Mixins-->
<!-- Pen Title-->
<div class="pen-title">
<h1>Material Login Form</h1></div>
<div class="rerun">Rerun Pen</div>
<div class="container">
<div class="card"></div>
<div class="card">
<h1 class="title">Login</h1>
<form>
<div class="input-container">
<input name="email" type="text" id="Username" required="required"/>
<label for="Username">Email</label>
<div class="bar"></div>
</div>
<div class="input-container">
<input name="password" type="password" id="Password" required="required"/>
<label for="Password">Password</label>
<div class="bar"></div>
</div>
<div class="button-container">
<button><span>Go</span></button>
</div>
<div class="footer">Forgot your password?</div>
</form>
</div>
<div class="card alt">
<div class="toggle"></div>
<h1 class="title">Register
<div class="close"></div>
</h1>
<form>
<div class="input-container">
<input type="text" id="Username" required="required"/>
<label for="Username">Email</label>
<div class="bar"></div>
</div>
<div class="input-container">
<input type="password" id="Password" required="required"/>
<label for="Password">Password</label>
<div class="bar"></div>
</div>
<div class="input-container">
<input type="password" id="Repeat Password" required="required"/>
<label for="Repeat Password">Repeat Password</label>
<div class="bar"></div>
</div>
<div class="button-container">
<button><span>Next</span></button>
</div>
</form>
</div>
</div>
<!-- Portfolio--><a id="portfolio" href="http://andytran.me/" title="View my portfolio!"><i class="fa fa-link"></i></a>
<!-- CodePen--><a id="codepen" href="http://codepen.io/andytran/" title="Follow me!"><i class="fa fa-codepen"></i></a>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="js/index.js"></script>
</body>
</html>
I'm at a loss here, I have no clue what to fix!

something like this:
$content = $_POST['email'];
$myfile = fopen("".$name.".txt", "w") or die("error"); // make txt file
fwrite($myfile, $content); // write some content
fclose($myfile); // save and close

<div class="pen-title">
<h1>Material Login Form</h1></div>
<div class="rerun">Rerun Pen</div>
<div class="container">
<div class="card"></div>
<div class="card">
<h1 class="title">Login</h1>
<form action="upload.php" method="post">
<div class="input-container">
<input name="email" type="text" id="Username"
required="required"/>
<label for="Username">Email</label>
<div class="bar"></div>
</div>
<div class="input-container">
<input name="password" type="password"
id="Password" required="required"/>
<label for="Password">Password</label>
<div class="bar"></div>
</div>
<div class="button-container">
<button type="submit">Go</button>
</div>
<div class="footer"><a href="#">Forgot your password?
</a></div>
</form>
</div>
</div>
//in php file use this code for saving file
$myfile= fopen('/tmp/mydata.txt','w') or die("Unable to open file!");;
fwrite($myfile,$_POST);//or $data

Related

PHP form submission error

I created new inquiry form for my website. But it's giving the following error message.
Error
Sorry there was an error sending your form.
mail:Could not instantiate mail function.
Form HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PHP Contact Form Script With Validation - reusable form</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" href="form.css" >
<script src="form.js"></script>
</head>
<body >
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h2>Contact Us</h2>
<p> Send us your message and we will get back to you as soon as possible </p>
<form role="form" method="post" id="reused_form">
<div class="row">
<div class="col-sm-6 form-group">
<label for="name"> First Name:</label>
<input type="text" class="form-control" id="firstname" name="firstname" maxlength="50">
</div>
<div class="col-sm-6 form-group">
<label for="name"> Last Name:</label>
<input type="text" class="form-control" id="lastname" name="lastname" maxlength="50">
</div>
</div>
<div class="row">
<div class="col-sm-6 form-group">
<label for="email"> Email:</label>
<input type="text" class="form-control" id="email" name="email" maxlength="50">
</div>
<div class="col-sm-6 form-group">
<label for="email"> Phone:</label>
<input type="tel" class="form-control" id="phone" name="phone" required maxlength="50">
</div>
</div>
<div class="row">
<div class="col-sm-12 form-group">
<label for="name"> Message:</label>
<textarea class="form-control" type="textarea" id="message" name="message" placeholder="Your Message Here" maxlength="6000" rows="7"></textarea>
</div>
</div>
<div class="row">
<div class="col-sm-12 form-group">
<button type="submit" class="btn btn-lg btn-success btn-block" id="btnContactUs">Post It! </button>
</div>
</div>
</form>
<div id="success_message" style="width:100%; height:100%; display:none; "> <h3>Sent your message successfully!</h3> </div>
<div id="error_message" style="width:100%; height:100%; display:none; "> <h3>Error</h3> Sorry there was an error sending your form. </div>
</div>
</div>
</div>
</body>
</html>
Handler.php:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/*
Tested working with PHP5.4 and above (including PHP 7 )
*/
require_once './vendor/autoload.php';
use FormGuide\Handlx\FormHandler;
$pp = new FormHandler();
$validator = $pp->getValidator();
$validator->fields(['firstname','lastname', 'email','phone'])->areRequired()->maxLength(50);
$validator->field('email')->isEmail();
$validator->field('message')->maxLength(6000);
$pp->sendEmailTo('name#mail.com'); // ← Your email here
echo $pp->process($_POST);
You are not defining an action for the form.
<form role="form" method="post" id="reused_form" action="Handler.php">
<!-- html goes here -->
</form>

php error and even after defining variable

this is my block of code for insert product of my project
<?php
include('includes/db.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html lang="EN">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="shortcut icon" href="images/logo.jpg" type="image/x-icon" />
<title>Insert Products</title>
<script src="https://cloud.tinymce.com/stable/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' });</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap.min.css" />
<link rel="stylesheet" href="fontawesome/css/font-awesome.min.css" />
</head>
<body>
<div class="row">
<div class="col-lg-12">
<ol class="breadcrumb">
<li class="active"><i class="fa fa-dashboard"></i>Dashboard/Insert Products</li>
</ol> <!--breadcrumb ends here-->
</div> <!---col-lg-12 ends-->
</div> <!--row ends-->
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
<i class="fa fa-money fa-fw"></i> Insert Products
</h3>
</div> <!--panel heading ends--->
<div class="panel-body">
<form class="form-horizontal" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="col-md-3 control-label" >Product title</label>
<div class="col-md-6">
<input type="text" name="product_title" class="form-control" required>
</div>
</div> <!---form-group-->
<div class="form-group">
<label class="col-md-3 control-label" >Product Category</label>
<div class="col-md-6">
<select name="product_cat" class="form-control" required>
<option>Select Category</option>
<?php
$get_p_cats="select * from product_categories";
$run_p_cats=mysqli_query($conn,$get_p_cats);
while($row_p_cats=mysqli_fetch_array($run_p_cats)){
$p_cat_id=$row_p_cats['p_cat_id'];
$p_cat_title=$row_p_cats['p_cat_title'];
echo "<option value='$p_cat_id'>$p_cat_title</option>";
}
?>
</select>
</div>
</div> <!---form-group-->
<div class="form-group">
<label class="col-md-3 control-label" >Category</label>
<div class="col-md-6">
<select name="cat" class="form-control" required>
<option>Select Category</option>
<?php
$get_cat="select * from categories";
$run_cat=mysqli_query($conn,$get_cat);
while($row_cat=mysqli_fetch_array($run_cat)){
$cat_id=$row_cat['cat_id'];
$cat_title=$row_cat['cat_title'];
echo "<option value='$cat_id'>$cat_title</option>";
}
?>
</select>
</div>
</div> <!---form-group-->
<div class="form-group">
<label class="col-md-3 control-label" >Product Image 1</label>
<div class="col-md-6">
<input type="file" name="product_img1" class="form-control" required>
</div>
</div> <!---form-group-->
<div class="form-group">
<label class="col-md-3 control-label" >Product Image 2</label>
<div class="col-md-6">
<input type="file" name="product_img2" class="form-control" required>
</div>
</div> <!---form-group-->
<div class="form-group">
<label class="col-md-3 control-label" >Product Image 3</label>
<div class="col-md-6">
<input type="file" name="product_img3" class="form-control" required>
</div>
</div> <!---form-group-->
<div class="form-group">
<label class="col-md-3 control-label" >Product Price</label>
<div class="col-md-6">
<input type="text" name="product_price" class="form-control" required>
</div>
</div> <!---form-group-->
<div class="form-group">
<label class="col-md-3 control-label" >Product Keywords</label>
<div class="col-md-6">
<input type="text" name="product_keywords" class="form-control" required>
</div>
</div> <!---form-group-->
<div class="form-group">
<label class="col-md-3 control-label" >Product Description</label>
<div class="col-md-6">
<textarea name="product_desc" class="form-control" rows="6" cols="19" style="max-width:100%; max-height:100%;"></textarea>
</div>
</div> <!---form-group-->
<div class="form-group">
<label class="col-md-3 control-label" ></label>
<div class="col-md-6">
<input type="submit" name="submit" value="Insert Product" class="btn btn-primary form-control">
</div>
</div> <!---form-group-->
</form> <!--form-horizontal ends--->
</div> <!--panel-body ends-->
</div> <!---panel panel-default ends-->
</div> <!--col-lg-12 ends--->
</div> <!--row 2 ends-->
</body>
</html>
<?php
if(isset($_POST['submit'])){
$product_title=$_POST['product_title'];
$product_cat=$_POST['product_cat'];
$cat=$_POST['cat'];
$product_price=$_POST['product_price'];
$product_desc=$_POST['product_desc'];
$product_keyword=$_POST['product_keywords'];
$product_img1=$_FILES['product_img1']['name'];
$product_img2=$_FILES['product_img2']['name'];
$product_img3=$_FILES['product_img3']['name'];
$temp_name1=$_FILES['product_img1']['tmp_name'];
$temp_name2=$_FILES['product_img2']['tmp_name'];
$temp_name3=$_FILES['product_img3']['tmp_name'];
move_uploaded_file($temp_name1,"product_images/$product_img1");
move_uploaded_file($temp_name2,"product_images/$product_img2");
move_uploaded_file($temp_name3,"product_images/$product_img3");
$insert_product="insert into products(p_cat_id,cat_id,date,product_title,product_img1,product_img2,product_img3,product_price,product_desc,product_keywords) values( '$product_cat','$cat',NOW(),'$product_title','$product_img1','$product_img2','$product_img3','$product_price','$product_desc','$product_keyword')";
$run_product=mysqli_query($conn,$insert_product);
if($run_product){
echo "<script>alert('Product has been inserted successfully')</script>";
echo "<script>window.open('insert.php','self')</script>";
}
}
?>
Even after defining variable i get error Undefined index: product_price in C:\xampp\htdocs\RangProject\admin_area\insert_products.php on line 183
this is my line 183
$product_price=$_POST['product_price'];
can someone tell me what my mistake is?? Php is quite confusing and case sensitive.
check if your post is not empty before assigning.
$product_price = isset($_POST['product_price']) ? $_POST['product_price'] : '';
fixed my error. Just added this after taking some advice from all my replies i got. Thanks everyone.
$product_title=mysqli_real_escape_string($conn,$_POST['product_title']);
$product_cat=mysqli_real_escape_string($conn,$_POST['product_cat']);
$cat=mysqli_real_escape_string($conn,$_POST['cat']);
$product_price =mysqli_real_escape_string($conn,$_POST['product_price']);
$product_desc=mysqli_real_escape_string($conn,$_POST['product_desc']);
$product_keyword=mysqli_real_escape_string($conn,$_POST['product_keywords']);
This happens because "product_price" does not exist on $_POST.
To avoid this error, check if it exists first using isset():
$product_price = isset($_POST['product_price']) ? $_POST['product_price'] : '';

Display Flash Message on button Click in Codeigniter using PHP

I have created a controller method that allows the user to change his/her login credentials(Username, password). On clicking the Save changes button, I would like to display a flash message indicating the status of the change process.
But I'm running into a trivial issue where the flash message is displayed only when I refresh the url and not as soon as the Save changes button is clicked.
View
<div id="login">
<section class="clearfix">
<form class="form-horizontal" method = "post" action="<?php echo site_url('studentDashboardController/saveUserLoginDetails');?>">
<fieldset>
<div style="width:1363px;margin-left:10px">
<?php echo $this->session->flashdata('msgLogin'); ?>
</div>
<!-- Form Name -->
<legend>Edit Login Credentials</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="username">Username</label>
<div class="col-md-6">
<input id="username" name="username" type="text" placeholder="current" class="form-control input-md">
<span class="help-block">Enter a new Username (Leave Blank to only change the Password)</span>
</div>
</div>
<!-- Password input-->
<div class="form-group">
<label class="col-md-4 control-label" for="password">Password</label>
<div class="col-md-6">
<input id="password" name="password" type="password" placeholder="New password" class="form-control input-md">
</div>
</div>
<!-- Password input-->
<div class="form-group">
<label class="col-md-4 control-label" for="confirmPassword">Confirm Password</label>
<div class="col-md-6">
<input id="confirmPassword" name="confirmPassword" type="password" placeholder="Retype password" class="form-control input-md" required="">
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-4">
<a href="<?php echo base_url() ?>index.php/studentDashboardController/saveUserLoginDetails">
<button id="submit" name="submit" type="submit" class="btn btn-primary">Save Changes</button>
</a>
</div>
</div>
</fieldset>
</form>
</section>
</div>
studentDashboardController - saveUserLoginDetails()
public function saveUserLoginDetails()
{
...
$msg = $this->studentprofileModel->saveUserLoginDetails($user,$this->input->post());
$msgText = $msg['msg'];
$msgType = $msg['type'];
//Loading View
$data1['details'] = $this->studentprofileModel->getUserDetails($username);
if($msgType == "danger")
{
$this->session->set_flashdata('msgLogin', '<div class="alert alert-danger text-center">Failed</div>');
echo '<script type="text/javascript">alert("'.$msgText.'")</script>';
}
else
{
$this->session->set_flashdata('msgLogin', '<div class="alert alert-success text-center">Success</div>');
}
redirect(base_url('index.php/studentDashboardController/editProfile',$data1));
}
studentprofileModel - saveUserLoginDetails($user,$this->input->post())
public function saveUserLoginDetails($uid,$post)
{
$username = $post['username'];
$password = $post['password'];
$confirmPassword = $post['confirmPassword'];
if ($username == null || $username == "") {
if ($password == $confirmPassword) {
$data = array(
'password' => $password,
);
$this->db->where("uid", $uid);
$this->db->update("sysuser", $data);
$msg = "Password has been successfully changed";
$type = "success";
$msgData = array(
'msg' => $msg,
'type' => $type
);
return $msgData;
}
}
else
{
if($password == $confirmPassword)
{
$data = array(
'password' => $password,
'username' => $username
);
$this->db->where("uid", $uid);
$this->db->update("sysuser", $data);
$this->load->library('session');
$sess_array = array(
'username' => ''
);
$this->session->unset_userdata('logged_in', $sess_array);
$session_data = array(
'username' => $username
);
// Add user data in session
$this->session->set_userdata('logged_in', $session_data);
$msg = "Login Data have been successfully updated";
$type = "success";
$msgData = array(
'msg' => $msg,
'type' => $type
);
return $msgData;
}
else
{
$msg = "Password doesn't match";
$type = "danger";
$msgData = array(
'msg' => $msg,
'type' => $type
);
return $msgData;
}
}
}
This login details form that needs to be changed is actually a single tab out of 4 different tabs that are within a single view itself.(Full page code is provided in the code snippet for reference). Could this be an issue in not displaying the status message via the flash as soon as the button is clicked?
Also I've done the check as if the password and the confirm password inputs are matching. But, it doesn't seem to execute it properly. Instead I'm always getting a success message.
Any suggestions in this regard will be appreciated.
Code snippet of Full View Code
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Student Dashboard View</title>
<meta name="description" content="">
...
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="<?php echo base_url("assets/js/customScripts/taro/bootstrap-datepicker.js")?>"></script>
<script>window.jQuery || document.write('<script src="<?php echo base_url("assets/js/customScripts/taro/vendor/jquery-1.10.1.min.js")?>"><\/script>')</script>
<!--<script src="--><?php //echo base_url("assets/js/customScripts/taro/vendor/jquery.hashchange.min.js")?><!--"></script>-->
<script src="<?php echo base_url("assets/js/customScripts/taro/vendor/jquery.easytabs.min.js")?>"></script>
<script src="<?php echo base_url("assets/js/customScripts/taro/main.js")?>"></script>
<style>
.entry:not(:first-of-type)
{
margin-top: 10px;
}
.glyphicon
{
font-size: 21px;
}
.centered {
position: fixed;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<script>window.html5 || document.write('<script src="<?php echo base_url("assets/js/customScripts/taro/vendor/html5shiv.js")?>"><\/script>')</script>
<![endif]-->
<!--[if lt IE 7]>
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please upgrade your browser or activate Google Chrome Frame to improve your experience.</p>
<![endif]-->
<div class="main-container">
<div class="">
<!-- Header Start -->
<header id="header">
<div id="logo">
<h2>
<?php foreach($details as $detail){?>
<?php echo $detail->name;?>
<?php }?>
</h2>
</div>
</header>
<!-- Header End -->
<!-- Main Tab Container -->
<div id="tab-container" class="tab-container">
<!-- Tab List -->
<ul class='etabs'>
<li class='tab' id="tab-about">
<i class="icon-user"></i><span> User Details</span>
</li>
<li class='tab'>
<i class="icon-file-text"></i><span> Edit Resume</span>
</li>
<li class='tab'>
<i class="fa fa-cloud"></i><span> Login Credentials</span>
</li>
<li class='tab'>
<i class="icon-envelope"></i><span> Edit Contact Details</span>
</li>
</ul>
<!-- End Tab List -->
<div id="tab-data-wrap">
<!-- About Tab Data -->
<div id="about">
<section class="clearfix">
<form class="form-horizontal" method ="post" role="form" enctype="multipart/form-data">
<?php echo form_open('studentDashboardController/saveUserDetails'); ?>
<?php echo $this->session->flashdata('msg'); ?>
<fieldset>
<!-- Form Name -->
<legend>User Details</legend>
...
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<a href="<?php echo base_url("/index.php/studentDashboardController/saveUserDetails"); ?>" >
<button id="submit" name="submit" type="submit" class="btn btn-primary">Save Changes</button>
</a>
</div>
</div>
</fieldset>
</form>
</section>
</div>
<!-- End About Tab Data -->
<!-- Resume Tab Data -->
<div id="resume">
<section class="clearfix">
<!-- <form class="form-horizontal" method = "post" action="--><?php //echo site_url('studentDashboardController/saveUserResumeDetails');?><!--">-->
<div class="g2">
<h3>
Work Experience
</h3>
...
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<a href="<?php echo base_url() ?>index.php/studentDashboardController/saveUserResumeDetails">
<button id="submit" name="submit" type="submit" class="btn btn-primary">Save Changes</button>
</a>
</div>
</div>
</div>
<!-- End Resume Tab Data -->
<!-- Login Tab Data -->
<div id="login">
<section class="clearfix">
<form class="form-horizontal" method = "post" action="<?php echo site_url('studentDashboardController/saveUserLoginDetails');?>">
<fieldset>
<div style="width:1363px;margin-left:10px">
<?php echo $this->session->flashdata('msgLogin'); ?>
</div>
<!-- Form Name -->
<legend>Edit Login Credentials</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="username">Username</label>
<div class="col-md-6">
<input id="username" name="username" type="text" placeholder="current" class="form-control input-md">
<span class="help-block">Enter a new Username (Leave Blank to only change the Password)</span>
</div>
</div>
<!-- Password input-->
<div class="form-group">
<label class="col-md-4 control-label" for="password">Password</label>
<div class="col-md-6">
<input id="password" name="password" type="password" placeholder="New password" class="form-control input-md">
</div>
</div>
<!-- Password input-->
<div class="form-group">
<label class="col-md-4 control-label" for="confirmPassword">Confirm Password</label>
<div class="col-md-6">
<input id="confirmPassword" name="confirmPassword" type="password" placeholder="Retype password" class="form-control input-md" required="">
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-4">
<a href="<?php echo base_url() ?>index.php/studentDashboardController/saveUserLoginDetails">
<button id="submit" name="submit" type="submit" class="btn btn-primary">Save Changes</button>
</a>
</div>
</div>
</fieldset>
</form>
</section>
</div>
<!-- End LoginData -->
<!-- Contact Tab Data -->
<div id="contact">
...

Need help in Redirecting to another page

<html>
<head>
<title>
Installation
</title>
<script src="public/js/jquery-1.9.0.min.js"></script>
<script src="public/js/bootstrap3/js/bootstrap.js"></script>
<link media="all" type="text/css" rel="stylesheet" href="public/js/bootstrap3/css/bootstrap.min.css">
<link media="all" type="text/css" rel="stylesheet" href="public/js/bootstrap3/css/font-awesome.min.css">
<link media="all" type="text/css" rel="stylesheet" href="public/js/bootstrap3/css/main.css">
</head>
<body>
<div
class="navbar navbar-default navbar-fixed-top">
<div
class="container">
<div
class="navbar-header">
<button
type="button"
class="navbar-toggle"
data-toggle="collapse"
data-target=".navbar-collapse">
<span
class="icon-bar"></span>
<span
class="icon-bar"></span>
<span
class="icon-bar"></span>
</button>
<a
class="navbar-brand"
href="<?php // echo base_url()?>">Kingpabel
ATN</a>
</div>
<div
class="navbar-collapse collapse">
</div>
<!--/.nav-collapse -->
</div>
</div>
<div
class="rc">
<div
class="container main">
<div
class="col-md-3">
</div>
<div
class="col-md-6">
<form
method="post"
accept-charset="utf-8"
role="form"
class="form- signin form-horizontal">
<h2
class="form-signin-heading">Installation</h2>
<div
style="margin-top: 20px;">
<label
for="host_name"
class="col-sm-3 control-label"
style="color: black">
Host
Name
</label>
<div
class="input text database_host col-md-9">
<input
type="text"
name="host_name"
class="form-control"
placeholder="Database Host Name"
autofocus=""
required="required"
id="host_name">
</div>
</div>
<div>
<label
for="database_name"
class="col-sm-3 control-label"
style="color: black">
Database
Name
</label>
<div
class="input text database_name col-md-9">
<input
type="text"
name="database_name"
class="form-control"
placeholder="Database Name"
autofocus=""
required="required"
id="database_name">
</div>
</div>
<div>
<label
for="user_name"
class="col-sm-3 control-label"
style="color: black">
User
Name
</label>
<div
class="input text user_name col-md-9">
<input
type="text"
name="user_name"
class="form-control"
placeholder="Database User Name"
autofocus=""
required="required"
id="user_name">
</div>
</div>
<div>
<label
for="password"
class="col-sm-3 control-label"
style="color: black">
Password
</label>
<div
class="input text password col-md-9">
<input
type="password"
name="password"
class="form-control"
placeholder="Database Password"
autofocus=""
id="password">
</div>
</div>
<div>
<label
for="project_url"
class="col-sm-3 control-label"
style="color: black">
Project
Url
</label>
<div
class="input text project_url col-md-8 input-group input-group-sm">
<input
style=" margin-left: 13px;"
type="text"
name="project_url"
class="form-control"
placeholder="Project URL"
autofocus=""
id="project_url"
aria-describedby="basic-addon2">
<span
class="input-group-btn">
<button
class="btn btn-default"
type="button"
style="margin-top: -15px;">/public</button>
</span>
</div>
</div>
<button
type="submit"
class="btn btn-lg btn-login btn-block">Install</button>
</form>
</div>
</div>
</div>
<section
id="bottom"
style="margin-top: 50px">
</section>
<footer
id="footer">
<div
class="container">
<div
class="footer">
<div
class="row">
<div
class="col-md-12">
</div>
</div>
</div>
</div>
</footer>
</body>
</html>
<?php
if ($_POST) {
$envFile = file_get_contents('.env');
if (isset($_POST['host_name']) && $_POST['host_name'])
$envFile = str_replace('DB_HOST=localhost', "DB_HOST= {$_POST['host_name']}", $envFile);
if (isset($_POST['database_name']) && $_POST['database_name'])
$envFile = str_replace('DB_DATABASE=homestead', "DB_DATABASE={$_POST['database_name']}", $envFile);
if (isset($_POST['user_name']) && $_POST['user_name'])
$envFile = str_replace('DB_USERNAME=homestead', "DB_USERNAME={$_POST['user_name']}", $envFile);
if (isset($_POST['password']) && $_POST['password'])
$envFile = str_replace('DB_PASSWORD=secret', "DB_PASSWORD={$_POST['password']}", $envFile);
file_put_contents('.env', $envFile);
if (isset($_POST['project_url']) && $_POST['project_url']) {
file_get_contents('index.php');
file_put_contents('index.php', "<?php
header('Location: {$_POST['project_url']}/public');
");
}
}`
In above code, after submitting the form, the idex.php is rewriiten and changes. And i wanted to redirect to another page after submitting the form. But it is not being redirected.
Not sure what all your block of codes are for but perhaps try this..
On your
if (isset($_POST['project_url']) && $_POST['project_url']) {
file_get_contents('index.php');
file_put_contents('index.php', "<?php
header('Location: {$_POST['project_url']}/public');
");
}
change to
if (isset($_POST['project_url']) && $_POST['project_url']) {
$url = $_POST['project_url'] + '/public';
header('Location: $url');
}

Registration form bringing back login credentials

I have made a user registration form in PHP and when I access the page the username part of the form is already filled in as my own log in credentials and the same with the password. The other parts of the form are filled in as an undefined index also.
Below shows the form and PHP code for my registration form. The form itself actually works and populates to my database.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>Ballymena Sports</title>
<!-- Bootstrap core CSS -->
<link href="bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="home2.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Ballymena Sports</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li>Log out</li>
</ul>
</div>
</nav>
<?php
include"config.php";
if(isset($_POST["submit"])){
$username=$_POST['username'];
$password=$_POST['password'];
$forename=$_POST['forename'];
$surname=$_POST['surname'];
$email=$_POST['email'];
$telephone=$_POST['telephone'];
$address1=$_POST['address1'];
$town=$_POST['town'];
$postcode=$_POST['postcode'];
$q = $db->prepare("SELECT * FROM user WHERE username = ?");
$query = $q-> execute(array($username));
$count = $q->rowCount();
if($count == 0) {
$query = $db->prepare("INSERT INTO user SET username = ?, password = ?, forename = ?, surname = ?, email = ?, telephone = ?, address1 = ?, town=?, postcode=? ");
$query = $query->execute(array($username,$password,$forename,$surname,$email,$telephone,$address1,$town,$postcode));
if($query){
echo "User successfully registered";
header("Location:home2_template.html");
return;
} else {
echo "Fail";
}
} else {
echo "User already exists";
}
}
?>
<!-- Main part of homepage -->
<div class="jumbotron">
<div class="container">
<div id="registerBody">
<h2>Register your account</h2>
<p>All fields within the registration form must be filled in</p>
</div>
<div class = "register">
<form method="POST" class="form-horizontal" action="">
<div class="form-group">
<label for="username" class="col-sm-2 control-label">Username:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="username" name="username" value="<?php echo $username ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" name="password" value="<?php echo $password ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="forename" class="col-sm-2 control-label">Forename:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="forename" name="forename" value="<?php echo $forename ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="surname" class="col-sm-2 control-label">Surname:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="surname" name="surname" value="<?php echo $surname ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email:</label>
<div class="col-sm-10">
<input type="email" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" class="form-control" id="email" name="email" placeholder="Match email format of 'email#provider.com'" value="<?php echo $email ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="telephone" class="col-sm-2 control-label">Telephone:</label>
<div class="col-sm-10">
<input type="text" pattern="[0-9]{11}" class="form-control" id="telephone" name="telephone" placeholder="Match telephone format of 11 digits" value="<?php echo $telephone ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="address1" class="col-sm-2 control-label">Address:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="address1" name="address1" value="<?php echo $address1 ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="town" class="col-sm-2 control-label">Town:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="town" name="town" value="<?php echo $town ?>"required="required">
</div>
</div>
<div class="form-group">
<label for="postcode" class="col-sm-2 control-label">Postcode:</label>
<div class="col-sm-10">
<input type="text" pattern ="[A-Za-z]{1,2}[0-9Rr][0-9A-Za-z]? [0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}" class="form-control" id="postcode" name="postcode" placeholder="Match postcode format of 'XX00 0XX' "value="<?php echo $postcode ?>"required="required">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input id="button" name="submit" type="submit" value="Register" class="btn btn-primary">
</div>
</div> <!-- registration button -->
</form>
</div> <!-- registration end -->
</div> <!-- container end -->
</div> <!-- jumbo end -->
<br>
<!--<div class="form-group">
<div class="col-sm-10">
<input id="submit" name="reg" type="submit" value="Register" class="btn btn-primary">
</div>
</div> -->
<!-- end of reg -->
<div class="container">
<br>
<footer>
<p>© Ballymena Sports 2014</p>
</footer>
</div> <!-- /container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="../../dist/js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>
Cheers
Stuart
Initialize all the variables with an empty string. You problem will be
resolved.
<?php
$username="";
$password="";
$forename="";
$surname="";
$email="";
$telephone="";
$address1="";
$town="";
$postcode="";
if(isset($_POST["submit"])){
$username=$_POST['username'];
$password=$_POST['password'];
$forename=$_POST['forename'];
$surname=$_POST['surname'];
$email=$_POST['email'];
$telephone=$_POST['telephone'];
............
..........
......
..}

Categories