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">
...
Related
i've been trying to make a login system on my website with a signup button but nothing returns to the database aka its empty, here's the code
<?php
include 'dbh.php', 'connect.php';
$usernames=$_POST['usernames'];
$passwords=$_POST['passwords'];
$repassword=$_POST['repassword'];
$email=$_POST['email'];
$taken="false";
$database="database1";
//Main if Statement
if($usernames&&$passwords&&$email&&$repassword){
//connect to database
$con = mysqli_connect('localhost', $username, $password) or die("Unable to log into database");
#mysql_select_db($database1, $con) or die("Unable to connect");
mysql_query("INSERT INTO `users` VALUES('', '$usernames', '$passwords', '$email', '$repassword')") or die ("Strange error");
echo "Account created";
mysql_close($con);
// header("location: index.php");
} else {
echo"You need to have Username, Password and Email!";
}
?>
i have tried to signup for testing but nothing returns to the database?
here's the html script
<!DOCTYPE html>
<html lang="en">
<head>
<link href="animate.css" rel="stylesheet">
<title>Twist - Shortie Lovers!</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<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>
</head>
<body>
<div class="navbar navbar-inverse navbar-static-top">
<div class="container">
Twist
<button class="navbar-toggle" data-toggle="collapse" data-target=".navHeaderCollapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="collapse navbar-collapse navHeaderCollapse">
<ul class="nav navbar-nav navbar-right">
<li class="active">Home</li>
<li>New</li>
<li class="dropdown">
Hot <b class="caret"></b>
<ul class="dropdown-menu">
<li>Test</li>
</ul>
</li>
<li>Upload</li>
<li>Sign up</li>
<li>Login</li>
</ul>
</div>
</div>
</div>
<div class="container">
<div class="jumbotron text-center">
<h1>You can do it!</h1>
<p>How? simple... Make your own movie, get rated by other users and movie makers and WIN!</p>
<a class="btn btn-info">Read More...</a>
</div>
</div>
<div class="container text-center">
<h3>Browse popular Posts or Login</h3>
</div>
<div class="navbar navbar-inverse navbar-fixed-bottom">
<div class="container">
<p class="navbar-text pull-left">Copyright © 2016 Twist</p>
Contribute
</div>
</div>
<!--For signup-->
<div class="modal fade" id="signup" role="dialog" action="create.php" method="POST">
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal" >
<div class="modal-header">
<h4>Signup</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="signup-username" class="col-lg-2 control-label">Username:</label>
<div class="col-lg-10">
<input type="text" name="usernames" class="form-control" id="signup-username" placeholder="Username" required>
</div>
</div>
<div class="form-group">
<label for="signup-password" class="col-lg-2 control-label">Password:</label>
<div class="col-lg-10">
<input type="password" name="passwords" class="form-control" id="signup-password" placeholder="Password" required>
</div>
</div>
<div class="form-group">
<label for="signup-repassword" class="col-lg-2 control-label">Again:</label>
<div class="col-lg-10">
<input type="password" name="repassword" class="form-control" id="signup-repassword" placeholder="Retype Password" required>
<!--<script>
$('form').on('submit1',function(){
if($('#signup-password').val()!=$('#signup-repassword').val()){
alert('Password does not match!');
return false;
}
return true;
});
</script>-->
</div>
</div>
<div class="form-group">
<label for="signup-email" class="col-lg-2 control-label">Email:</label>
<div class="col-lg-10">
<input type="text" name="email" class="form-control" id="signup-email" placeholder="Email" required>
</div>
</div>
</div>
<!--<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
</div>
</div>-->
<div class="modal-footer">
<a class="btn btn-primary" data-dismiss="modal">Close</a>
<button class="btn btn-primary" type="submit1">Sign Up</button>
</div>
</form>
</div>
</div>
</div>
<!--Login -->
<div class="modal fade" id="login" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal" action="">
<div class="modal-header">
<h4>Login</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="login-username" class="col-lg-2 control-label">Username:</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="login-username" placeholder="Username" required>
</div>
</div>
<div class="form-group">
<label for="login-password" class="col-lg-2 control-label">Password:</label>
<div class="col-lg-10">
<input type="password" class="form-control" id="login-password" placeholder="Password" required>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
</div>
</div>
<div class="modal-footer">
<a class="btn btn-primary" data-dismiss="modal">Close</a>
<button class="btn btn-primary" type="submit">Log In</button>
</div>
</form>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Your query is probably not working because you override your username and password, so you probably can't login:
$username=$_POST['username'];
$password=$_POST['password'];
$repassword=$_POST['repassword'];
$email=$_POST['email'];
$taken="false";
$database="database1";
$password=""; //This one is already set!!!
$username="root"; //This one is already set!!!
EDIT:
Try adding something like this, to make sure your query is right:
$result = mysql_query('YOUR QUERY');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
I'm new to web development and I would like to know if it's safe to put the reCaptcha private key in the contact-us.php page where I use
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" to submit the form to the same page because of IE issues on submitting the form to other php page as sentmail.php or thankyou.php page where I could hide the reCaptcha private key?
Here is my code. If someone could help me understand where is the problem or how should I divide the code in 2 pages: contact.php and sendmail.php I will really appreciated.
<?php
//set validation error flag as false
$error = false;
//check if form is submitted
if (isset($_POST['submit']))
{
//Sanitize incoming data and store in variable
$name = trim(stripslashes(htmlspecialchars($_POST['InputName'])));
$fromemail = trim(stripslashes(htmlspecialchars($_POST['InputEmail'])));
$subject = trim(stripslashes(htmlspecialchars($_POST['InputSubject'])));
$message = trim(stripslashes(htmlspecialchars($_POST['InputMessage'])));
//Validate data and return success or error message
if (!preg_match("/^[a-zA-Z ]+$/",$name))
{
$error = true;
$name_error = "Please Enter Valid Name";
}
if(!filter_var($fromemail,FILTER_VALIDATE_EMAIL))
{
$error = true;
$fromemail_error = "Please Enter Valid Email Address";
}
if(empty($subject))
{
$error = true;
$subject_error = "Please Enter Your Subject";
}
if(empty($message))
{
$error = true;
$message_error = "Please Enter Your Message";
}
// grab recaptcha library
require_once "recaptchalib.php";
// your secret key
$privatekey = "----------secret key-----------";
//check reCaptcha
if ($_POST["g-recaptcha-response"]) {
$response_json = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".urlencode($privatekey)."&response=".urlencode($_POST["g-recaptcha-response"]));
$response = json_decode($response_json, true);
}
// if submitted check response
if ($_POST["g-recaptcha-response"]) {
$response = $reCaptcha->verifyResponse(
$_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]
);
}
// empty response
$response = null;
// check secret key
$reCaptcha = new ReCaptcha($privatekey);
if (!$error)
{
// If there are no errors, send the email
$toemail = "email#domain.com";
$subject = "Message from Contact Form: " . $subject;
$body = "Message from Contact Form: \n\n Name: $name \n From: $fromemail \n Subject: $subject\n Message: \n $message";
$headers = "From: $fromemail\n";
$headers .= "Reply-To: $fromemail";
if (mail ($toemail, $subject, $body, $headers))
$alertmsg = '<div class="alert alert-success text-center">Thank you for contacting us. We will be in touch with you very soon.</div>';
else
$alertmsg = '<div class="alert alert-danger text-center">Sorry there was an error sending your message. Please try again later.</div>';
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Company | Contact us </title>
<meta charset="UTF-8">
<!--[if IE]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery.min.js"></script>
<!-- Custom CSS files -->
<link href="css/style.css" rel='stylesheet' type='text/css' />
<!----webfonts--->
<!-- Recaptcha -->
<script type="text/javascript">
var onloadCallback = function() {
grecaptcha.render('html_element', {
'sitekey' : '----sitekey------'
});
};
</script>
</head>
<body>
<!-- container -->
<div class="container">
<!-- header -->
<header class="row">
<!-- logo -->
<div class="logo">
<img src="images/logo.png" title="Logo" alt="Logo" width="300" height="125" />
</div><!--- End logo -->
</header>
<!-- navbar -->
<div class="row">
<nav class="navbar navbar-default navbar-inverse navbar-fixed-top" role="navigation">
<div class="container-fluid">
<!-- Toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#collapse" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div><!-- /.navbar-header -->
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="collapse">
<ul class="nav navbar-nav navbar-right">
<li>Home </li>
<li>About us</li>
<li class="dropdown">
Services <span class="caret"></span>
<ul class="dropdown-menu">
<li>Services1</li>
<li>Services2</li>
</ul>
</li>
<li>application</li>
<li class="active">Contact us <span class="sr-only">(current)</span></li>
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div><!-- navbar -->
</div><!-- End container -->
<!----MAP ---->
<div class="welcome-note">
<div class="container">
<div class="map">
<iframe src="link"></iframe>
</div>
</div>
</div><!---- MAP ---->
<br/>
<!-- Contact with Map - START -->
<div class="container">
<div class="row">
<div class="contact-left col-md-4">
<div class="panel panel-default text-center">
<div class="panel-body text-center">
<address>
</address>
</div>
</div>
</div>
<!---- contact us form----->
<div class="col-md-7">
<div class="well well-md">
<div><span><h2>Contact us</h2></span>
</div><br/>
<form role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" id="contactForm" enctype="multipart/form-data" data-toggle="validator" data-disable="false">
<?php if (isset($alertmsg)) { echo $alertmsg; } ?>
<fieldset>
<div class="form-group">
<div class="col-xs-10 col-md-offset-1">
<span class="text-danger"><?php if (isset($name_error)) echo $name_error; ?></span><br/>
<label for="InputName">Name *</label>
<input type="text" class="form-control" name="InputName" id="InputName" placeholder="First & Last Name: Jane Doe" autocomplete="on" autofocus required value="<?php if($error) echo $name; ?>" />
</div>
</div>
<div class="form-group">
<div class="col-xs-10 col-md-offset-1">
<span class="text-danger"><?php if (isset($fromemail_error)) echo $fromemail_error; ?></span> <br/>
<label for="InputEmail">Email *</label>
<input type="email" class="form-control" name="InputEmail" id="InputEmail" placeholder="Email: example#domain.com" autocomplete="on" required value="<?php if($error) echo $fromemail; ?>" />
</div>
</div>
<div class="form-group">
<div class="col-xs-10 col-md-offset-1">
<span class="text-danger"><?php if (isset($subject_error)) echo $subject_error; ?></span> <br/>
<label for="InputSubject">Subject *</label>
<input type="text" class="form-control" name="InputSubject" id="InputSubject" placeholder="Subject" autocomplete="on" required value="<?php if($error) echo $subject; ?>" />
</div>
</div>
<div class="form-group">
<div class="col-xs-10 col-md-offset-1">
<span class="text-danger"><?php if (isset($message_error)) echo $message_error; ?></span><br/>
<label for="InputMessage">Message *</label>
<textarea class="form-control" rows="5" name="InputMessage" id="InputMessage" placeholder="Enter your massage for us here. We will get back to you within 2 business days." autocomplete="off" required><?php if($error) echo $message; ?></textarea>
</div>
</div>
<div class="form-group">
<div class="col-xs-10 col-md-offset-1">
<br/>
<div class="g-recaptcha" data-sitekey="sitekey"></div>
<script type="text/javascript">
var onloadCallback = function() {
alert("grecaptcha is ready!");
};
</script>
</div>
</div>
<!--<div class="form-group">
<div class="col-xs-10 col-md-offset-1">
<!-- input element hidden as dummy code for IE to can submit the form -->
<!--<input type="hidden" name="submit" id="submit" class="btn btn-primary" value="Send" />
</div>
<div class="form-group">
<div class="col-xs-10 col-md-offset-1">
<button type="submit" id="submit" name="submit" class="btn btn-primary" value="Send">Send message <span class="glyphicon glyphicon-send"></span></button>
</div>
</div>-->
</fieldset>
<div class="col-md-4 col-md-offset-1">
<input type="submit" id="submit" name="submit" class="btn btn-primary" value="Send message" >
</div>
</form>
</div>
</div>
</div>
<div class="clearfix"> </div>
</div>
<!-- End contact -->
<!-- javascript -->
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!-- Recaptcha -->
<script src="https://www.google.com/recaptcha/api.js"></script>
</body>
</html>
<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');
}
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
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'];
............
..........
......
..}