Re-populate upload file field in codeigniter? - php

I want to re-populate upload field in form in codeigniter here is my code
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php if(isset($upload_error) && !empty($upload_error)): ?>
<p><?php echo $upload_error ?></p>
<?php endif; ?>
<?php echo validation_errors() ?>
<?php echo form_open_multipart('') ?>
Enter title: <br>
<input type="text" name="title" value="<?php echo set_value('title') ?>" > <br><br>
Enter Content:<br>
<textarea cols="40" rows="5" name="content"><?php echo set_value('content') ?></textarea> <br><br>
<input type="file" name="userfile" size="20" value="<?php echo set_value('userfile') ?>"> <br><br>
<input type="submit" name="submit">
</form>
</body>
</html>
I tried set_value in userfile but it didn't work. Pls tell me how to do it

You cannot due to security reasons. The browser shouldn't have access to the user file system without premission.

Related

PHP sessions not working on multiple page form when going back to the first page

I don't get the PHP $_SESSION working when I try to echo the result on the first page. The error message states basically that the variables are undefined.
The path i want to use:
1st page = form 1
2nd page = form 2
Go back to 1st page = form 1 with all input filled from previous submit + all data from the 2 forms in a text.
Is that possible ?
Page 1 = index.php:
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<form action="overview.php" id="regForm" method="post">
<div class="tab">
<h2>1. Your info</h2>
<p><input type="text" placeholder="Prénom" name="fname" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
<p><input type="text" placeholder="Nom" name="name" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
<input type="submit" value="Valider">
</div>
</form>
<?php echo "Your name is :",$_SESSION['name'], "and your first-name is ", $_SESSION['fname'];?>
<?php echo "Your e-mail is :", $_SESSION['email'] ;?>
</body>
</html>
Page 2 = overview.php:
<?php session_start(); ?>
<?php
$_SESSION['fname'] = $_POST['fname'];
$_SESSION['name'] = $_POST['name'];
?>
<form action="index.php" id="regForm" method="post">
<h2>1. Tes informations personnelles</h2>
<p><input type="text" placeholder="e-mail" name="email"></p>
<input type="submit" value="Valider">
</form>
Back to Page 1 = index.php:
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<form action="overview.php" id="regForm" method="post">
<div class="tab">
<h2>1. Your info</h2>
<p><input type="text" placeholder="Prénom" name="fname" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
<p><input type="text" placeholder="Nom" name="name" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
<input type="submit" value="Valider">
</div>
</form>
<?php echo "Your name is :",$_SESSION['name'], "and your first-name is ", $_SESSION['fname'];?>
<?php echo "Your e-mail is :", $_SESSION['email'] ;?>
</body>
</html>
Do you guys see any issue that prevents the code to run ?
Just found out the "WHY".
I basically blocked cookies when I started the project which prevent $_sessions to work properly.
So make guys sure that cookies are enabled on your website. The kind of things that waste an hour of work ...

Passing hidden value to a php variable in input tag

