Jump to content


Photo

Forms inside while loop


  • Please log in to reply
3 replies to this topic

#1 Pineapple

Pineapple
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 13 June 2006 - 11:47 PM

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:

$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 } ?>


#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 14 June 2006 - 04:28 AM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 14 June 2006 - 04:40 AM

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.
~ D Kuang

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 14 June 2006 - 04:54 AM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users