Jump to content

how do I set a default variable from an edit form that is using php array list


jwmiller

Recommended Posts

I have a simple form that edits student information.  I have several fields that I have a drop down list pulled as an array from a separate table.  I would like the information that is already entered on the student to be the default but still have the drop down list available if the item was entered in error. For example:  Drop down list of all the states of America, OH was entered for the student, when the edit form comes up for State in Address I would like OH to be the default but still have the list to choose if OH was entered in error. I have tried selected = $selected but then the entire drop down changed to OH.  I am very, very much a beginner in this

 

<?php

  // list of possible modules comes from database

 

          $state_array=get_states();

 

 

          //display as a drop down list for end user to choose

  if (!is_array($state_array))

{

echo "<p>No States are currently available<p>";

return;

}

 

             

 

          foreach ($state_array as $row)

  {

              echo "<option value=\"".($row['stateid'])."\"";

              // if existing state, put in current catgory

              echo "<strong>  ".($row['stateid'])."</strong><br />";

              echo "<option select=\"selected\">OH";

          }

   

  ?>

Link to comment
Share on other sites

<?php

$c = 3;

$a = array(
1 => 'Option 1',
2 => 'Option 2',
3 => 'Option 3',
4 => 'Option 4',
);

echo '<select name="name">';

foreach ($a as $k => $v )
echo '<option value="'.$k.'"'.($k == $c ? ' selected="1"':'').'>'.$v.'</option>';
echo '</select>';

?>

Link to comment
Share on other sites

Thank you for your reply, I am still not real clear but I will copy your code and see if I can make it work - so i can understand your logic.

 

I was not real clear in MY coding - the OH is really coming over as a variable $studstate - I just hard coded it in the loop to see if I could get anything to default to the information that is already stored in the students database.

 

Thanks again

 

Link to comment
Share on other sites

I ran your code and that is exactly what I need to happen, however I am stuck trying to translate the way you wrote your code to my way

 

So if I have the variable returned $studstate (this has OH stored in this field in the students table and I returned that text and stored it in $studstate)

I am also getting the table from the states table and returning as an array

See below

How do I translate your option set up :

foreach ($a as $k => $v )

echo '<option value="'.$k.'"'.($k == $c ? ' selected="1"':'').'>'.$v.'</option>';

to minic my set up below.  That is now where I am stuck.  I am getting confused between my [] and your => as well as where exactly I can place  -- as well as create the ($row==$studstate ? selected ) statement

$studstate='OH' 

foreach ($state_array as $row)

  {

              echo "<option value=\"".($row['stateid'])."\"";

              // if existing state, put in current catgory

              echo "<strong>  ".($row['stateid'])."</strong><br />";

           

          }

Link to comment
Share on other sites

HA, got it THANK YOU SO MUCH.  This was the piece of the puzzle I needed.  This was my final echo option statement

 

              echo '<option value="'.($row['stateid']).'"'.(($row['stateid'])==$studstate ? 'selected="1"':'').'>';

 

and it is exactly what I needed. :D

 

So How do I mark this as solved?

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.