I am new to coding and pardon me if it a silly thing I am missing. I have searched through the forum & did not find an answer that suits my need. I have 2 files: jobs.php & jobprocess.php
Jobs.php goes as
<?php session_start();
include('dbConnect.php');
$q1="abc";
$q2="pqr";
$q3="xyz";
$opportunity=29;
echo "Opportunity is". $opportunity;
?>
<html>
<head>
<div align="center">
<form method="post" method="post" action="jobprocess.php">
<input type="text" name="q1" placeholder="<?php echo $q1;?>"><br>
<input type="text" name="q2" placeholder="<?php echo $q2;?>"><br>
<input type="text" name="q3" placeholder="<?php echo $q3;?>"><br>
<input type="hidden" name="opportunity" value="<?php echo $opportunity;?>">
<ul class="actions">
<li><input type="submit" name="submit" value="I would like to join!! "></li>
</ul>
</form>
</div>
</head>
<body>
</body>
</html>
jobprocess.php goes with the code
<?php session_start();
include('dbConnect.php');
$opportunity = $_GET['opportunity'];
echo "opportunity is " . $opportunity;
?>
Unfortunately, the above code is not defining value="29" for opportunity on 2nd page. Thanks in advance
If you echo anything before the html tag it would effectively make the html invalid. Also, the head of the document MUST not have presentational html elements such as forms, divs etc
<?php
session_start();
include('dbConnect.php');
$q1="abc";
$q2="pqr";
$q3="xyz";
$opportunity=29;
?>
<html>
<head>
<title>must have a title</title>
</head>
<body>
<?php
echo "Opportunity is". $opportunity;
?>
<div align="center">
<form method="post" method="post" action="jobprocess.php">
<input type="text" name="q1" placeholder="<?php echo $q1;?>"><br>
<input type="text" name="q2" placeholder="<?php echo $q2;?>"><br>
<input type="text" name="q3" placeholder="<?php echo $q3;?>"><br>
<input type="hidden" name="opportunity" value="<?php echo $opportunity;?>">
<ul class="actions">
<li><input type="submit" name="submit" value="I would like to join!! "></li>
</ul>
</form>
</div>
</body>
</html>
And because the form is set to POST you should probably check and use the POSTed variable rather than a GET variable
<?php
session_start();
include('dbConnect.php');
$opportunity = $_POST['opportunity'];
echo "opportunity is " . $opportunity;
?>
Surprisingly my answer that suggested using session variable instead of hidden form field was deleted?! I guess session variables are illegal now?
The answer was chosen for the best answer even.

Unable to get jQuery validation to work

