How to call a value from function set_value in Code Igniter? - php

I have a simple task , and i'm using a MVC methods and CI framework for this task.
I have made a view to input data to database, and it's work, and i make a 2 anchors, those are an [Update] and [Delete], the function delete is working, but the update isn't working.
After user click anchor [Update], it will linked to another view(update_view), and i want to show the content which i've clicked in the (update_view). and i think it use a set_value to set a second parameter, to show a value in my update_view.
This is my code in a view(update_view)
<!-- update_view.php -->
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>Update</h2>
<?php echo form_open('site/update'); ?>
<p>
<label>Judul : </label>
<input type="text" name="judul" id="judul" value="<?php echo set_value('judul','??')?>"/>
</p>
<p>
<label>Konten : </label>
<input type="text" name="konten" id="konten" size="100px" value="<?php echo set_value('konten','??')?>"/>
</p>
<p><input type="submit" value="Ubah" /></p>
<?php echo form_close(); ?>
</body>
</html>
What should i put in input tag in value attributes, i want to show a value in input fields (judul, konten) from page before where i clicked the anchors.
I still can't show a image for a view before click, because i'm still don't have 10 rep to share images. so i'will show the coding where the view(options_view) i've clicked the anchors.
This below is the code in a view(options_view) :
<!-- options_view.php -->
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>Create</h2>
<?php echo form_open('site/create'); ?>
<p>
<label>Judul : </label>
<input type="text" name="judul" id="judul" />
</p>
<p>
<label>Konten : </label>
<input type="text" name="konten" id="konten" size="100px"/>
</p>
<p><input type="submit" value="Simpan" /></p>
<?php echo form_close(); ?>
<hr/>
<h2>Read</h2>
<?php if(isset($records)): foreach($records as $baris) : ?>
<h3><?php echo $baris->judul ?></h3>
<div><?php echo $baris->konten ?></div>
<?php echo anchor("site/view_update/$baris->id","[Update]"); ?>
<?php echo anchor("site/delete/$baris->id","[Delete]"); ?>
<?php endforeach; ?>
<?php else : ?>
<h3>Tidak ada data.</h3>
<?php endif; ?>
</body>
</html>
i'm still doubt, whether i should add code in my controller or my view(set_value).
So anyone can help me to solve this problem.
Thank's for help

set_value is codeigniter form_helper function. That helps u to print previous input value when form_validation fails.
In your case if u want to show your data u need to pass data to the view in controller.
E.G:
Controller:
$data["me"] = $this->model->getData($id);
$this->load->view("update_view",$data);
View:
<input type="text" name="judul" id="judul" value="<?php echo $me->judul;?>"/>

Related

why hidden box using in php post method in form

Hi I am in New In Php.
I am asking why we are using hidden text box and given value =1.
<input type="text" name="form_submitted" value="1"/>
<html>
<head>
<title>Registration Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<?php if (isset($_POST['form_submitted'])): ?>
<?php if (!isset($_POST['agree'])): ?>
<p>You have not accepted our terms of service</p>
<?php else: ?>
<h2>Thank You <?php echo $_POST['firstname']; ?></h2>
<p>You have been registered as <?php echo $_POST['firstname'] . ' ' . $_POST['lastname']; ?> </p>
<p> Go <a href="sample.php" >back</a> to the form</p>
<?php endif; ?>
<?php else: ?>
<h2>Registration Form</h2>
<form action="sample.php" method="POST">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br>
Agree to Terms of Service: <input type="checkbox" name="agree"> <br>
**<input type="hidden" name="form_submitted" value="1"/>**
<input type="submit" value="Submit">
</form>
<?php endif; ?>
</body>
</html>
In this case, the reason the developer have done this, is because it is used in the first IF-statement.
if (isset($_POST['form_submitted'])):
When the form is submitted, it is sent to the same file. When a form is submitted, the form values is accessible via the $_POST paremeter in php. So if $_POST['form_submitted'] is set, then he executes the following code, and if not, the code inside else: is executed
I also have to say that this code is not a good example of how to handle form submissions, and should be improved.
Developers using <input type="hidden" name="form_submitted" value=""> because sometimes they use it as reference for the form submission. They can use it for conditions or whatever functionality they want to use with the hidden input.
Instead of showing the exact input (input text), they're hiding it for more cleaner form.

