473,606 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Undefined Variable Solution??

blyxx86
256 Contributor
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?

Expand|Select|Wrap|Line Numbers
  1. if(isset($result) && is_array($result)) // check to see if data is available to fill form
  2. {
  3.     $action = 'welcome/update';
  4.     $submit = 'Update Old';
  5. } else {
  6.     $action = 'welcome/insert';
  7.     $submit = 'Add New';
  8. }
  9.     echo form_open($action);
  10.     echo form_input('service_id',$result['service_id']); // undefined variable
  11.     echo form_input('updated_date',$result['updateddate']); // undefined variable
  12.     echo form_submit('submit',$submit);
  13.     echo form_close();
  14.  
Mar 12 '09 #1
10 7985
blyxx86
256 Contributor
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?

Expand|Select|Wrap|Line Numbers
  1. if(isset($result) && is_array($result))
  2. {
  3.     $action = 'welcome/update';
  4.     $submit = 'Update Old';
  5. } else {
  6.     $action = 'welcome/insert';
  7.     $submit = 'Add New';
  8. }
  9.     echo form_open($action);
  10.     echo form_input('service_id',@$result['service_id']);
  11.     echo form_input('updated_date',@$result['updateddate']);
  12.     echo form_submit('submit',$submit);
  13.     echo form_close();
  14.  
Mar 12 '09 #2
Markus
6,050 Recognized Expert Expert
Check out the ternary operator, to easily set a default value to your variable if it doesn't already exist. Using @ is expensive.
Mar 12 '09 #3
blyxx86
256 Contributor
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)?

Expand|Select|Wrap|Line Numbers
  1. if(isset($result) && is_array($result))
  2. {
  3.     $action = 'welcome/update';
  4.     $submit = 'Update Old';
  5. } else {
  6.     $action = 'welcome/insert';
  7.     $submit = 'Add New';
  8. }
  9. echo form_open($action);
  10. echo form_input('service_id',(!isset($result['service_id'])) ? '' : $result['service_id']);
  11. echo form_input('updated_date',(!isset($result['updateddate'])) ? '' : $result['updateddate']);
  12. echo form_submit('submit',$submit);
  13. echo form_close();
  14.  
Mar 12 '09 #4
Markus
6,050 Recognized Expert Expert
@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.
Mar 13 '09 #5
blyxx86
256 Contributor
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?
Mar 13 '09 #6
Markus
6,050 Recognized Expert Expert
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:
Expand|Select|Wrap|Line Numbers
  1. <?php if ( strlen ( validation_errors() ) > 0 ) : ?>
  2.         <div id="validation_errors">
  3.             <?=validation_errors()?>
  4.         </div>
  5.         <?php endif; ?>
  6.  
  7.         <fieldset>
  8.             <legend>Log in</legend>
  9.             <form method="post" action="">
  10.  
  11.                 <table align="center">
  12.                     <tr>
  13.                         <td>Username:</td>
  14.                         <td><input type="text" name="username" value="<?=set_value('username')?>" /></td>
  15.                     </tr>
  16.                     <tr>
  17.                         <td>Password:</td>
  18.                         <td><input type="password" name="password" value="<?=set_value('password')?>" /></td>
  19.                     </tr>
  20.                     <tr>
  21.                         <td>&nbsp;</td>
  22.                         <td align="right"><input type="submit" name="log_in" value="Log in" /></td>
  23.                     </tr>
  24.                 </table>
  25.  
  26.             </form>
  27.         </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.
Mar 13 '09 #7
blyxx86
256 Contributor
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.
Mar 13 '09 #8
Markus
6,050 Recognized Expert Expert
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.
Mar 14 '09 #9
blyxx86
256 Contributor
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!
Mar 16 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
8265
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 /htdocs/nuke/html/mainfile.php on line 16 PHP Notice: import_request_variables(): No prefix specified - possible security hazard in
3
6115
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 getting values from a form submission, the book says:
3
59770
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 at work but it has difficulty
4
4149
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 others. It would therefore seem prudent, before using this variable, to do something like: if (typeof (window.outerHeight) != "undefined") { do stuff that refers to this variable } else { work around the fact that the variable isn't defined }
6
1826
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: onclick="myFunction(12345)"
2
3440
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: http://www.torusdesign.nl/spry/test.php?folder=schilderijen/vrij_werk&navColor=SchilderijenNAV This is the code: <?php echo $folder; ?> <?php echo $navColor; ?>
1
7578
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 in C:\wamp\www\cbmall\index.php on line 7 Warning: mysql_connect() : Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\cbmall\index.php on line 7 Error connecting to database server: Access denied for user...
0
8009
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8432
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8428
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8078
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6753
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5456
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3919
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3964
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1548
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.