Passing a view as a dialog with codeigniter - php

What i would like to do, is instead of loading up a separate page to input data into a database, i would like to pop open a dialog, and upon submission, it would add data to the database and refresh the page.
For example: I'm on the index page and want to add another entry to a datagrid. I would click a button that adds a new entry. A dialog pops up, i add information into it, and hit submit. It would then refresh and remain on the index page.
This is what i currently have, but it doesn't do anything right now.
Index page:
<div id="createEntry">
<button>Create new item</button>
</div>
$("button")
.button().click(function(){
$.post('<?php echo base_url(); ?>test/create', function(response){
$('#dialog').html(response);
});
});
Create page
<div id="dialog" title="Create new test case">
<?php echo form_open(base_url() . 'test/create') ?>
<label for="title">Title</label><br />
<input type="text" name="title" /><br />
<label for="something">Something</label><br />
<textarea name="something"></textarea><br />
<input type="submit" name="submit" value="Create" />
</div>
<script>
$("#dialog").dialog();
</script>
Controller
public function create()
{
//...
//some code here for validation
//
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$var = $this->load->view('test/create', '', TRUE);
$this->load->view('templates/footer');
}
else
{
//adds to db
$this->test_model->set();
}
}
I feel like i'm close. There's just something i'm missing
Any help is appreciated.

Simple answer. I needed a <div id="dialog"> on the index page.

Related

php: Form to reveal more options depending on which radio button checked

I'm trying to create a form for entering Computer or Televisions as electronics. I want first the user to select which of the two he's entering, and depending on which one he chooses, he's going to have different stuff to fill in.
I tried to write this but it doesn't work. I get an error on $answer= $_POST['type'];
Please let me know what is wrong, I'm kind of new at php. Thanks.
<div class="form-group">
<h3 style="color:blue;">Type of Item</h1>
<form name ="form1" method ="post">
<input type="radio" name="type" value="Computer"> Computer<br>
<input type="radio" name="type" value="Television"> Television<br>
</form>
</div>
<?php
$answer = $_POST['type'];
if ($answer == "Computer") {
//show stuff
}
else {
//show other stuff
}
?>
$_POST will be empty until the form is actually submitted. Your form does not have a submit button. That said, this type of task is usually better done in javascript since it doesn't require a full page reload just to open up the next set of options in a form.
Here's an example of how this might work using a simple javascript function:
<script type="text/javascript">
function toggleOptions() {
if ( document.getElementById('type_Computer').checked ) {
document.getElementById('nextSetOfComputerOptions').style.display = '';
} else {
document.getElementById('nextSetOfComputerOptions').style.display = 'none';
}
}
</script>
<input type="radio" name="type" id="type_Computer" value="Computer" onclick="toggleOptions();">
<div id="nextSetOfComputerOptions" style="display:none;">
<!-- more form fields -->
</div>

sending data through submit using colorbox ajax

HTML
<div class="cus_input">
<form id="category_form" method="GET" action="<?php echo base_url(); ?>welcome/find/" >
<input type="text" id="category_input" name="q" placeholder=" Find Category"/>
<a type="submit" class="ajax cboxElement" id="category_submit" src="<?php echo base_url() ?>img/board/icons/add.jpg" value="" />
</form>
</div>
Controller
function find()
{
$this->pageload_model->load_page();
$getquery = $this->input->get("q");
$data['find'] = $this->find_model->get_find_view($getquery);
$page['content'] = $this->load->view("template/findtemplate.php", $data);
echo json_encode($page);
}
This controller correctly generates my desired results when you manually go to the URI. It was slightly modified in the attempt to use jquery ui dialog. However, i'd much rather use colorbox or fancybox at this point.
find model:
public function get_find_view($q)
{
if (!$q) {
$html = "Search disrupted: <a href='". base_url()."'>Go back to main page </a>";
return $html;
} else {
$accountdata['found'] = $this->find_model->get_found_view($q);
$accountdata['create_category'] = $this->find_model->get_create_category_view($q);
return $this->load->view('find/find_view', $accountdata , TRUE);
}
}
the colobox ajax JS should be: $('ajax').colorbox()
I have the ajax class in 2 areas...a straight up link, which works with no problems, and the above mentioned submit button. What I am looking to do is send the input value to the find() controller, load the page, and open it via the colorbox window. I eventually would add a change to pushstate, so that the new URL / page can be crawled and shared.
I am having no luck. Thanks for the help! Will add any code you might need.
I haven't used colorbox before so I'm not 100% on this. I think what you need to do is serialize the data from your form and handle the ajax request yourself. So something like this:
HTML:
<div class="cus_input">
<form id="category_form" method="GET" action="<?php echo base_url(); ?>welcome/find/" onsubmit="return category_form_submit();" >
<input type="text" id="category_input" name="q" placeholder=" Find Category"/>
<a type="submit" id="category_submit" src="<?php echo base_url() ?>img/board/icons/add.jpg" value=""/>
</form>
</div>
JQUERY:
function category_form_submit() {
$.get('/welcome/find?' + $('#category_form').serialize(), function(response){
$.fn.colorbox({html:response});
//here is where you could put your pushstate code
});
return false;
}

