Jump to content

Archived

This topic is now archived and is closed to further replies.

Pineapple

Forms inside while loop

Recommended Posts

Hey guys, very very very new to PHP and I'm stumped on this problem. I have a while loop that pulls the rows out of a database and outputs each one in a different row of a table. Within each table I also have a form to update the information if desired. However, it seems like the way I'm submitting the form, it's only submitting the first form regardless of which submit I select.

My question then is, how do I distinguise one submit button from another? My first thought was to append the iteration number or the id number of the row to the name of the button, but then IF statement doesn't work right.

Any help you guys can offer would be really appreciated. Here'es what I've got so far:

[code]$result = mysql_query("SELECT * FROM schedule",$db);

$count = 0;

while($row = mysql_fetch_array($result)){

$count++;
?>
            <tr>
                <td>
                    <div class="name">
                        <?php echo "$row[name]";?>
                    </div>
                </td>
                <td width="10%">
                    <div class="date">
<?php
    echo "$row[sun_start] - $row[sun_stop]";
                        
    if ($submit) {

    // process form
    echo "$row[id]";

    $sql = "UPDATE schedule SET sun_start = $time WHERE id = $row[id]";

    $result = mysql_query($sql);

    } else{

    // display form

?>
                     <form method="post" action="<?php echo $PHP_SELF?>">
                            <input type="text" value="<?php echo "$row[sun_start]";?>">
                            <select name="time">
                                <option>12:00</option>
                                <option>12:30</option>
                                <option>01:00</option>
                            </select>
                            <br><br>
                            <input type="submit" value="Submit" name="submit">
                        </form>
<?php
}
?>
                    </div>
                </td>
            </tr>
<?php } ?>[/code]

Share this post


Link to post
Share on other sites
i can't really follow the logic of your script without knowing what it is you are attempting to do here, but assuming that the logic is working the way you do want it to, what you need to do is in your submit input tags do this:

<input type="submit" value="Submit" name="submit[!--coloro:red--][span style=\"color:red\"][!--/coloro--][][!--colorc--][/span][!--/colorc--]">

and in your if statement you would do this (assuming that's why you have that $count in there):
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
if ($submit[!--coloro:red--][span style=\"color:red\"][!--/coloro--][$count][!--colorc--][/span][!--/colorc--]) {
.
.
.
}
[/quote]
and you are gonna want to put your $count++ at the end of your while loop not at the beginning cuz the $submit array (like all arrays) starts at zero not one.

Share this post


Link to post
Share on other sites
You know, I didn't see the entire code, but since you have different <form>'s, you don't need to discern between them: the broswer *should* send the selected form only.

Share this post


Link to post
Share on other sites
here's the problem i see in this: say he has

form A
form B
form C

if you put information into the fields in form A but click on form C's button, what happens? nothing from form A gets submitted, right? so pineapple, you are going to have to throw in an additional if statement to check to see if the fields actually have stuff in it.

but I have to ask, why are you doing lots of forms like that? wouldn't it be more efficient just to have 1 form? your if statement does the exact same thing, no matter which form you submit: update the same column in the same row, based on the same variables.

btw, i am assuming that at some point in time before all this in your script, you are doing some kind of

$submit = $_POST['submit'];
$time = $_POST['time'];

right?

and also you have this input field:

<input type="text" value="<?php echo "$row[sun_start]";?>">

since you have no name attached to it, and you don't seem to be doing anything with it, i see no reason for you to be doing it this way. It looks to me like you are just labeling your dropdown for the user. Just simply echo the variable. No need to put it in a text field.

Share this post


Link to post
Share on other sites

×

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.