I have a input field
<?php
echo form_open('moneyexchange/borrow_first_page'); ?>
<input id ="Amount" type="text" placeholder="Amount in €" name="writtenamount">
<a type="submit" href="<?php echo base_url();? >index.php/moneyexchange/invest_first_page">invest</a>
</form>
And in codeigniter moneyexchange controller I have a function here
public function invest_first_page(){
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->view('header');
$userProvidedAmount = $this->input->post("writtenamount");
$data = array(
'userProvidedAmount' => $userProvidedAmount
);
$this->load->view("invest_firstpage", $data);
$this->load->view('footer');
}
But for some reason I cannot get the value from the input field in the view file to the controller, please help.
it shows that my $userPRovidedAmount is a (bool)false value. But I need to get numbers from the input variable
You have a link where your submit button should be. A link wont submit the form (unless there is some unseen javascript) - it will just make a regular GET request to the page (hence why POST is empty in your controller)
Change the action of your form and make the submit button a form element:
<?php echo form_open('moneyexchange/invest_first_page'); ?>
<input id ="Amount" type="text" placeholder="Amount in €" name="writtenamount"/>
<input type="submit" value="invest"/>
<?php echo form_close();?>
Related
I have installed a complete PHP system that is based on CodeIgniter. Its first view is a form with action set to
"<?php echo current_url(); ?>"
and this view is shown from a file that defines a class with some methods, a method for each subsequent view. This setup method exists in the directory: setup/controllers/Setup.php, while the views shown are in the directory: setup/views.
The code that shows the first view is:
if ($this->input->post('licence_agreed')) {
$this->session->set_tempdata('setup_step', 'requirements', $this->setup_timeout);
redirect('requirements');
}
if ( ! file_exists(VIEWPATH .'/license.php')) {
show_404();
} else {
$this->load->view('header', $data);
$this->load->view('license', $data);
$this->load->view('footer', $data);
}
The license form is defined as:
<form accept-charset="utf-8" method="POST" action="<?php echo current_url(); ?>" />
<input type="hidden" name="licence_agreed" value="1" />
<div class="terms"><?php echo lang('text_license_terms'); ?></div>
<div class="buttons">
<p class="text-right"><?php echo lang('text_license_agreed'); ?></p>
<div class="pull-right">
<button type="submit" class="btn btn-success"><?php echo lang('button_continue'); ?></button>
</div>
</div>
When I load the first view and post the form, it reloads the same view again. I have tried to debug the method above and it seems that it only executes the same if condition each time with the same result, regardless of the value of licence_agreed parameter.
I can't figure out what is the problem. It works fine on the server. But locally it reloads the same very first view all the time with no action taken.
I have home_c controller, home_m model and home_v view page in code igniter application folder. My view home_v page contains following code.
<?php
echo form_open('home_c/save');?>
Name:<input type="text" name="name" value="">
<input type="submit" name="sub" value="Save">
<?php echo form_close();?>
<?php
echo form_open('home_c/view');?>
<input type="submit" name="view" value="View">
<?php echo form_close();?>
My problem is that, I could not execute function view() in controller home_c. But I can execute same function by placing instead of the view button. I don't know what is the real issue behind this. Anybody please help me to solve this issue.
If you want different action for each button, use the a href = base_url 'Controller/Method'.. Like this one, button type = "button" a href=" base_url() . 'Controller/Method'
if($this->input->post('sub')=='Save'){
$this->your_model->add($data);
}
if($this->input->post('view')=='View'){
$this->load->view('view',$data);
}
Change like this and check
I'm trying to create a page with a form using ci.
When i submit the form, the controller says that I have no data that's been submitted.
I can't see where my error lies.
Here's the view:
<?php echo validation_errors(); ?>
<?php echo form_open('widgets/search/'.$hardwaremodel.'/'.$objectid.'/'.$name.'/'.$fd); ?>
<div class="form-group">
<label for="search">Last 4 characters of address:</label>
<input type="text" class="form-control" id="searchstring" placeholder="last 4 characters" size="4">
</div>
<button type="submit" class="btn btn-default">Search</button>
<button type="cancel" class="btn btn-default">Cancel</button>
</form>
Once the page renders, the form tag ends up looking like this:
<form action="http://myserver/myciapp/index.php/widgets/search/205406zl/5461/SW-1/SW1net" method="post" accept-charset="utf-8">
The controller:
public function search()
{
$searchstring = $this->input->post(); // form data
var_dump($searchstring);
exit;
}
The results of the var_dump shows:
bool(false)
Thanks
EDIT 1
I haven't posted the entire HTML page that includes the form... but I display some of the fields passed in the URI as headings on the page - just before I create the form. Hope that clarifies...
Would this impact the POST data? Why is that relevant?
Thanks
A few things I'd suggest doing. First is, if you are going to include other variables in the form_open tag, I would add those variables to your controller, and put them in the form_open tag as URI strings. This will allow the form validation to work if you are going to echo out validation errors.
Also, you should be calling a name on the input->post() to get the specific item, (but you don't need to to get all POST data).
Controller:
public function search($hardwaremodel, $objectid, $name, $fd) {
$searchstring = $this->input->post('search_string'); // form data
var_dump($searchstring);
exit;
}
View:
<?php echo form_open('widgets/search/'.$this->uri->segment(3).'/'.$this->uri->segment(4).'/'.$this->uri->segment(5).'/'.$this->uri->segment(6)); ?>
<div class="form-group">
<label for="search">Last 4 characters of address:</label>
<input type="text" class="form-control" id="search string" name="search_string" placeholder="last 4 characters" size="4">
</div>
Form elements are referenced by name attribute which is missiong on input field searchstring.
Add:
name="searchstring"
on your input field.
in this code you have not used name attribute.You use id.
try this one
<input type="text" name="searchstring" value="xyz">
I am new to PHP Codeigniter framework. I am designing a page in which I am using a link. On clicking the link it is calling a jquery function, which submits form through jquery. I have used codeigniter form validation methods for server side validation and for the timebeing I have disabled the client side validation.
The problem is that in this process when the form is submitted through jquery, the codeigniter form validation method is not working.
But if I am using a submit button to submit the form then the codeigniter form validation method works perfectly.
Please advise me what to do if I need to submit the form through jquery and use the codeigniter form validation method.
Please find the code below:
Login Form:
<?php echo validation_errors(); ?>
<form name="login-form" id="login-form" method="post" action="<?php echo base_url();?>index.php/login/login_form" >
<H2>Login</H2>
<div id="login-box-name">
Email:
</div>
<div id="login-box-field">
<input name="user-name" id="user-name" class="form-login" title="Please Enter Correct User Name" value="" size="30" maxlength="2048" />
</div>
<div id="login-box-name">
Password:
</div>
<div id="login-box-field">
<input name="password" id="password" type="password" class="form-login" title="Please Enter Correct Password" value="" size="30" maxlength="2048" />
</div>
<br />
<span class="login-box-options">
<input type="checkbox" name="1" value="1" title="Want this computer to remember you!!"> Remember Me Forgot password?
</span>
<br />
<br />
<a href="" id="login-submit">
<img src="<?php echo base_url();?>assets/images/login-btn.png" width="110" height="40" style="margin-left:90px;" />
</a>
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
jquery function to submit the form on clicking the "link":
$("#login-submit").click(function()
{
$('#login-form').submit();
return false;
});
Controller function:
public function login_form()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$data['title'] = 'Log In';
$data['errorMessage'] = '';
$this->form_validation->set_rules('user-name', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('login', $data);
$this->load->view('templates/footer');
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('templates/menu');
$this->load->view('index', $data);
$this->load->view('templates/footer');
}
}
Here if I click on the "Submit button of the form, then the codeigniter validation works for user-name and password fields. But if I click the link with id="login-submit", then it calls the jquery function and the form get submitted. But the codeigniter validation does not work for user-name and password fields this time.
I need to submit the form through the link and the codeigniter validation function should work for this.
Thanks in Advance.....
Use .preventDefault() instead of just returning false on the anchor click event.
This has happened to me before and it seems to me that there is a conflict somewhere using .submit() inside a click event and returning false to stop the anchor's default behavior.
The code above is working fine. Actually I made a silly mistake in my code.
I used following code for jQuery and it is working now.
$("#login-submit").click(function(e) {
e.preventDefault();
$('#login-form').submit();
});
Thanks
it happens because it calls the function all the time when clicking on the button. you need to stop that.
$("#login-submit").click(function(e){
e.preventDefault();
$('#login-form').submit();
});
I want to build an string/text manipulation app that:
An app consists of a form
User inputs text there as a string using
Click a button - which usues $_POST method to manipulate a string using oop method to :
Example method assigned to a button:
public function revers_sentence() {
$this->string = strrev($this->string);
return $this;
}
Then the manipulated string is displayed in the same form.
User can do another manipulation with a converted string
How to assign method to a button to trigger the function and display results in the same form?
Can it be achived in a single php file to send and get a result?
(I want to store my classes in a seperate file)
Any help/idea Appreciated - any wise guy?
edit:
<?php
require_once('mod.php');
$string='';
if (isset($_POST['string']))
$string=$_POST['string'];
if (isset($_POST['DoStuff']))
{
//$string = reverse_1_word($string);
**$string->reverse();**<-------------------------------Fatal error:---------
}
if (isset($_POST['DoOtherStuff']))
{
$string = doOtherStuffWithThisString($string);
}
?>
Fatal error: Call to a member function odwroc() on a non-object on line 14 so how to make every new string an object ?
I would do something like that:
<?php
Class MyString{
private $string='';
function __construct(){
if (isset($_POST['string']))
$this->string=$_POST['string'];
}
function doStuffWithThisString(){
$this->string=$this->string.'!';
}
function doOtherStuffWithThisString(){
$this->string=$this->string.'!!!';
}
function getString(){
return $this->string;
}
}
$myString = new MyString();
if (isset($_POST['DoStuff']))
{
$myString->doStuffWithThisString();
}
if (isset($_POST['DoOtherStuff']))
{
$myString->doOtherStuffWithThisString();
}
?>
<form action="" method="post">
<!-- blank action attribute will post form to the current page-->
<input type="text" value="<?=$string->getString()?>" name="string" />
<!-- <?=$string->getString()?> is the same as <?php echo $string->getString(); ?> -->
<input type="submit" value="Do Stuff" name="DoStuff" />
<input type="submit" value="Do Other Stuff" name="DoOtherStuff" />
</form>
You could have a form like this :
<form action="currentFile.php" method="post">
<input type="text" value="myValue" name="text" />
<input type="submit" value="Reverse !" name="reverse" />
<input type="submit" value="Other manipulation" name="otherManip" />
</form>
When you receive the data, you can do this :
if ( isset($_POST['reverse']) ) {
// User clicked on the reverse button
} elseif ( isset($_POST['otherManip']) ) {
// User clicked on the other button
}
to achive the send and get in a single PHP file you have to set the forms action to the actual site you are on. I do this ba writing <form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post"> FORMFIELDS AND DATA </form>
To work with the returned values you just have to build an if-statement, that checks for the passed values if (isset($_POST['buttonname'])){ //DO SOMETHING}