0

here I have another question, how can we update the database through html/php form with some of the data was populated using dropdown menu?

It is something like this:

User choose to update the asset, and the asset will display the form with the stored data, and they can just directly update the asset using that form again (this is including the dropdown menu).

Hope some of you can get back to me soon. Really appreciate it. Thank you.

Edited:

<form method="POST" name="update" action="update.php">

<h2>EDIT</h2>
<table class="reference" cellspacing="0" cellpadding="0" border="1" width="60%"  id="tablecss">

       <tr>
       <td> Category </td>
       <td> <?php echo '<select name="categoryid">';
       foreach ($category as $data)
       {
       echo '<option'.($row['name']==$data? ' selected' : '').'>'.$data.'</option>';
       }
       echo '</select>'; ?>  </td>
       </tr>

       <tr>
       <td> Brand </td>
       <td> <?php echo '<select name="brandid">';
       foreach ($brand as $data1)
       {
       echo '<option'.($row['name']==$data1? ' selected' : '').'>'.$data1.'</option>';
       }
       echo '</select>'; ?>  </td>
       </tr>

       <tr>
       <td> Location </td>
       <td> <?php echo '<select name="locationid">';
       foreach ($location as $data2)
       {
       echo '<option'.($row['name']==$data2? ' selected' : '').'>'.$data2.'</option>';
       }
       echo '</select>'; ?>  </td>
       </tr>

       <tr>
       <td> Staff </td>
       <td> <?php echo '<select name="staffno">';
       foreach ($staff as $data3)
       {
       echo '<option'.($row['name']==$data3? ' selected' : '').'>'.$data3.'</option>';
       }
       echo '</select>'; ?>  </td>
       </tr>

       <tr>
       <td> Supplier </td>
       <td> <?php echo '<select name="supplierid">';
       foreach ($supplier as $data4)
       {
       echo '<option'.($row['name']==$data4? ' selected' : '').'>'.$data4.'</option>';
       }
       echo '</select>'; ?>  </td>
       </tr>

       <tr>
       <td> Project </td>
       <td> <?php echo '<select name="projectid">';
       foreach ($project as $data5)
       {
       echo '<option'.($row['name']==$data5? ' selected' : '').'>'.$data5.'</option>';
       }
       echo '</select>'; ?>  </td>
       </tr>

      <tr>
            <td> <input type="hidden" name="assetid" value="<?php echo "$assetid"; ?>" > </td>
      </tr>

</table>
              <input type="submit" name="submit" value="Update"  onclick="return confirm('Update this?');">
              <input type="button" value="Back" onclick="history.back();">

</form>

So, basically this can't function well. They didn't capture the stored data, but it can update into the database. Any help?

8
  • 3
    I'm not trying to be an arse but SO isn't a code writing service - to have a better chance of a good answer, I suggest you list what you've tried and what didn't work and why, and list whatever code you have already. if you aren't sure where to start, you should try doing a web search for code samples - the php manual is a great resource too: php.net/manual/en and also the w3c tutorials may fill in some blanks with regards to drop downs: w3schools.com/html. I'm not saying you don't know enough to ask - just provide some evidence that you tried before you asked. Commented Dec 22, 2011 at 10:50
  • Maybe he just needs general idea how to do it? Commented Dec 22, 2011 at 10:52
  • Your update confuses me more than your original post... What is it exactly that you want to do? Whhat is wrong with your code? What is working not like you want it to? What happens and what is supposed to happen instead? What's the focus of your question? How to dynamically populate a form? How to submit a form? How to store the user entries into a DB after they submit the form? How to handle select-values after the submit? Please be more specific with your questions so we can put more effort in answering it than in guessing what it's supposed to mean. Commented Dec 22, 2011 at 11:23
  • @Quasdunk Well, I think your code below is what I need but I have to work on it more. What I want is - update the data in the form of FORM which was used for storing the data. I want the stored data to shows in the FORM when I click edit button, and after edit the FORM, click update button to update the data. My code doesn't correctly capture the "assetid" for asset that I click to update, they grab this 1 same data again & again, & update it into the database. And oh, I can perfectly update the database if there's no dropdown menu. Hope you can understand now. Thank you Commented Dec 22, 2011 at 12:01
  • Alright, thanks for clarification, I think I understand now :) So everything works fine as long there are no <select> elements in the form, right? And when there are some, the assetid field returns always the same value on submit (is it empty or just always the same?), right? Commented Dec 22, 2011 at 12:11

1 Answer 1

1

This is pretty straight forward form processing and you could have found out how to do it with 1 minute of googling...

//formpage.php:

<?php
$data_to_populate_dropdown = array();
//run some sql query or whatever to populate that array
//..or just populate the dropdown statically, then there's no need for this

//if the form has been submitted
$errors = array();
if(isset($_GET['action']) && $_GET['action']=='process') {
  if(isset($_POST['my_dropdown'])) {
    $my_dropdown_value = mysql_real_escape_string($_POST['my_dropdown']);
    //write this to db...
    header('Location: formpage.php'); //redirect to avoid double posting;
  } else {
    $errors[] = 'Some data is missing';
  }
}

if(!empty($errors)) {
  foreach($errors as $error) {
    echo $error;
  }
}
?>


<form action="formpage.php?action=process" method="post">
  <select name="my_dropdown">
    <?php foreach($data_to_populate_dropdown as $key => $value) {
       echo '<option value="'.$key.'">'.$value.'</option>';
    } ?>
  </select>
  <input type="submit" value="OK" />
</form>

This code is not tested and quite abstract, but I think you get the idea.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.