I am using php and CodeIgniter. I am a novice at both of these (career VB.Net and C# developer). However, trying to create a basic registration form, I'm having a hard time getting jQuery validation to work.
header.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?><!DOCTYPE html>
<html>
<head>
<?php if(isset($title)) {?>
<title>Discuss Cards - <?php echo $title ?></title>
<?php } else { ?>
<title>Discuss Cards</title>
<?php } ?>
<link rel="stylesheet" href="<?php echo base_url();?>styles/forum.css" type="text/css"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
<script src="<?php echo base_url();?>js/jquery.validate.min.js"></script>
</head>
<body>
<?php if(isset($title)) {?>
<h1><?php echo $title?></h1>
<?php } ?>
<?php if (isset($page_description)) {?>
<p id="page_description"><?php echo $page_description?></p>
<?php } ?>
create.php
<script>
$("#createaccount").validate();
</script>
<?php $attributes = array('id'=>'createaccount');
echo form_open('user/create_account',$attributes); ?>
<?php echo form_label('Email','txtEmail');?>
<br />
<?php $data = array('type'=>'email','name'=>'txtEmail','id'=>'txtEmail','maxlength'=>'50','minlength'=>'2');
echo form_input($data); ?>
<br /><br />
<?php echo form_label('Username','txtUsername');?>
<br />
<?php $data = array('name'=>'txtUsername','id'=>'txtUsername','maxlength'=>'50','minlength'=>'5');
echo form_input($data); ?>
<br /><br />
<?php echo form_label('Password','pswPassword');?>
<br />
<?php $data = array('name'=>'pswPassword','id'=>'pswPassword');
echo form_password($data); ?>
<br /><br />
<?php echo form_label('Confirm Password','pswConfirmPassword');?>
<br />
<?php $data = array('name'=>'pswConfirmPassword','id'=>'pswConfirmPassword');
echo form_password($data); ?>
<br /><br />
<input type="submit" value="Register" name="Register" />
<?php echo form_close();?>
footer.php
<strong>© 2016</strong>
</body>
</html>
result...
<!DOCTYPE html>
<html>
<head>
<title>Discuss Cards - Create New Account</title>
<link rel="stylesheet" href="http://[::1]/forum/styles/forum.css" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://[::1]/forum/js/jquery.validate.min.js"></script>
</head>
<body>
<h1>Create New Account</h1>
<script>
$("#createaccount").validate();
</script>
<form action="http://[::1]/forum/index.php/user/create_account" id="createaccount" method="post" accept-charset="utf-8">
<label for="txtEmail">Email</label>
<br>
<input type="email" name="txtEmail" value id="txtEmail" maxlength="50" minlength="2">
<br>
<br>
<label for="txtUsername"></label>
<br>
<input type="text" name="txtUsername" value id="txtUsername" maxlength="50" minlength="5">
<br>
<br>
<label for="pswPassword">Password</label>
<br>
<input type="password" name="pswPassword" value id="pswPassword">
<br>
<br>
<label for="pswConfirmPassword">Confirm Password</label>
<br>
<input type="password" name="pswConfirmPassword" value id="pswConfirmPassword">
<br>
<br>
<input type="submit" value="Register" name="Register">
</form>
<strong>© 2016</strong>
</body>
</html>
Now, the links to the js and css files are correct. I used the example from http://jqueryvalidation.org/documentation/:
<form class="cmxform" id="commentForm" method="get" action="">
<fieldset>
<legend>Please provide your name, email address (won't be published) and a comment</legend>
<p>
<label for="cname">Name (required, at least 2 characters)</label>
<input id="cname" name="name" minlength="2" type="text" required>
</p>
<p>
<label for="cemail">E-Mail (required)</label>
<input id="cemail" type="email" name="email" required>
</p>
<p>
<label for="curl">URL (optional)</label>
<input id="curl" type="url" name="url">
</p>
<p>
<label for="ccomment">Your comment (required)</label>
<textarea id="ccomment" name="comment" required></textarea>
</p>
<p>
<input class="submit" type="submit" value="Submit">
</p>
</fieldset>
</form>
<script>
$("#commentForm").validate();
</script>
However, I feel that I'm missing something because it's not working. When I use their demo (http://jqueryvalidation.org/files/demo/), a new label is created under the input area which displays the error. However, when I test my code (Notepadd++, latest version of Google Chrome, WAMPServer 2.5), I only get Chrome validation. Could someone point out to me what I'm doing incorrect? Thanks.
A bit too long for a comment, but not sure if it is the only problem. The first thing I see is that the
<script>
$("#createaccount").validate();
</script>
is located before the form with id="createaccount". This means this javascript code is parsed and run before the element exists inside the DOM.
In order to run this JS when the DOM is loaded, you then need to wrap it into this specific jquery part :
$( document ).ready( function( ) {
$("#createaccount").validate();
}
(And ideally you put all JS at the end of the page, before the closing </body> tag)

How process two forms in 1 PHP file?

I've got 1 single PHP file, which contains 2 variables... what I want to achieve is this, first get the first name from the user, and store in in variable $name, then show a form to ask the user for his lastname, THEN.. print both variables, the thing is, when the second form is submitted the first variable disappears, is there a way to keep it in memory?
<?php
$title = rand(100, 300);
?>
<!doctype html>
<html>
<head>
<?php
//functions to call
function nameform() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="name"><br>
<input type="submit" value="name">
<input type="hidden" name="val1">
</form></center>
<?php
}
function lastname() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="lastname"><br>
<input type="submit" value="lastname">
<input type="hidden" name="val2">
</form></center>
<?php
}
?>
<meta charset="utf-8">
<title><?php
echo ("$title"); ?></title>
</head>
<body>
<?php
if(!isset($_POST['name'])){
nameform();
}
if (isset($_POST['name'])) {
$name = $_POST['name'];
lastname();
}
if (isset($_POST['lastname'])) {
$lastname = $_POST['lastname'];
echo "Your name is $name and your last name is $lastname";
}
?>
</form>
</body>
</html>
How about waiting until the form is complete to submit it? I would use a little javascript to take care of this.
<?php
$title = rand(100, 300);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title><?php
echo ("$title"); ?></title>
</head>
<body>
<center>
<?php
if (isset($_POST)) {
$name = $_POST['name'];
$lastname = $_POST['lastname'];
echo "Your name is $name and your last name is $lastname";
} else {
?>
<form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<div id="firstname">
<input type="text" autofocus name="name"><br>
<a href="javascript:;"
onclick="document.getElementById('firstname').style.visibility='hidden';document.getElementById('lastname').style.visibility='visible';">Next</a>
</div>
<div id="lastname" style="display:none;">
<input type="text" name="lastname"><br>
<input type="submit" value="lastname">
</div>
</form>
<?php } ?>
</center>
</body>
</html>
Just set the correct info in your input type hidden, when your second form is send you can get the name value in $_POST['val2'].
See the example:
<?php
$title = rand(100, 300);
?>
<!doctype html>
<html>
<head>
<?php
//functions to call
function nameform() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="name"><br>
<input type="submit" value="name">
<input type="hidden" name="val1">
</form></center>
<?php
}
function lastname() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="lastname"><br>
<input type="submit" value="lastname">
<input type="hidden" name="val2" value="<?php echo $_POST['name']; ?>">
</form></center>
<?php
}
?>
<meta charset="utf-8">
<title><?php
echo ("$title"); ?></title>
</head>
<body>
<?php
if(!isset($_POST['name'])){
nameform();
}
if (isset($_POST['name'])) {
$name = $_POST['name'];
lastname();
}
if (isset($_POST['lastname'])) {
$lastname = $_POST['lastname'];
/*
Here you can get the name that come from the input type hidden named "val2"
*/
$name = $_POST['val2'];
echo "Your name is $name and your last name is $lastname";
}
?>
</form>
</body>
</html>
Or as sjagr says you can use SESSIONS, take a look to this example:
How preserve my inputs values each time that I refresh my page?

