Jump to content

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]
Link to comment
https://forums.phpfreaks.com/topic/11933-forms-inside-while-loop/
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.
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.
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.