Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Use same form snippit for both "new" and edit"

I have been following the Ruby on Rails course here and I've noticed that rails reuses a snippit of code from a form for both the new and edit functions.

Is there a way to do the same for php?

I'm have the below form for my new action, but I'd like to just have a line include 'formForEditAndNew.php' in both my edit and new pages

    <div class = 'row'>
        <div class = 'span1'>
        </div>
        <div class = 'span7'>
            <div class = 'well'>
                <form class="form-horizontal" action = 'addPatchConfirm.php' method = 'post'>
                    <fieldset>

                    <!-- Form Name -->
                    <legend><?php $pageTitle ?></legend>

                    <!-- Text input-->
                    <div class="control-group">
                        <label class="control-label" for="name">Patch name</label>
                        <div class="controls">
                            <input id="name" name="name" type="text" placeholder="" class="input-xlarge">

                        </div>
                    </div>

                    <!-- Textarea -->
                    <div class="control-group">
                        <label class="control-label" for="description">Patch Description</label>
                        <div class="controls">                                       
                            <textarea id="description" name="description"></textarea>
                        </div>
                    </div>

                    <!-- Multiple Checkboxes (inline) -->
                    <div class="control-group">
                        <label class="control-label" for="environemtn">Environment</label>
                        <div class="controls">
                            <label class="checkbox inline" for="environemtn-0">
                                <input type="checkbox" name="environment[com1]" id="environemtn-0" value="Com1">
                                Com1
                            </label>
                            <label class="checkbox inline" for="environemtn-1">
                                <input type="checkbox" name="environment[com2]" id="environemtn-1" value="Com2">
                                Com2
                            </label>
                            <label class="checkbox inline" for="environemtn-2">
                                <input type="checkbox" name="environment[bofa]" id="environemtn-2" value="BofA">
                                BofA
                            </label>
                        </div>
                    </div>

                    <!-- Multiple Checkboxes (inline) -->
                    <div class="control-group">
                        <label class="control-label" for="hlType">High Level Type</label>
                        <div class="controls">
                            <label class="checkbox inline" for="hlType-0">
                                <input type="checkbox" name="hlType[code]" id="hlType-0" value="Code">
                                Code
                            </label>
                            <label class="checkbox inline" for="hlType-1">
                                <input type="checkbox" name="hlType[db]" id="hlType-1" value="DB">
                                DB
                            </label>
                            <label class="checkbox inline" for="hlType-2">
                                <input type="checkbox" name="hlType[wcf]" id="hlType-2" value="WCF">
                                WCF
                            </label>
                        </div>
                    </div>

                    <!-- Select Basic -->
                    <div class="control-group">
                        <label class="control-label" for="type">Detailed Type</label>
                        <div class="controls">
                            <select id="type" name="type" class="input-large">
                                <option>Code - Full Build</option>
                                <option>Code - App Change</option>
                                <option>Code - WCF Change</option>
                                <option>Config - Portal</option>
                                <option>Config - Back Office</option>
                                <option>Config - WCF</option>
                                <option>Config - Portal Logger</option>
                                <option>Database - Scheme Change</option>
                                <option>Database - Logging</option>
                                <option>Integration - SSIS package</option>
                                <option>Integration - Stored Proc</option>
                                <option>Misc</option>
                            </select>
                        </div>
                    </div>
                    <!-- Text input-->
                    <div class="control-group">
                        <label class="control-label" for="tfsID">TFS ID</label>
                        <div class="controls">
                            <input id="tfsID" name="tfsID" type="text" placeholder="12345" class="input-mini">

                        </div>
                    </div>

                    <!-- Select Basic -->
                    <div class="control-group">
                        <label class="control-label" for="release">Release</label>
                        <div class="controls">
                            <select id="release" name="release" class="input-small">
                                <option>6.11.0</option>
                                <option>6.10.1</option>
                                <option>6.10.0</option>
                                <option>6.9.0</option>
                            </select>
                        </div>
                    </div>

                    <!-- Button -->
                    <div class="control-group">
                        <label class="control-label" for="addPatch"></label>
                        <div class="controls">
                            <input type = 'submit' class="btn btn-success" value = 'Add Patch'>
                        </div>
                    </div>

                    </fieldset>
                </form>
            </div>
        </div>
    </div>

Also as a follow up, I made the above form with this site. Does it need to be that long? Or can I trim it down some? I'm very new to bootstrap so I'm not sure what NEEDS to be there, and what is there solely due to a generator

1 Answer

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

PHP has an include statement that will include another file into the current one:

include("path/to/file/formForEditAndNew.php");

Is that what you had in mind?

That's the part I'm familiar with. The part I'm unfamiliar with is what I put in that file so that when I edit the text boxes, checkboxes, and radio buttons are filled in, but when i add new, everything is blank

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

Ah. The code for that can be all the same, you just use blank values. Something like this:

---- new.php (excerpt) ----

$name = "";
include("form.php");

---- edit.php (excerpt) ----

$name = $object->property_that_has_the_right value in it;
include("form.php");

---- form.php (excerpt) ----

<input name="name" type="text" value="<?php echo htmlspecialchars($name); ?>">

I cover how to load values from one place into input fields in a form in my videos on validation errors (Integrating Validation Errors). It's a slightly different context, but it's a similar principle.

Does that help?

I think that helps. if htmlspecialchars($name) is blank, it doesnt get upset or anything? Just returns a blank value?

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

If the variable is not defined, then it could get upset. But if you make sure to create it in the new.php, then it will be fine.