Jump to content

Form fields in PHP code


shaung
Go to solution Solved by AJinNYC,

Recommended Posts

Hello,

 

I have these form fields in HEREDOC syntax but I need to add more PHP which isn't possible in heredoc.  I wish to remove the heredoc for this section, but I suck at echoing form fields.  Could someone help me with this?

        foreach ($arrValues as $row)
{
   $id = $row['employee_id'];
   echo <<<FORM_FIELDS
            <tr>
                <td><input type="text" name="record[$id][firstName]" value="{$row['first_name']}" readonly /></td>
                <td><input type="text" name="record[$id][lastName]" value="{$row['last_name']}" readonly /></td>
                <td><input type="text" name="record[$id][height]" value="{$row['height']}" readonly /></td>
                <td><input type="text" name="record[$id][cap]" value="{$row['cap']}" readonly /></ td>
                <td><input type="text" name="record[$id][color]" value="{$row['colors']}" readonly /></td>


                        <td><select name="record[$id][degree]" </td>
                        <option> "Filler" </option>
                        </select> 


                        <td><select name="record[$id][school]" </td> 
                            <option>{$row['name']}</option> 
                        </select> 
                    
                <td><input type="submit" name="update" value="Update" /></td>
            </tr>
FORM_FIELDS;
}

What I need to do is populate the dropdown option boxes from the DB but I cannot use a loop within the heredoc blocks.  Apparently PHP loops are not possible within heredoc.   I would prefer to just echo everything out, but with my limited PHP skills, I keep making a mess of it.

 

Can anyone help me?

 

Thanks!

Link to comment
Share on other sites

if i remember correctly, you want to pre-select the option for the currently displayed record. seems that is what this previous post did - http://forums.phpfreaks.com/topic/284692-populate-an-arrayed-dropdown-when-clicked/

 

you would form the option list in a php variable before the start of the heredoc statement and just put the php variable in at the correct point.

Link to comment
Share on other sites

  • Solution

One issue I see is it looks like you're missing your closing bracket on your select elements. 

 

 

It's not too hard to echo out. Just use single quotes, around the HTML block, and then you have to "breakout" of the single quotes because variables inside single quotes are treated like strings/plain-text.

 

For example: echo 'Some Text '.$variable.' Some more text';

 

You can think of the quotes as thread and the periods (or more technically concatenating) as needle holes... you're stitching together the variable, and the text after the variable, to the first bit of text.

 

So using the above method on your code, you end up with this.

        foreach ($arrValues as $row)
{
   $id = $row['employee_id'];
   echo '<tr>
                <td><input type="text" name="record['.$id.'][firstName]" value="'.$row['first_name'].'" readonly /></td>
                <td><input type="text" name="record['.$id.'][lastName]" value="'.$row['last_name'].'" readonly /></td>
                <td><input type="text" name="record['.$id.'][height]" value="'.$row['height'].'" readonly /></td>
                <td><input type="text" name="record['.$id.'][cap]" value="'.$row['cap'].'" readonly /></ td>
                <td><input type="text" name="record['.$id.'][color]" value="'.$row['colors'].'" readonly /></td>


                        <td><select name="record['.$id.'][degree]"> </td>
                        <option> "Filler" </option>
                        </select> 


                        <td><select name="record['.$id.'][school]"> </td> 
                            <option>'.$row['name'].'</option> 
                        </select> 
                    
                <td><input type="submit" name="update" value="Update" /></td>
            </tr>';
}

If you don't know it, the definition of concatenating is to: link (things) together in a chain or series.

Edited by AJinNYC
Link to comment
Share on other sites

One issue I see is it looks like you're missing your closing bracket on your select elements. 

 

 

It's not too hard to echo out. Just use single quotes, around the HTML block, and then you have to "breakout" of the single quotes because variables inside single quotes are treated like strings/plain-text.

 

For example: echo 'Some Text '.$variable.' Some more text';

 

You can think of the quotes as thread and the periods (or more technically concatenating) as needle holes... you're stitching together the variable, and the text after the variable, to the first bit of text.

 

So using the above method on your code, you end up with this.

        foreach ($arrValues as $row)
{
   $id = $row['employee_id'];
   echo '<tr>
                <td><input type="text" name="record['.$id.'][firstName]" value="'.$row['first_name'].'" readonly /></td>
                <td><input type="text" name="record['.$id.'][lastName]" value="'.$row['last_name'].'" readonly /></td>
                <td><input type="text" name="record['.$id.'][height]" value="'.$row['height'].'" readonly /></td>
                <td><input type="text" name="record['.$id.'][cap]" value="'.$row['cap'].'" readonly /></ td>
                <td><input type="text" name="record['.$id.'][color]" value="'.$row['colors'].'" readonly /></td>


                        <td><select name="record['.$id.'][degree]"> </td>
                        <option> "Filler" </option>
                        </select> 


                        <td><select name="record['.$id.'][school]"> </td> 
                            <option>'.$row['name'].'</option> 
                        </select> 
                    
                <td><input type="submit" name="update" value="Update" /></td>
            </tr>';
}

If you don't know it, the definition of concatenating is to: link (things) together in a chain or series.

 

OK, I'm starting to see what is going on now.  It's just like one long string.  

 

So I should be able to flip the 'readonly' to write by putting it in a variable and just setting it to an empty string as needed...  thanks a lot.

 

I have programmed for  years off and on, just haven't done it in about 3 years and have never done web programming/scripting before.  Appreciate everyone's help.

Edited by shaung
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.