How to make "MadLibs" form results appear below form fields after submitting?

Thanks in advance for your help. I've searched a lot before posting this but I end up more confused than when I started :)
I'm trying to have one page contain the form fields and after pressing submit, the resulting story with user's form field entries inserted into the story.
It would be great to have the text from the form fields remain so that the user doesn't need to retype everything if they need to change a word or two.
I really appreciate your help. Hopefully this will help many people at once.
<html>
<head>
<title>My MadLib</title>
</head>
<body>
<h1>MadLib</h1>
<?php if (isset($_POST['action']) && $_POST['action'] == "show"): ?>
<p>Hello, I am a <?php echo $_POST['adj'] ?> computer that owns a <?php echo $_POST['noun'] ?>.</p>
<?php else : ?>
<form action="madlib.php" method="post">
<input type="hidden" name="action" value="show">
<p>An adjective: <input type="text" name="adj"></p>
**strong text** <p>A noun: <input type="text" name="noun"></p>
<p><input type="submit" value="Go!"></p>
</form>
<?php endif ?>
</body>
</html>
As you said you don't want to "keep it simple", you may simply add the needed value attribute to each of your <input>s, like this:
<html>
<head>
<title>My MadLib</title>
</head>
<body>
<h1>MadLib</h1>
<?php
if (isset($_POST['action']) && $_POST['action'] == "show") {
?>
<p>Hello, I am a <?php echo #$_POST['adj']; ?> computer that owns a <?php echo #$_POST['noun']; ?>.</p>
<?php
} else {
?>
<form action="madlib.php" method="post">
<input type="hidden" name="action" value="show">
<p>An adjective: <input type="text" name="adj" value="<?php echo #$_POST['adj']"; ?> /></p>
**strong text**
<p>A noun: <input type="text" name="noun" value="<?php echo #$_POST['noun']"; ?> /></p>
<p><input type="submit" value="Go!"></p>
</form>
<?php
}
?>
</body>
</html>
Note the (sometimes unloved) "#" to prevent firing a notice when $_POST['...'] doesn't exist yet. I also added the same in your <p>Hello... line.

Printing html page and doing some action using 1 button

