0

I am unable to figure it out on how to retain select option value after submission. I have looked at various forums and self-help sites:

store drop down options in mysql database, PHP1

http://www.tizag.com/mysqlTutorial/mysqlinsert.php

insert value of html select form into mysql database

Using $_POST to get select option value from HTML

Yet, when refresh the page after selecting the dopwdown options, it wasn't able to retain the last selected value:

Select PHP codes:

$dropdown = elgg_echo('<DIV align="left", >
                    <form method="post" action="Select.php">    
                    <select name="mycustomFile" >
                        <option>Select Value..</option>
                        <option value="A">a</option>
                        <option value="B">b</option>
                        <option value="C">c</option>
                        <option value="D">d</option>
                    </select>
                    <p><input type="submit" value="Submit"</p>
                    </form>
                </DIV>');

2nd Code: Select.php (where I perform $_POST[''] action)

<?php
/**
 * Override the ElggFile
 */
class FilePluginSelect extends ElggObject {
protected function  initializeAttributes() {
    parent::initializeAttributes();
    $this->attributes['customFile'] = "my_select";
}
public function __construct($guid = null) {
    parent::__construct($guid);
}
public function customFile(){

    //method to call on model to allow select option
    //To post select option into mysql database
    $selectOption =$_POST['mycustomFile'];
    if(isset($selectOption)){
        $sql = "INSERT INTO Entries (mycustomFile) VALUES (".$_POST['selectOption'].")";
    }else {  // User selected nothing
    echo 'No options selected!';
    }
}

}

Could someone please help to enlighten me what has gone wrong. Thanks

Community
  • 1
  • 1
developer
  • 17
  • 5

2 Answers2

0

You're doing it completely wrong. You probably just should use Elgg data model and store your value in metadata unless you have very good reason to ignore framework and do stuff on your own.

First of all don't bother extending ElgObject, you're doing it completely wrong and don't really need it.

To retain selection, you have to read the value and mark tag with selected attribute.

You need to update also action of saving file to include new field value. To save it to metadata you just need to use EAV interface of the entity (you'll find example in http://learn.elgg.org/en/1.12/tutorials/blog.html). Than you can read it within the view you're extending

As for the select element itself, you should use existing input/dropdown view instead. It will save you some boilerplate code. Also no need to add tag as you should be extending existing form view. Views documentation is here: http://learn.elgg.org/en/1.12/guides/views.html

I strongly recommend starting your search in Elgg documentation first. Elgg does have some learning curve, but you want to learn the proper way for your code to be maintainable in future.

Paweł Sroka
  • 428
  • 2
  • 10
  • I am completely confuse with what you are trying to say – developer Aug 21 '15 at 03:39
  • so are you saying that I do not need to extend an ELGGObject and no need for me to instantiate an attribute? Hence, I would have to make use of actions/ exxtension such as elgg_extract to retrieve the values from the dropdown list and append it to a declared array? – developer Aug 21 '15 at 03:45
  • now when i use the existing input/select view, it gives me a blankscreen. `$params = array( 'name' => 'params[value_display]', 'value' => $value_display, 'options'=> array(A, B, C, D), ); $display_dropdown = echo elgg_view('input/select' $params);` – developer Aug 21 '15 at 04:18
-1

set mycustomFile to mycustomFile[] . Also don't post anything into the database without clearing it, its very dangerous.

update:

Your php for getting the value of the select form is correct. Your script isn't working for another reason. Here is a sample of retrieving the select fields data.

<?php
    if(isset($_POST['button']))
    {
        echo 'select value: ' . $_POST['something'];
    }
?>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
            <select name="something">
                <option value="a">some a</option>
                <option value="b">some b</option>
                <option value="c">some c</option>
            </select>
            <button name="button">
                click
            </button>
        </form>
    </body>
</html>
Exploit
  • 6,278
  • 19
  • 70
  • 103
  • i set that in both at select.php and in the select option list? – developer Aug 20 '15 at 02:44
  • i think it has to do with $this->attributes['customFile'] = "my_select"; change customFile to mycustomFile – Exploit Aug 20 '15 at 02:45
  • For retrieving the data, do I put it under the `if(isset($selectOption))` method? and do I still use the following code: `if(isset($selectOption)){ $sql = "INSERT INTO Entries (mycustomFile) VALUES (".$_POST['selectOption'].")"; //Echo the confirmation of selected options echo 'Selected Value:

    '; foreach($selectOption as $key => $selectOption){ echo $selectOption . "
    "; } }else { // User selected nothing echo 'No options selected!'; }`
    – developer Aug 20 '15 at 03:18