简体   繁体   中英

Use PHP to parse HTML page to get selected value from a dropdown list. Is it possible?

I have a program that uploads a .csv file of golf scores. upon upload the data from the file is diplayed in the form of a html table in the browser. the code for this is shown below.

echo '<div class="tablediv"><table><form method="post" action="new.php" >';


while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if($ctr ==1)
        {
        echo '<tr>';
        echo "<th>" . $data[3] . "</th><th>" .  $data[4] .
        "</th><th>  " .     $data[5] . "</th><th> " . $data[6].
        "</th><th>" . $data[7] ."</th><th> Prize </th>" ;
        echo '</tr>';
        $ctr++ ;
        }
        else if ($ctr > 1) 
        {
        echo '<tr>';
        echo "<td>" .$data[9] . "</td><td>" .  $data[10] .
       "</td><td>" . $data[11] . "</td><td>  " . $data[12]. "</td><td>" . $data[13] . "</td><td><select name='dropdown'>
        <option value='empty'>None Allocated</option>
        <option value='firstprize'>First Prize</option>
        <option value='secondprize'>Second Prize</option>
        <option value='thirdprize'>Third Prize</option>
        </select></td>";
        echo '</tr>';
        }

    }
    echo '<input type="submit" name="submit" value="Submit" class="submit1" /></form></table></div></br>';

fclose($handle);
}

once a user has uploaded the file and it is diplayed I want the user to be able to allocate a prize via the dropdown list and then, once this is done the data can be commited to a database. what i need to do is basically get the selected prize position also and commit that to the database along with the other data. im just not too sure how to get at the selected value in the dropdown list. would the best way to do this be to use PHP simple HTML DOM parser? or does anyone know of an easier way. Thanks a lot for your help in advance!

the initial upload of the file looks like below. last column being the dropdownlist

 Player name    H'cap   Score   Front   Back    Prize
 name           18        66    33.0    33.0    none allocated
 name           11        67    33.5    33.5    none allocated
 name           4         67    33.0    34.0    none allocated
 name           12        67    31.0    36.0    none allocated
 name           11        68    34.5    33.5    none allocated
 name           6         68    34.0    34.0    none allocated
 name           19        68    31.5    36.5    none allocated
 name           11        69    36.5    32.5    none allocated
 name           11        69    33.5    35.5    none allocated

I guess yes! You can do kind of this way:

echo "<td>" .$data[9] . "</td><td>" .  $data[10] .
       "</td><td>" . $data[11] . "</td><td>  " . $data[12]. "</td><td>" . $data[13] . "</td><td><select>
        <option value='empty'", ($prize == "None") ? ' class="selected"' : '',">None Allocated</option>
        <option value='firstprize'", ($prize == "First Prize") ? ' class="selected"' : '',">First Prize</option>
        <option value='secondprize'", ($prize == "Second Prize") ? ' class="selected"' : '',">Second Prize</option>
        <option value='thirdprize'", ($prize == "Third Prize") ? ' class="selected"' : '',">Third Prize</option>
        </select></td>";

As @Carsten said, you don't need to overcomplicate this. You can easily get the value from a dropdown:

<form action="page.php" method="post">
<select name="example">
        <option value='empty'>None Allocated</option>
        <option value='firstprize'>First Prize</option>
        <option value='secondprize'>Second Prize</option>
        <option value='thirdprize'>Third Prize</option>
</select>
</form>

<!-- page.php -->

<?
    echo $_POST['example'];

If you select 'Second Prize' it will echo 'secondprize'

my solution was this. this way i can get the post data on the new.php page by using var_dump($_POST). I create another counter "$counter2" and initialize it to 1. then in append the counter to the dropdownlist name " and var_dump($_POST) gives me an output like this

 array(76) { ["dropdown1"]=> string(11) "secondprize"    
 ["dropdown2"]=> string(5) "empty" ["dropdown3"]=> string(10) "firstprize" 
 ["dropdown4"]=> string(5) "empty" ["dropdown5"]=> string(5) "empty"

here are the changes i made to the function.

    if ($this->fileerror > 0) {
    echo "Error: " . $this->fileerror . "<br />"; 
}else{ 
if (($handle = fopen($this->filetemp, "r")) !== FALSE) {
    $ctr = 1; // used to exclude the CSV header
            $count2 = 1;        
    echo '<div class="tablediv"><table><form method="post" action="new.php" >';

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if($ctr ==1)
        {

        echo '<tr>';
        echo "<th>" . $data[3] . "</th><th>" .  $data[4] . "</th><th>  " . $data[5] . "</th><th> " . $data[6]. "</th><th>" . $data[7] ."</th><th> Prize </th>" ;
        echo '</tr>';
        $ctr++ ;
        }
        else if ($ctr > 1) 
        {
        echo '<tr>';
        echo "<td>" .$data[9] . "</td><td>" .  $data[10] . "</td><td>" . $data[11] . "</td><td>  " . $data[12]. "</td><td>" . $data[13] . "</td><td><select name='dropdown" .  $count2."'>
        <option value='empty'>None Allocated</option>
        <option value='firstprize'>First Prize</option>
        <option value='secondprize'>Second Prize</option>
        <option value='thirdprize'>Third Prize</option>
        </select></td>";
        echo '</tr>';
        $count2++;
        }

    }
    echo '</br><input type="submit" name="submit" value="Submit" /></form></table></div></br>';

fclose($handle);
}

this is a pretty sloppy solution i know. so any other solutions you may have for this would be appreciated. thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM