Good Morning,
I have a set of variables being passed to a form via CodeIgniter's MVC setup, however I am trying to keep from duplicating the form elements if at all possible.
However, I am getting undefined variable errors (for good reasons) when trying to build the input fields on the "add" form.
I know I can duplicate the form elements, one with the $result['data'] and one without, but I would really prefer not.
How would you suggest I build the form below so that I only pass the form_xxxx() functions once? -
if(isset($result) && is_array($result)) // check to see if data is available to fill form
-
{
-
$action = 'welcome/update';
-
$submit = 'Update Old';
-
} else {
-
$action = 'welcome/insert';
-
$submit = 'Add New';
-
}
-
echo form_open($action);
-
echo form_input('service_id',$result['service_id']); // undefined variable
-
echo form_input('updated_date',$result['updateddate']); // undefined variable
-
echo form_submit('submit',$submit);
-
echo form_close();
-
10 7815
AHA!!! Now I get it. Turns out you can use those '@' to stop errors from being reported.
Is there a better solution for this? Something like an internal if statement? -
if(isset($result) && is_array($result))
-
{
-
$action = 'welcome/update';
-
$submit = 'Update Old';
-
} else {
-
$action = 'welcome/insert';
-
$submit = 'Add New';
-
}
-
echo form_open($action);
-
echo form_input('service_id',@$result['service_id']);
-
echo form_input('updated_date',@$result['updateddate']);
-
echo form_submit('submit',$submit);
-
echo form_close();
-
Check out the ternary operator, to easily set a default value to your variable if it doesn't already exist. Using @ is expensive.
YAY! That's what I was looking for.
I did not know the cost of using '@' as with only two variables it is very quick (and running on my local server).
Would you suggest rewriting the form_open and form_submit lines? instead of using the two variables ($action and $submit)? -
if(isset($result) && is_array($result))
-
{
-
$action = 'welcome/update';
-
$submit = 'Update Old';
-
} else {
-
$action = 'welcome/insert';
-
$submit = 'Add New';
-
}
-
echo form_open($action);
-
echo form_input('service_id',(!isset($result['service_id'])) ? '' : $result['service_id']);
-
echo form_input('updated_date',(!isset($result['updateddate'])) ? '' : $result['updateddate']);
-
echo form_submit('submit',$submit);
-
echo form_close();
-
@blyxx86
You know, I'm a CI evangelist, but I've never understood the form helper; it seems pretty lazy to me. Also, I'd think it was more load on the server, too, than just using pre-wrote HTML. I'll benchmark it and see.
I would suggest that you set your variables from within your controller, to abstract away any PHP from the presentation (the fundamental target of MVC), and pass it through the $this->load->view($view, $vars) - $vars being an array of your form data.
- Mark.
It took me some time to move myself over to the form helper.
I was experimenting with form classes (not to be confused with the helper functions) since I have A LOT of forms that need to be setup dynamically.
You're right though, I do need to pass the "Update/New" through the controller rather than set them in the view. I am wondering if I would be able to do the same with the $view_data['result'] variable that is passed to my view, but I would still receive the error about the variable not being defined. Sadly I have only been using this MVC for about 2 weeks, so it's still new to me.
Do you think you could show me a sample of your CI forms that you use? How do you create an add/update form?
It took me some time to move myself over to the form helper.
I was experimenting with form classes (not to be confused with the helper functions) since I have A LOT of forms that need to be setup dynamically.
OK.
You're right though, I do need to pass the "Update/New" through the controller rather than set them in the view. I am wondering if I would be able to do the same with the $view_data['result'] variable that is passed to my view, but I would still receive the error about the variable not being defined. Sadly I have only been using this MVC for about 2 weeks, so it's still new to me.
In the controller, you would check for the variables value (using the ternary operator), and then pass through the values.
Do you think you could show me a sample of your CI forms that you use? How do you create an add/update form?
I don't do my form's dynamically, because I always know what I need from a form. I do, however, use the set_value() function - available when using the form validation class, which you should be ;)
Here's how I generally set up a form: -
<?php if ( strlen ( validation_errors() ) > 0 ) : ?>
-
<div id="validation_errors">
-
<?=validation_errors()?>
-
</div>
-
<?php endif; ?>
-
-
<fieldset>
-
<legend>Log in</legend>
-
<form method="post" action="">
-
-
<table align="center">
-
<tr>
-
<td>Username:</td>
-
<td><input type="text" name="username" value="<?=set_value('username')?>" /></td>
-
</tr>
-
<tr>
-
<td>Password:</td>
-
<td><input type="password" name="password" value="<?=set_value('password')?>" /></td>
-
</tr>
-
<tr>
-
<td> </td>
-
<td align="right"><input type="submit" name="log_in" value="Log in" /></td>
-
</tr>
-
</table>
-
-
</form>
-
</fieldset>
- Most developers would kill me for using 'short tags', but the jokes on you because CodeIgniter has a config option to rewrite short tags to full tags! So nerr.
So how would you pass values from a database to populate your form?
The form helper has a set_value() function that allows you to specify a value to enter in the form, but the form validation set_value() doesn't appear to have that.
I plan to start using the form validation class, but have not yet had the chance to experiment with it.
So how would you pass values from a database to populate your form?
The form helper has a set_value() function that allows you to specify a value to enter in the form, but the form validation set_value() doesn't appear to have that.
Ah, my error. set_value() is part of the form helper.
To pass values from my database, I'd call my model to return an array (or object) of the data I need. Then I'd pass that into the view (using the second parameter of load->view( ). In the view I'd output the data into the input's value.
I plan to start using the form validation class, but have not yet had the chance to experiment with it.
It's a great tool.
- Mark.
Thank you Mark.
I guess the form validation calls part of the form helper into it. Makes sense.
I will play around with that tutorial, though I am still confused as to how to setup the models within CI. I still have a lot to learn, but I am thankful for how well put together the CI user guide is.
Hopefully I will understand the MVC concepts more by the time I am supposed to have some 50+ table database application ready. HAHA!
@blyxx86
I didn't understand the 'model' side of MVC for a while, either. But, in basic terms, the model is what handles grabbing data from your database and returning it to your controller, and taking data from your controller and sticking it in your database.
Any questions you have, be sure to post 'em.
- Markus.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dan Finn |
last post by:
OpenBSD 3.2
Apache 1.3.26
PHP 4.3.4
PHP-Nuke 6.9
getting these in the apache error log:
Sun Nov 16 20:20:16 2003] PHP Notice: Undefined variable:
HTTP_USER_AGENT in...
|
by: Jason |
last post by:
hello,
i am new to PHP, so go easy.
I am using the examples in the book:
PHP: Your Visual Blueprint For Creating Open Source, Server Side Content
In the section where they talk about...
|
by: bissatch |
last post by:
Hi,
I get the following error:
Notice: Undefined variable: end_while in C:\Program
Files\Apache\Apache2\htdocs\csp\inc\xmlmenu.php on line 102
This is a script that works on the server...
|
by: Chris Beall |
last post by:
If you want your code to be bulletproof, do you have to explicitly check
for the existence of any possibly-undefined variable?
Example: window.outerHeight is defined by some browsers, but not...
|
by: Jeremy Felt |
last post by:
Newbie here. I'm sure I'm missing something EXTREMELY simple, but an
hour of searching has led to nothing.
I'm playing around with ajax and trying to pass a variable to a
function.
If I do:...
|
by: Bob Bruyn |
last post by:
I've recently installed Apache 2 and php 5.2 on my WIndows XP
machine. Everything is up and running.
I'm passing some vars via the URL. It works fine online:...
|
by: bob johnson |
last post by:
Notice: Undefined variable: db_host in C:\wamp\www\cbmall\index.php on line 7
Notice: Undefined variable: db_user in C:\wamp\www\cbmall\index.php on line 7
Notice: Undefined variable: db_pass...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
| |