displaying the html entities in php code

How to display the input type elements in the php code in the same page
I am trying this code but it is not working
HTML
<html>
<input type="text" name="keywords" id="keywordsid" value="sample keyword" />
</html>
PHP code
<?php
echo $kewords;
?>
To display html input type fields in the php code what should we do?
index.php file
<html>
<form action="index.php" method="post">
<input type="text" name="keywords" id="keywordsid" value="sample keyword" />
<input typ="submit" value="Submit">
</form>
</html>
<?php
if(isset($_POST['keywords']))
{
$keywords = $_POST['keywords'];
echo $keywords;
}
?>
Use PHP pre defined $_SERVER variable :
$_SERVER['PHP_SELF']
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="keywords" id="keywordsid" value="sample keyword" />
<input type="submit">
</form>
<?php
if(isset( $_POST['keywords']))
{
echo $_POST['keywords'];
}
?>
</body>
</html>
Here, <?php echo $_SERVER['PHP_SELF']; ?> helps you to get result on the same page without sending the request to another page.
First of all without form you dont get input type field name in php.
so, use form and after submmit form you are using $_POST['keywords'] instead of $kewords to echo input field name.
let, your page url is index.php
<html>
<body>
<form action="index.php" method="post">
<input type="text" name="keywords" id="keywordsid" value="sample keyword" />
<input type="submit">
</form>
<?php
if(isset( $_POST['keywords']))
echo $_POST['keywords'];
?>
</body>
</html>
Try something like this
<html>
<form action ="" method="post">
<input type="text" name="keywords" id="keywordsid" value="sample keyword" />
<input type="submit">
</html>
and write php code
<?php
if(isset($_POST['keywords']))
{
echo $_POST['keywords'];
}
?>
This is the way to write php and html
You can use the code bellow to echo the keywords.
<?php
echo '<input type="text" name="keywords" id="keywordsid" value="'.$keywords.'" />';
?>

Categories