PHP Codeigniter form submit using JQuery

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();
});

jQuery popup dialog on PHP form that confirms and refreshes page - not working

I have seen what seems like a hundred ways to do what I want but I can't seem to get a single one to work. I have a test page here : http://upcycledonline.com/test/Site/defaultUpCyc.php
What I want to happen is when the user clicks submit a pop window appears saying "Thanks! Your email has been added". When they click 'ok' the pop will close and the page refreshes. Right now I have the pop up going but after clicking the ok button it goes to my PHP page.
FYI: I am new to PHP and Javascript
Here is the form code and Javascript
<div id="signUp">
<script>
function confirmSubmit() {
if (confirm("Are you sure you want to submit the form?")) {
document.getElementById("FORM_ID").submit();
}
return false;
}
</script>
<?php
//if the validation falls back to php, then print the validation error
if (isset($error_message)) echo $error_message;
?>
<form method="post" action="process-form.php" id="emailForm" name="emailForm" target="_self">
<h4>Sign up to be notified when we go live!</h4>
<!--value="<?php if (isset($_POST['email'])) echo $_POST['email'];?>"-->
<label for="email">E-mail</label>
<input type="text" name="email" id="email" />
<!-- onSubmit="alert('Thank you. Your email has been added.')"-->
<input type="submit" name="submit" id="submit" value="Submit" onclick="return confirm('Are you sure?');">
<p>emails will not be shared with third parties</p>
</form>
<script>
<?php echo $validation_js_code;?>
</script>
</div>
You could do a couple things:
move your form processing logic to defaultUpCyc.php, submit the form to that URI and then have defaultUpCyc.php both process the form and reload the page.
Use AJAX, and post the data to process-form.php, this wouldn't require any refresh at all.
Do a redirect in process-form.php to defaultUpCyc.php.

Linking page in a div with html forms and php

So I have this html code
<div id="wrap">
<div id="header">
</div>
<div id="content">
<form method="POST" action="code.php">
Name:
<input type="text" name="name" size="50">
<input type=submit value="Get Code">
</form>
</div>
<div id="footer">
</div>
Is it possible to load the code.php after the user clicks submit into the #content div?
Essentially, what I want is when the user clicks the submit button, the code.php after processing is loaded onto the same #content div.
So let say in my code.php, after processing the inputted data, I come up with this lilne of code,
<?php
some processing code here;
$name = 'john';
echo $name;
?>
So then after hitting submit, user would see
<div id="content">
john
</div>
Hope I didn't complicate my question by repeating myself, please let me know if this is possible with javascript, php or whatever.
Thanks for the read!
#JohnP yes, $.load is a good solution. However, you'll need to send the form data in the request:
UPDATED [3] for sending a POST with multiple fields and checkboxes:
$('form').submit(function(){
// create an object to send as a post
var form = this,
fields = form.elements,
el,
post = {};
for (var i = fields.length; i--; ) {
el = fields[i];
if (el.name) {
switch (el.type) {
case 'checkbox':
case 'radio':
post[el.name] = (el.checked) ? el.value : '';
break;
default:
post[el.name] = el.value;
}
}
}
// send the form data in the load request...
$('#content').load(this.action, post);
return false;
});
This will send the data as a POST.
Since you've tagged jQuery, I'll use a jQuery example
$(document).ready(function(){
$('form').submit(function(){
$('#content').load('code.php');
return false;
})
})
This makes a couple of assumptions here
This assumes that code.php is in the same path that you are in now.
There is only one form in the page.
As #johnhunter points out, this example obviously won't work with post. You can send the post data along with the method. See here for usage : http://api.jquery.com/load
EDIT
Here's a fiddle example : http://jsfiddle.net/jomanlk/J4Txg/
It replaces the form area with the content from jsfiddle/net/echo/html (which is an empty string).
NOTE 2 Make sure to include the code in $(document).ready() or include it at the bottom of the page. It goes without saying you need jQuery in your page to run this.
You might want to check out jquery form plugin http://jquery.malsup.com/form/#
in simple way use
<div id="content">
if(isset($_POST['submit'] && !empty($_POST) )
{
// do your all post process
$name ='John';
echo $name;
}
else {
<form method="POST" action="$_SERVER['PHP_SELF']">
<label for="uname" >Name:</label><input type="text" name="uname" id="uname" size="50">
<input type=submit value="Get Code" name="submit">
</form>
}
</div>

Categories