Results 1 to 9 of 9

Thread: <input> in a while loop

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    <input> in a while loop

    PHP Code:
    <?php if($_POST['rowPlus']){ $totalRows++;}?>
            <form method="post">
            <input type="submit" class="box" name="rowPlus" value="NewRow" />
            <input type="hidden" name="totalRows" value="<?php echo $totalRows?>" />
            </form>    <?php echo $totalRows; } ?>
    PHP Code:
    <form action="" methord="Post">
              <table width="98%" border="1" cellpadding="2" cellspacing="2">
    <?php $rowR=0; while($rowR $totalRows){  
                
    $deleteRowKey=$_REQUEST['deleteRow'];
                
    $deleteRow=array_flip($deleteRowKey);
                
    $amount=$_GET['amount'];?>
                <tr>
                  <td align="center" valign="top" class="medium"><?php 
                                
                  
    echo "T";
                  if(
    $amount[$rowR] > 1){ echo ($newRowR 1)." - "$newRowR=$newRowR+$amount[$rowR];}else{$newRowR++;}
                  echo 
    $newRowR;
                   
    ?></td>
                  <td align="center" valign="top" class="medium"><input name="amount[<?php echo $rowR?>]" type="text" value="<?php echo $amount[$rowR]; ?>" size="2" /></td>
                  <td align="center" valign="top" class="medium"><?php print_r ($_GET['amount']); ?></td>
                  <td align="center" valign="top" class="medium">
                  <input type="submit" name="deleteRow[<?php echo $rowR ?>]" value="X" />
                  </td>
                  <td align="center" valign="top" bgcolor="#CCCCCC" class="medium">&nbsp;</td>
                </tr><?php $rowR++;} ?>

    </table></form>
    This Is a part of my html page i'm having trouble with. I'm looking to delete a row from my table, I know I can adapt my code to do this. The error I am having is when I place somthing in a text box 'amount[]' and press enter the first button 'deleteRow[0]' also gets submitted and so the first row will get deleted if I put in my code?!

    I'm not looking for anything to complicated just some logic on how to hide or not send (miss) the first button so its not shown.

    Thanks

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: <input> in a while loop

    uhh. if you don't want the first button to show -at all- on the form, just check what $rowR is and don't do anything if it's equal to 0?

    PHP Code:
                <tr>
                  <td align="center" valign="top" class="medium"><?php
                                
                  
    echo "T";
                  if(
    $amount[$rowR] > 1){ echo ($newRowR 1)." - "$newRowR=$newRowR+$amount[$rowR];}else{$newRowR++;}
                  echo 
    $newRowR;
                   
    ?></td>
                  <td align="center" valign="top" class="medium"><input name="amount[<?php echo $rowR?>]" type="text" value="<?php echo $amount[$rowR]; ?>" size="2" /></td>
                  <td align="center" valign="top" class="medium"><?php print_r ($_GET['amount']); ?></td>
    <?php if($rowR != 0): ?>
                  <td align="center" valign="top" class="medium">
                  <input type="submit" name="deleteRow[<?php echo $rowR ?>]" value="X" />
                  </td>
    <?php endif; ?>
                  <td align="center" valign="top" bgcolor="#CCCCCC" class="medium">&nbsp;</td>
                </tr>
    I'm not sure if this is what you're asking for, though.

    from what you explained, the only thing I can think of is when you hit enter when you're editing, say, the 6th item, the 0th item is being submitted?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: <input> in a while loop

    Thanks for your reply. Your quite right what I had in mind isn't going to work Doh! My problem is that I've managed to identify which row the 'X' button is clicked on but if I input something in to a text box an submit the first 'X' box also gets submitted!

    If I change the input type to hidden (before the first button it doesn't get picked-up, after the button and it I cann't identify which button)It doesn't help!?

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: <input> in a while loop

    I'm still not completely understanding the problem you're having. but I'll give it a go!

    it looks like you're using one <form> for this entire debacle, right? well, that could explain what's going on. so, what's wrong with using multiple forms? you obviously want data being submitted on the 6th line not to include anything on the 0th line, so do that. just move your <form> tag and </form> tag within your while loop -- or is this not something you can do?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: <input> in a while loop

    All the information is in one table / one form, I think this is needed because if a rows height is expanded (adding things to the other columns) everything in the row stays aligned with each other.

    I could put a form in each cell?? But I can't see that improving my problem? Just making my code harder to read

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: <input> in a while loop

    having multiple <form> tags for every cell, every row, or every table won't change how the table displays.

    I suggested this because you're saying that button[0] is showing as submitted when you're submitting button[6]. this is weird and shouldn't be happening, and I have no idea why it is happening -- I can't just take the incomplete script you posted and run it and see what's happening. but I did take some of your script and modified it so that it would run, and didn't experience what you're describing. I typed a value into an input[4] and hit button[4] and amount[4] and deleterow[4] were both set, no deleterow[0]. then, I typed something in both input[0] and input[4] and hit button[4] and had the same result but with amount[0] defined as well (but still, no deleterow[0]).

    if they were all on different forms, then this wouldn't have any chance of happening.

    also, I just realized that on your <form> tag you spelled method wrong (as methord). this is making your form submit as get instead of post.

    I'm not sure where else to go with this.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: <input> in a while loop

    Sorry,

    If i submit with a button it records which button I'm using OK.
    The problem is when i enter something in one of the text boxes and hit enter.

    The whole form gets sent which means that not only do I get to record what has been entered into the text box but also the first button gets sent as being pressed.

    This isn't good because I'm looking to turn the buttons into delete buttons which will delete the first row every time I enter a value in a text box.

    Doh!

    I was wondering why the form method was sending as GET. I think I'll keep it an just tidy-up the code to read properly.

    Thanks.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: <input> in a while loop

    I'm finding that with multiple other buttons on the same form I can single out what is being pressed and what isn't.

    Is there an alternative to <textarea> that could be a better option?

    ! I could Include A submit button that would mean I wouldn't have to use the [return key] in the textbox to submit the form !

    Can I submit a form with a button thats not on the form (being submitted)?

    Thanks!
    Last edited by LingoOutsider; Jul 29th, 2009 at 06:25 AM.

  9. #9
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: <input> in a while loop

    you might be able to force it to submit via javascript but I've never tried.

    I still don't see why you can't just make separate forms for every row. you don't want the entire form being sent, you want just one. if the form only has two elements, then if you have input[1] selected and hit enter while you're typing into it, button[1] will be submitted no matter what. the only values that would be submitted then are amount[1] and deleterow[1]. it sounds like it would be a whole lot easier than what you're trying to think up:

    PHP Code:
    <table>
    <?php for($i 0$i 6$i++): ?>
      <form action="whatever.php" method="post">
        <tr>
          <td><?php echo $i?>
          <td><input type="text" name="amount[<?php echo $i?>]" /></td>
          <td><Input type="submit" name="deleterow[<?php echo $i?>]" value="X" /></td>
        </tr>
      </form>
    <?php endfor; ?>
    </table>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width