i have a problem with a form in codeigniter. i have tried different things but i can not find a solution.
this is the form:
<form method="post" action="<?php echo base_url(); ?>cms/cms/newUser">
<div class="row">
<div class="col-md-3">
Naam:
</div>
<div class="col-md-9">
<input type="text" id="naam" name="username" />
</div>
</div>
<div class="row">
<div class="col-md-3">
E-mail:
</div>
<div class="col-md-9">
<input type="email" name="mail" id="email" />
</div>
</div>
<div class="row">
<div class="col-md-3">
Passwoord:
</div>
<div class="col-md-9">
<input type="password" name="pass" id="password" />
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<input type="submit" name="aanmaken" class="btn btn-block btn-info" value="aanmaken" />
</div>
</div>
in my controller i process the data coming from the form.
the controller is located at controllers/cms/cms.php
class Cms extends MY_Controller
{
public function newUser()
{
if(isset($_POST))
{
echo "verwerk deze data <br>";
if(isset($_POST['username']))
{
echo 'naam is set!';
}
else {
echo 'naam is not set!';
}
$naam = $_POST['username'];
$mail = $_POST['email'];
$pass = $_POST['password'];
echo "naam: " . $naam . "<br>";
echo "mail: " . $mail . "<br>";
echo "pass: " . $pass . "<br>";
$time = new DateTime();
$time->setTimezone(new DateTimeZone ('Europe/Brussels'));
$tijd = $time->format('Y-m-d H:i:s');
include_once('application/libraries/PasswordHash.php');
$hasher = new PasswordHash(12,false);
$passwoord = $hasher->HashPassword($pass);
$gebruiker = new Gebruiker();
$gebruiker->naam = $naam;
$gebruiker->email = $mail;
$gebruiker->paswoord = $passwoord;
$gebruiker->laatsteLogin = $tijd;
$gebruiker->active = 0;
echo "coded pas" . $passwoord . "<br>";
}
else
{
echo "no post";
}
}
when i check my website and fill in the form i get 3 php error that the values that i put in the variable from the post does not exist. also the if(isset) function says that the variable is not set. i don't know why the form action doesn't work.
i even tried the codeigniter way but it gave the same result.
$name = $this->input->post('username');
thanks for the help.
HTML:
enter code here
<form method="post" action="<?php echo site_url(); ?>/cms/cms/newUser">
<div class="row">
<div class="col-md-3">
Naam:
</div>
<div class="col-md-9">
<input type="text" id="naam" name="username" />
</div>
E-mail:
<div class="row">
<div class="col-md-3">
Passwoord:
</div>
<div class="col-md-9">
<input type="password" name="pass" id="password" />
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<input type="submit" name="aanmaken" class="btn btn-block btn-info" value="aanmaken" />
</div>
</div>
PHP:
class Cms extends MY_Controller
{
public function newUser()
{
if(isset($_POST))
{
echo "verwerk deze data <br>";
if($this->input->post('username'))
{
echo 'naam is set!';
}
else {
echo 'naam is not set!';
}
$naam = $this->input->post('username');
$mail = $this->input->post('mail');
$pass = $this->input->post('pass');
echo "naam: " . $naam . "<br>";
echo "mail: " . $mail . "<br>";
echo "pass: " . $pass . "<br>";
$time = new DateTime();
$time->setTimezone(new DateTimeZone ('Europe/Brussels'));
$tijd = $time->format('Y-m-d H:i:s');
include_once('application/libraries/PasswordHash.php');
$hasher = new PasswordHash(12,false);
$passwoord = $hasher->HashPassword($pass);
$gebruiker = new Gebruiker();
$gebruiker->naam = $naam;
$gebruiker->email = $mail;
$gebruiker->paswoord = $passwoord;
$gebruiker->laatsteLogin = $tijd;
$gebruiker->active = 0;
echo "coded pas" . $passwoord . "<br>";
}
else
{
echo "no post";
}
}
$this->input->post('') will check automatically not need of isset().
$this->input->post('') will take name not id
In the form tag
<form method="post" action="<?php echo base_url(); ?>cms/cms/newUser">
cms is the name of the controller and you have used it twice. Try with
<form method="post" action="<?php echo base_url(); ?>cms/newUser">
you need to pass only controllername/func_name and keep your controller file in controller dir
then try in CI way
<?php echo form_open("cms/newUser");?>
<?php echo form_close();?>
or
<form method="post" action="<?php echo base_url(); ?>cms/newUser">
Else you need to use routing for this and tell to pick this controller on that route
for this go to config/routes.php and add route like:-
$route['cms'] = "cms/cms";
try to load form helper and use this code
controller:
$this->viewContent['form_action'] = base_url()."cms/cms/newUser";
$this->load->view('your_view_page',$this->viewContent);
view:
echo form_open($form_action);
Related
Hi i have a registration form in my website.if the particular field in the db is null then the form should not be displayed to the user.Here if the payment_category_upload field is empty then the form should not displayed to the user otherwise the form should be displayed.
<?php include 'includes/db.php';
$sql = "SELECT * FROM users WHERE username = '$_SESSION[user]' AND user_password = '$_SESSION[password]' AND payment_category_upload!='' ";
$oppointArr =array();
$result = mysqli_query($conn,$sql);
if (mysqli_num_rows($result) > 0)
{
if(isset($_POST['submit_user'])|| isset($_POST['save_users']))
{
$formsubmitstatus = isset($_POST['submit_user'])?1:0;
if($_FILES["affidavits_upload"]["tmp_name"]!="")
{
$pname = rand(1000,10000)."-".str_replace("-"," ",$_FILES["affidavits_upload"]["name"]);
$affidavits_upload = $_FILES["affidavits_upload"]["tmp_name"];
$uploads_dir = '../admin/images/uploads';
move_uploaded_file($affidavits_upload, $uploads_dir.'/'.$pname);
}
else
{
$pname = $_POST['hid_affidavits_upload'];
}
$id= $_POST['users_id'];
$ins_sql = "UPDATE users set affidavits_upload='$pname',status='3',affidavitsupload_submit_status='$formsubmitstatus' WHERE users_id = $id";
$run_sql = mysqli_query($conn,$ins_sql);
$msg = 'Your Application successfully submitted. ';
$msgclass = 'bg-success';
}
else
{
$msg = 'Record Not Updated';
$msgclass = 'bg-danger';
}
}
else
{
echo "Please make the payment to enable Affidavits";
}
?>
FORM :
<form class="form-horizontal" action="affidavits.php" method="post" role="form" enctype="multipart/form-data" id="employeeeditform">
<?php if(isset($msg)) {?>
<div class="<?php echo $msgclass; ?>" id="mydiv" style="padding:5px;"><?php echo $msg; ?></div>
<?php } ?>
<input type='hidden' value='<?=$id;?>' name='users_id'>
<div class="form-group">
<label for="affidavits_upload" class="col-sm-4 control-label">Affidavits Upload</label>
<div class="col-sm-8">
<input type="hidden" value="<?php echo $oppointArr['affidavits_upload'];?>" name="hid_payment_category_upload">
<input type="file" name="affidavits_upload" id="affidavits_upload">
<?php if(!empty($oppointArr['affidavits_upload'])){?>
<div>
<?php echo $oppointArr['affidavits_upload'];?>
</div>
<?php }?>
<span class="text" style="color:red;">Please upload PDF Format Only</span>
</div>
</div>
<div class="col-sm-offset-2">
<?php if($oppointArr['affidavitsupload_submit_status'] == 0){ ?>
<button type="submit" class="btn btn-default" name="save_users" id="save_users">Save</button>
<button type="submit" class="btn btn-default" name="submit_user" id="subject">Submit Application</button>
<?php } ?>
</div>
</form>
//Add this Css class
.hiddenBlock {
display:none
}
<div class="<?php echo isset(test_field)?"":"hiddenBlock"; ?>">
<form>...<form>
</div>
You can do it like this for your field.
I have a simple test.php script:
<?php
$name = $_GET['name'];
$response = "Hi, ".$name."! How are you?";
echo $response;
?>
I know I can echo the html script in the php like: echo "<b>".$response."</b>"; but how can I just return the $response to my html page, and in the html page, it format the $response string?
I have a index.html page:
<title>Your name</title>
<center>Welcome to the coolest page ever</center>
<br>
"The PHP $response goes here"
<br>
<u>This is the footer</u>
As you can see, there is a html page with a template, and the response is required on one spot only (in the middle of the page). I don't want to echo the entire html script.
If I put the php script into the html, then my php script is exposed...
Example link: www.domain.com/test.php?name=John
save this page with whatever.php.
<html>
<title>Your name</title>
<body>
<center>Welcome to the coolest page ever</center>
<br>
<?php if(isset($_GET['name'])) { $response = "Hi, ".$_GET['name']."! How are you?";
echo $response;}
?>
<br>
<u>This is the footer</u>
</body></html>
You can put the html in your php page test.php
Here is an example of how I have used echo inside of html to display a value
First do your php to connect to your database and execute whatever query you want
I use includes as well to make dealing with the header/Navigation and footer simple for every page. Instead of having to have that code inside of your test.php page you can create separate pages and just include them. That way if you want to edit your header or footer you can do it in one place and it will change for all of your pages.
As you will see in my html you can then put the echo value into the html like so value="
//php code begins here
<?php
include_once 'header.php';
$sql = "SELECT * FROM `person` WHERE person_id = " .$user_id." ";
$conn = mysqli_connect( $dbServername , $dbUsername , $dbPassword , $dbName );
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "user_id: " . $row["person_id"]. " - person_id: " . $row["person_first"]. " " . $row["person_last"]. "<br>";
$person_id = $row["person_id"];
$person_first = $row["person_first"];
}
} else {
echo $sql;
}
$conn->close();
?>
//first php section ends here
//html section starts here
<div class="form-group">
<label class="col-md-12">First Name</label>
<div class="col-md-12">
<input type="text" required="" name="person_first" value="<?php echo $person_first;?>" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label class="col-md-12">Last Name</label>
<div class="col-md-12">
<input type="text" required="" name="person_last" value="<?php echo $person_last;?>" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label for="example-email" class="col-md-12">Mobile</label>
<div class="col-md-12">
<input type="text" required="" name="person_mobile" value="<?php echo $person_mobile;?>" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label for="example-email" class="col-md-12">Address</label>
<div class="col-md-12">
<input type="address" required="" name="person_address" value="<?php echo $person_address;?>" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label class="col-md-12">Email</label>
<div class="col-md-12">
<input type="text" required="" name="person_email" value="<?php echo $person_email;?>" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label class="col-md-12">SSN</label>
<div class="col-md-12">
<input type="text" required="" name="SSN" value="<?php echo $SSN;?>" class="form-control form-control-line"> </div>
</div>
//html ends here
//php footer code starts here
<?php
include_once 'footer.php';
?>
// php footer ends here
Begin code for new page header.php
<?php
include 'includes/dbh.php';
session_start();
if (!isset($_SESSION["u_id"]))
{
header("location: login.php");
}
$user_id = $_SESSION['u_id'];
echo "UserID is '".$user_id."'";
$sql = "SELECT * FROM users WHERE user_id='$user_id'";
// $sql = "SELECT SINGLE `user_id` FROM `users` WHERE user_id = '".$user_id."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "user_id: " . $row["user_id"]. " - Name: " . $row["user_first"]. " " . $row["user_last"]. "<br>";
$displayname = $row["user_first"]. " " . $row["user_last"];
$email = $row["user_email"];
$uid = $row["user_uid"];
}
} else {
echo "0 results";
}
$sql = "SELECT * FROM players WHERE player_id=$user_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "player_id: " . $row["player_id"]. " - player_id: " . $row["player_first"]. " " . $row["player_last"]. "<br>";
$player_id = $row["player_id"];
$player_first = $row["player_first"];
$player_last = $row["player_last"];
$player_mobile = $row["mobile"];
$player_address = $row["player_address"];
$player_city = $row["city"];
$player_state = $row["state"];
$player_zip = $row["zip"];
$player_dob = $row["dob"];
$player_gender = $row["gender"];
}
}
$conn->close();
?>
End header.php
I have a seperate navigator.php included on top of every page that I have for public.And it has a login form.If users have an account,they can login and be sent to the current page that they are at.
I pass the current URL adress to a hidden input as it's value.And post it to giris.php(login).Then redirecting the user with Header.
But when it comes to register.php(when no sessions were set);Im trying to login there and it still sends me back to the register.php.But SESSION is being set.Thats where I need an exception and want to send user to the index.php through register.php.
navigator.php
<div id="top">
<ul class="topnav" id="myTopnav">
<li>Anasayfa</li>
<li>İletişim</li>
<li>Hakkımızda</li>
<?php
if (isset($_SESSION["giris"]))
{
echo '<li>Panel</li>
<li>Çıkış Yap</li>';
}
else
{
$url= $_SERVER["REQUEST_URI"];
echo '<li>Kayıt Ol</li>
<li id="log">
<form method="post" action="giris.php"><div id="login">
<input type="hidden" name="location" value="'.$url.'">
<input type="text" name="username" placeholder="Kullanıcı Adı" class="loginField" required>
<input type="password" name="password" placeholder="Şifre" class="loginField" required>
<input type="submit" name="login" value="Giriş" id="logBut">
</form>
</li>';
}
?>
<li class="icon">
☰</li>
</ul>
</div>
<div id="banner">
<div id="title">
<h1>Topluluk Bloğu</h1>
<br/>
<h5>Community Blog</h5>
<br/>
<?php if(isset($_SESSION["giris"])){echo '<p id="username">Hoşgeldin '.$_SESSION["kullanici"].'</p>'; }?>
</div>
</div>
giris.php
<?php
session_start();
ob_start();
include 'func/constr.php';
if(isset($_POST["login"]))
{
$kullanici = $_POST['username'];
$password = $_POST['password'];
$URL = $_POST["location"];
$query = mysqli_query($connect,"SELECT * FROM kullanicilar where kullanici_adi='$kullanici' and sifre='$password'");
$count = mysqli_num_rows($query);
if ($count == 1)
{
$_SESSION["giris"] = true;
$_SESSION["kullanici"] = $kullanici;
$_SESSION["sifre"] = $password;
header("Location:$URL");
}
else
{
$invalid = "Kullanıcı adı ya da şifre yanlış";
$_SESSION["invalid"] = $invalid;
header("Location:index.php");
}
}
ob_end_flush();
?>
try this but not tested, if your other code is ok and redirect problem then
header("Location:$URL");
to
header('Location: ' . $URL);
Actually while uploading a file I am creating folder with timestamp and also saving folder name along with filename to the database this is working fine. While editing I have input type hidden in this I am posting value if user do not change files and also if folder exits already it should not create folder and If user change files that time it should create a new folder.Only it is going to if condition. not else part.Please help me someone.
Below is PHP code,
if(isset($_FILES['event_image']['name'])&& $_FILES['event_image']['name']!="")
{
$folder_name =$_FILES['event_image']['name'];
echo $folder_name;
}
else
{
$folder_name =$_POST['image_exists'];
echo $folder_name;
}
//print_r($pathToUpload);die;
$old = umask(0);
if (!file_exists($folder_name))
{
//echo "djfgd";die;
$folderName = $_POST['event_name'].time();
$pathToUpload = 'images/events/' . $folderName;
$create = mkdir($pathToUpload, 0777,true);
//chmod($pathToUpload,0755,true);
if ( ! $create )
return;
}
else{
//echo "dqqqqqq";die;
//$folderName = $_POST['event_name'].time();
//$pathToUpload = 'images/events/' . $folderName;
//$create = mkdir($pathToUpload, 0777,true);
echo "already exits";die;
}
umask($old);
HTML code:
<div class="col-md-6">
<div class="form-group">
<label for="event_image">Event Banner</label>
<input type="file" value="<?php echo $event_image; ?>"
class="form-control" id="eventimage" name="event_image">
<input type="hidden" name="image_exists" value="<?php echo $event_image;?>"
class="form-control" id="eventimage" placeholder="Enter Image Text" aria-describedby="fileHelp">
<div><?php echo $event_image;?></div>
</div>
</div>
Alright I have simulated your case on my machine using Codeigniter and simple PHP is_dir function.
Here is my test view
Created with Following Markup
<div class="container" style="margin-top:100px; ">
<div class="col-md-6" style="border: 1px dotted red; ">
<?php if(isset($errors)){?>
<div class="alert alert-danger">
<?php print_r($errors);?>
</div>
<?php }?>
<?php if(isset($success)){?>
<div class="alert alert-success">
<?php print_r($success);?>
</div>
<?php }?>
<form action="" method="post" id="form1">
<div class="form-group">
<label > Directory Name</label>
<input type="text" name="name" class="form-control" >
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger" >Create</button>
</div>
</form>
</div>
</div>
My Controller
public function index()
{
$data['title']='Multiple submit';
if($_POST)
{
$path='uploads/'.$_POST['name'];
if (!is_dir($path)) {
mkdir($path);
$data['success']='Directory Created Successfully';
$this->load->view('form',$data);
}
else
{
$data['errors']='Directory Already Exist';
$this->load->view('form',$data);
}
}
else
{
$this->load->view('form',$data);
}
}
I create a directory, which is success.
I try it again, get an error
The Directory created in my path is
I hope you can modify this code as per your requirements.
As i am newbie to PHP kindly pardon me if i looks silly ,
I created a form in php , while i do the update part of the form the update reflects in db whereas in the form it still shows the same old value . i tried refresh and force refresh but nothing changes .
Whereas if i logout and login again , the form shows the updated value .
I tried using die(); after mysql_close($link); but it logs out the session and needs to re-login .
Kindly help me on viewing the changes while i am still inside the login .
My code is as follows :
<?php
if(isset($_POST['update'])) {
$name_a = $_POST['name'];
$email_a = $_POST['email'];
$pass_a = $_POST['password'];
$sql = "UPDATE admin SET a_name = '$name_a', a_email = '$email_a', password = '$pass_a' where aid='$update_id' ";
$retval = mysql_query($sql,$link);
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($link);
}else {
?>
<!-- Widget: user widget style 1 -->
<div class="box box-widget widget-user-2">
<!-- Add the bg color to the header using any of the bg-* classes -->
<div class="widget-user-header bg-yellow">
<div class="widget-user-image">
<?php echo '<img src="' . $img . '" class="img-circle" alt="User Image">'; ?>
</div>
<!-- /.widget-user-image -->
<h3 class="widget-user-username"><?php echo "$name"; ?></h3>
<h5 class="widget-user-desc"><?php echo "$role"; ?></h5>
</div>
<div class="box-footer no-padding">
<form role="form" method = "post" action = "<?php $_PHP_SELF ?>">
<div class="box-body">
<div class="form-group">
<label for="exampleInputName1">Name</label>
<input type="text" class="form-control" id="exampleInputName1" name="name" value="<?php echo "$name"; ?>">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" name="email" value="<?php echo "$email"; ?>">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" name="password" value="<?php echo "$password"; ?>">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="update" id="update" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
<!-- /.widget-user -->
<?php
}
?>
SOLUTION
1) use the updated value like $name_a instead of $name because $name_a contain updated value and $name contain old value
2) reload page after update and get new value from database on page load and store that value in $name , $email etc variable (if new data update successfully in database then only you get new value )
3) if You store your data in session or cookie then update session and cookie value also when you update in database
Try this:
<?php
$name = '';
$email = '';
$password = '';
$update_id = '';
//$img = '';
//$role = '';
//$link = null;
if(
isset($_POST['update']) &&
isset($_POST['id']) &&
isset($_POST['name']) &&
isset($_POST['email']) &&
isset($_POST['password'])
) {
$update_id = mysql_real_escape_string($_POST['id']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);
$sql = 'UPDATE admin SET a_name = \'' . $name . '\', a_email = \'' . $email . '\', password = \'' . $password . '\' WHERE aid = \'' . $update_id . '\'';
$result = #mysql_query($sql, $link);
if(!$result)
die('Could not update data: ' . mysql_error($link));
echo 'Updated data successfully', "\n";
}
elseif(isset($_GET['id'][0])) {
$update_id = mysql_real_escape_string($_GET['id']);
$sql = 'SELECT a_name,a_email,a_password FROM admin WHERE aid = \'' . $update_id . '\'';
$result = #mysql_query($sql, $link);
if($result) {
$result = mysql_fetch_row($result);
$name = $result[0];
$email = $result[1];
$password = $result[2];
}
else {
echo 'Could not find the id.' . "\n";
$update_id = '';
}
}
unset($result);
if(isset($update_id[0])) {
mysql_close($link);
?>
<!-- Widget: user widget style 1 -->
<div class="box box-widget widget-user-2">
<!-- Add the bg color to the header using any of the bg-* classes -->
<div class="widget-user-header bg-yellow">
<div class="widget-user-image">
<img src="<?php echo htmlspecialchars($img); ?>" class="img-circle" alt="User Image">
</div>
<!-- /.widget-user-image -->
<h3 class="widget-user-username"><?php echo htmlspecialchars($name); ?></h3>
<h5 class="widget-user-desc"><?php echo htmlspecialchars($role); ?></h5>
</div>
<div class="box-footer no-padding">
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($update_id); ?>">
<div class="box-body">
<div class="form-group">
<label for="exampleInputName1">Name</label>
<input type="text" class="form-control" id="exampleInputName1" name="name" value="<?php echo htmlspecialchars($name); ?>">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" name="email" value="<?php echo htmlspecialchars($email); ?>">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" name="password" value="<?php echo htmlspecialchars($password); ?>">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="update" id="update" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
<!-- /.widget-user -->
<?php }
else {
$sql = 'SELECT aid,a_name FROM admin';
$result = #mysql_query($sql, $link);
if($result) {
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '' . $row['a_name'] . '<br />' . "\n";
}
}
mysql_close($link);
}
?>
As #DivyeshSavaliya mentioned in the comment the issue is ,
I didn't Used Select query after update . Once done that the issue solved
The new working code is
<?php
if(isset($_POST['update'])) {
$name_a = $_POST['name'];
$email_a = $_POST['email'];
$pass_a = $_POST['password'];
$sql = "UPDATE admin SET a_name = '$name_a', a_email = '$email_a', password = '$pass_a' where aid='$update_id' ";
$retval = mysql_query($sql,$link);
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
}
$result = mysql_query("SELECT * FROM admin where aid='$update_id' ",$link);
while($row = mysql_fetch_array($result)){
$name = $row['a_name'];
$email = $row['a_email'];
$password = $row['password'];
}
mysql_close($link);
?>
Thanks to #DivyeshSavaliya