I have a Print button which I want to do 2 actions when clicked:
Updating my database.
Printing the HTML page.
This is what I've done so far, but it's not working:
<form action="" method="POST">
<body >
<?php
$n=$_POST['ID'];
$a=implode("</br>",$n);
list($add, $ward) = explode("(!#!)", $a);
?>
<div id="container">
<p id="address">
<?php echo"$address";?>
</p>
<p id="ward">
<?php echo"$ward";?>
</p>
</div>
<input type="submit" name="Print" value="Print" />
<?php
if(isset($_POST['Print']))
{
?><script>javascript:window.print()</script><?php
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
}?>
<div id="footer">
</div>
</form>
After using this print button , my database is getting updated, but the print window is showing error(i.e the variables posted from other page are showing errors).
Can anyone please help me print and update at same time with this Print button?
If your page does not have it, add form tags. They are mandatory unless you want to AJAX the data. Also remove the semi-colon from the end of the sql
<form action="" method="POST">
<input type="submit" name="Print" value="Print" />
</form>
<?php
if(isset($_POST['Print'])) {
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
?><script>window.print();</script>
<?php } ?>
UPDATE: Perhaps you mean this, but I do not want to keep correcting HTML.
<?php
$n=$_POST["ID"];
$a=implode("</br>",$n);
list($add,$ward)=explode("(!#!)", $a);
?>
<body>
<div id="headerbg">
<div id="header-e1"><a align="left" href="escalationReport.php">Back </a>
</div>
<div id="header-e3"><a align="right" href="logout.php">Logout </a>
</div>
<h1><p>Issue Notice</h1>
</div>
<div id="container">
<p id="address">
<?php echo "$address";?>
</p>
<p id="ward">
<?php echo "$ward";?>
</p>
</div>
<form action="" method="POST">
<input type="submit" name="Print" value="Print" />
</form>
<?php if(isset($_POST[ 'Print'])) {
mysql_query( "UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
?>
<script>
window.print();
</script>
<?php } ?>
<div id="footer"></div>
</body>
you have an error in the update query, you have placed semicolon inside the query so please remove that so the query will be like this.
Make sure your form method is POST
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
Make sure you are using input type button inside the form tag. And use post method for form.

PHP: When echo a JavaScript alert, text input size decreases

I have a basic HTML form like this. When submitted, it checks for empty fields and alert the user that both fields are required. After alert, user is redirected to the same page but text input sizes decrease. How can I retain the same width, height after PHP "echo"?
Screenhot 1:
Screenshot 2:
Screenshot 3:
Controller:
function validate_user_pass() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username','Email', 'required|max_length[30]');
$this->form_validation->set_rules('password','Password', 'required|max_length[32]');
if ($this->form_validation->run() == FALSE ) {
//$this->load->view('login_error');
echo '<script type="text/javascript">alert("Required fields are empty!");</script>';
$this->index();
}
else {
$this->verify();
}
}
View:
<?php echo form_open('login/validate_user_pass'); ?>
<div class="login">
<div class="login-screen">
<div class="login-icon">
<img src="<?php echo base_url(); ?>assets/images/1374606542_newsstand_ios7_ios_7.png" alt="logo">
<h4>Hotel<small>Management System</small></h4>
</div>
<div class="login-form">
<img src="<?php echo base_url(); ?>assets/ico/logo_small.png" alt="logo">
<div class="control-group">
<input type="text" class="login-field" name="username" value="<?php echo set_value('username'); ?>" placeholder="Enter username" id="login-name">
</div>
<div class="control-group">
<input type="password" class="login-field" name="password" value="" placeholder="Password" id="login-pass">
</div>
<input type="submit" class="btn btn-primary" value="Login">
</div>
</div>
</div>
<?php echo form_close(); ?>
it may be late to answer but same problem was with me. I overcome this by putting the error popup box out of the header. I searched everywhere and ended up here.
My code was something like this before:
<?php
//some code for verification and echo out/show error message
?>
<html>
<!--HTML code that has form-->
</html>
What I did was something like this:
<html>
<head>
<title></title>
</head>
<body>
<?php
//some code for verification and echo out/show error message
?>
<div>
<!--HTML code that has form-->
</div>
</body
</html>
This problem mostly exist because of echo present in header part. You can check your views for this and that may solve the problem.
This typically happens when a php error or var_dump() is on the page, but you may not see it because it is hidden under a fixed header div, for example.
Look at your View source & check if there aren't php errors on the page

php conditional syntax; both branches appearing

A php 'if statement' example from a tutorial by Kevin Yank doesn't seem to work. Specifically, both branches of the conditional appear when the page loads.
I have also tried changing to the non-shorthand 'if' syntax, to no avail.
Is the problem something other than syntax?
The code is as follows:
<!DOCTYPE html>
<html>
<head>
<title> Sample Page </title>
</head>
<body>
<?php if(isset($name)) : ?>
<p> Your name: <?php echo ($name); ?> </p>
<p> This paragraph contains a link that passes the name variable on to the next document. </p>
<?php else : ?>
<!-- No name has been provided, so prompt the user for one -->
<form action="<php echo($PHP_SELF); ?>" method="get">
Please enter your user name:
<input type="text" name="name">
<input type="submit" value="ok">
</form>
<?php endif; ?>
</body>
</html>
This script left out a very important element >>> $name=$_GET['name']; <<<
The name must first be declared as a variable.
It has been added inside this line: <?php if(isset($name)) : ?>
See my code below: (tested and working)
<HTML>
<HEAD>
<TITLE> Sample Page </TITLE>
</HEAD>
<BODY>
<?php $name=$_GET['name']; if (isset($name)): ?>
<P>Your name: <?php echo($name); ?></P>
<P>This paragraph contains a
<A HREF="newpage.php?name=<?php echo(urlencode
($name)); ?>">link</A> that passes the
name variable on to the next document.</P>
<?php else: ?>
<!-- No name has been provided, so we
prompt the user for one. -->
<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD="GET">
Please enter your name: <INPUT TYPE="TEXT" NAME="name">
<INPUT TYPE="SUBMIT" VALUE="GO">
</FORM>
<?php endif; ?>
</BODY>
</HTML>
Plus as an added bonus (tested and working), you can use the PHP code below, inside your goodone.php file to echo the name after you clicked on the link after you submitted the form. You can do whatever you wish from hereonin, for database use, etc.
<?php
echo($_GET['name']);
// do something else
?>

Categories