My controls receives some params from the user, I would like to place them inside the view I'm calling, how should I do this without splitting the view into multiple parts?
Thank you.
If I understand correctly, you should be able to do something like this for your controller
<?php
class Blog extends Controller
{
function index()
{
$data['title'] = "My Real Title";
$data['heading'] = "My Real Heading";
$this->load->view('blogview', $data);
}
}
?>
And something like this for your view:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
This is from the Codeignitor User guide here
In Controller:
function show_something() {
$data = array();
if ($this->input->post('some_form_field') {
$data['form_value'] = $this->input->post('some_form_field');
}
$this->load->view('some_view');
}
In View:
<html>
<head>
</head>
<body>
<?php if ($form_value): ?>
<h1><?= $form_value; ?></h1>
<?php endif; ?>
<form method="post" action="">
<input type="text" name="some_form_field" />
<input type="submit" value="Show Value on Page" />
</form>
</body>
</html>
in controller
function show_something() {
$data = array();
if ($this->input->post('some_form_field') {
$data['form_value'] = $this->input->post('some_form_field');
}
$this->load->view('some_view', $data);
}
in view
<html>
<head>
</head>
<body>
<?php if ($form_value): ?>
<h1><? echo $form_value; ?></h1>
<?php endif; ?>
<form method="post" action="">
<input type="text" name="some_form_field" />
<input type="submit" value="Show Value on Page" />
</form>
</body>
</html>
Related
I am trying to find a way for multiple pages to access the data provided from a single HTML form submission.
Currently, I have 4 pages...
index.php (containing my HTML form)
functions.php (where the HTML
form sends data to)
results1.php
results2.php
Can anyone please let me know where I'm going wrong or point me in the right direction?
index.php
<!DOCTYPE html>
<head>
</head>
<body>
<form action="functions.php" method="post">
<input type="text" name="value1">
<input type="text" name="value2">
<input type="submit">
</body>
</html>
functions.php
<?php
$value1 = $_POST["value1"];
$value2 = $_POST["value2"];
?>
results1.php & results2.php
<!DOCTYPE html>
<head>
<?php include_once("functions.php") ?>
</head>
<body>
<?php echo $value1, $value2; ?>
</body>
</html>
You can do it using PHP Session.
Try with the given code below.
index.php
<!DOCTYPE html>
<head>
</head>
<body>
<form action="functions.php" method="post">
Value 1: <input type="text" name="value1"><br>
Value 2 :<input type="text" name="value2"><br>
<input type="submit">
</body>
</html>
functions.php
<?php
session_start();
$_SESSION["value1"] = $_POST["value1"];
$_SESSION["value2"] = $_POST["value2"];
?>
results1.php & results2.php
<!DOCTYPE html>
<head>
<?php session_start(); ?>
</head>
<body>
<?php echo 'Value 1:'. $_SESSION["value1"]; ?><br>
<?php echo 'Value 2:'. $_SESSION["value2"]; ?>
</body>
</html>
<?php
session_start();
function submitform()
{
if(isset($_POST['submit']))
{
notify('positive','hello'.$_POST['name']);
}
}
function notify($type = 'neutral', $message = 'Hello')
{
$_SESSION['notify']['type'] = $type;
$_SESSION['notify']['message'] = $message;
}
function notifcation()
{
if(isset($_SESSION['notify']))
{
$type = $_SESSION['notify']['type'];
$message = $_SESSION['notify']['message'];
$html = '<div class="notify'.$type.'">'.$message.'</div>';
echo $html;
}
}
?>
test.php
<?php require 'functions.php' ?>
<html>
<head>
<title>Notification bar</title>
<link rel="stylesheet" href="" />
</head>
<body>
<div id="container">
<?php notification(); ?> //error
<h1>Test</h1>
<h2>Notifcation</h2>
<form action="<?php submitForm(); ?>" method="post">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
<input type="submit" name="submit" id="submit" value="Send" />
</form>
</div>
</body>
</html>
I am creating a notification for my clothing website and where if the admin account creates a new product, it will show in a box (homepage) of the new entity. So I have created an input, where it will output what was written in the form, but in order to show it in my homepage, how would I created a storage box containing a history of products added, because my output right now only shows when I put a text and press enter and only after another text is inserted.
I have this code (it's an include), but the thing is that when I send the form the $_POST data is not being sent.
Im checking for $_POST data like this
if(isset($_POST['ft_upload']) && $_POST['ft_upload'] == 1){
//$usuario -> uploadFirstTime2($db);
echo "ok";
}
and the code for the form is
<div class="ft_userImage" style="background: url(<?php echo $usuario -> getProfileImage(); ?>);"></div>
<p class="ft_step2_continue"><?=$TEXT_BUTTONS['continue'];?></p>
<form action="" method="POST" class="ft_step2_form_upload">
<input type="hidden" name="ft_upload" value="1" />
</form>
<script>
$("p.ft_step2_continue").click(function(){
$(".ft_step2_form_upload").submit();
});
</script>
check.php
<?php
if(isset($_POST['ft_upload']) && $_POST['ft_upload'] == 1) {
echo "ok";
}
?>
index.html
<!DOCTYPE html>
<html>
<head>
<title>form</title>
</head>
<body>
<form action="check.php" method="POST" class="ft_step2_form_upload">
<input type="hidden" name="ft_upload" value="1" />
</form>
<button id="ft_step2_continue">SEND</button>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$("#ft_step2_continue").click(function(){
$(".ft_step2_form_upload").submit();
});
</script>
</body>
</html>
it works fine.
i think, you just forgot action="check.php" in your form tag.
Here check this:
<?php var_dump($_POST); ?>
<html>
<head>
<title></title>
</head>
<body>
<form action="" method="POST" class="ft_step2_form_upload">
<input type="hidden" name="ft_upload" value="1" />
</form>
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script>
$("p.ft_step2_continue").click(function(){
$(".ft_step2_form_upload").submit();
});
</script>
</body>
</html>
Ok so i did this. Saved it in a php file. And then triggered the submit and it outputs:
array(1) { ["ft_upload"]=> string(1) "1" }
Your php code should be in the same file where the form html is written because your action attribute is empty.
You need to specify the action attribute.
<form action="check.php" method="POST" class="ft_step2_form_upload">
I think you want transfer a file to server??? if yes:
<form method="POST" action="this-file-name.php" enctype="multiform/form-data">
Your Photo:
<input type="file" name="filet" required="required">
<input type="submit" value="Send Photo">
</form>
<?php
if(isset($_FILES['filet'])) {
$dir = './my_dir_name/';
$file_name = $_FILES['filet']['name'];
$doit = #move_uploaded_file($_FILES['filet']['tmp_name'],$dir.$file_name);
if($doit) {
echo 'File '.$file_name.' uploaded';
}
}
?>
I'm trying to make a basic html form that passes information to a php object then adds said object to an array. It works to the point of passing the information from the form, to the object, and adding it to the array and displaying said object. However, when I try to add a second object to the array it only seems to replace the array with a new single element array rather then adding to it. Here is my code... any ideas?
index.php file:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Custom Forms</title>
</head>
<body>
<h2>Add Data</h2>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First Name:<input type="text" size="12" maxlength="12" name="Fname"><br />
Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />
<button type="submit" name="submit" value="client">Submit</button>
</form>
<?php
include_once 'clientInfo.php';
include_once 'clientList.php';
if ($_POST) {
$clientArray[] = new clientInfo($_POST["Fname"], $_POST["Lname"]);
}
if (!empty($clientArray)) {
$clientList = new clientList($clientArray);
}
?>
<p>go to client list</p>
</body>
</html>
clintInfo.php file:
<?php
class clientInfo {
private$Fname;
private$Lname;
public function clientInfo($F, $L) {
$this->Fname = $F;
$this->Lname = $L;
}
public function __toString() {
return $this->Fname . " " . $this->Lname;
}
}
?>
clientList.php file:
<?php
class clientList {
public function clientList($array) {
foreach($array as $c) {
echo $c;
}
}
}
?>
EDITED WORKING CODE WITH ANSWER
index.php file:
<?php
include('clientInfo.php');
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Custom Forms</title>
</head>
<body>
<h2>Add Data</h2>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First Name:<input type="text" size="12" maxlength="12" name="Fname"><br />
Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />
<button type="submit" name="submit" value="client">Submit</button>
</form>
<?php
if ($_POST) {
$testClient = new clientInfo($_POST["Fname"], $_POST["Lname"]);
echo $testClient . " was successfully made. <br/>";
$_SESSION['clients'][] = $testClient;
echo end($_SESSION['clients']) . " was added.";
}
?>
<p>go to client list</p>
</body>
</html>
clientList.php file:
<?php
include('clientInfo.php');
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>
Accessing session variables
</title>
</head>
<body>
<h1>
Content Page
</h1>
<?php
for ($i = 0; $i < sizeof($_SESSION['clients']); $i++) {
echo $_SESSION['clients'][$i] . " was added. <br/>";
}
?>
<p>return to add data</p>
</body>
</html>
The object file clientInfo.php stayed the same. The objects needed to be stored in a multidimensional $_SESSION array and recalled with a for loop, a foreach loop would not work, unless some one else knows a way to make a foreach loop work, stick to a for. On a side note the $testClient variable could be skipped and just created and placed with in the $_SESSION at the same time, however doing it with the temp variable made it easier to trouble shoot and see how to make it work. Just thought I'd post the working code with the answer supplied by Josh!
You need to add persistance to your array object.
See PHP's $_SESSION.
If you don't store it to memory or disk in between requests, it cannot possibly exist on successive loads. There is a nice tutorial in that link that should get you up and running.
Alternatively, you can store things in a database, for larger needs.
I am trying to send PHP array by html form by POST. The code which I am using is below:
<?php
$content = array('111', '222' );
?>
<html>
<head>
<title>PAGE TITLE</title>
</head>
<body>
<form name="downloadexcel" action="downloadexcel.php" method="post">
<input type="text" name="data" value="<?php echo $content; ?>"/>
Download
</form>
<script type="text/javascript">
function submitform()
{
document.downloadexcel.submit();
}
</script>
</body>
</html>
How can I send an PHP array by html form?
Any web link or source code would be appreciated.
You can't send array like this. It will return Array. You can do it this way
<form name="downloadexcel" action="downloadexcel.php" method="post">
<?php foreach ($content as $item): ?>
<input type="text" name="data[]" value="<?php echo $item; ?>"/>
<?php endforeach ?>
Download
</form>
This variant is the most elegant to use in templates or anywhere in the code. HTML will be easily validated by IDE and code assist will be also available.
<?php
$content = array('111', '222' );
?>
<html>
<head>
<title>PAGE TITLE</title>
</head>
<body>
<form name="downloadexcel" action="downloadexcel.php" method="post">
<?php foreach($content as $c) { ?>
<input type="text" name="data[]" value="<?php echo $c; ?>"/>
<?php } ?>
Download
</form>
<script type="text/javascript">
function submitform()
{
document.downloadexcel.submit();
}
</script>
</body>
</html>
Here's is an simple example:
$content = array('111', '222');
foreach($content as $c)
{
echo "<input name='arr[]' value='{$c}'>";
}
You could alternatively use serialize and unserialize to send the values.