Jump to content


Photo

Help - new to php


  • Please log in to reply
5 replies to this topic

#1 Teldosh

Teldosh

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 24 March 2013 - 11:05 AM

<form action="test.php" method="POST">
<br>
<?php
    
    for ($i=1; $i<=6; $i++){
        echo $i.'<input type="text" name="option_"> '.'<br>';
        
  }
?>
 <br>

<input type="submit" name="submit" value="submit">

<br><input type="text" size ="2" name="add_field"> <input type="submit" name="add" value="add">
    Add extra boxes
    <?php
              $select = array();
                 if ($_POST['submit']){
                    for ($x = 1; $x<= 6; $x++){
                 if ($_POST["option_$x"]){
               array_push($select, $_POST["option_$x"]);
             }
        }
    if (count($select)){
    echo '<br>'.'<br>'.'<br>'."The Decider has choosen: " .'<p>'.$select[rand(0,count($select) - 1)].'</p>';
          }
    }
    ?>
</form>

 

Hi, I have 6 textfeilds and a submit button. When someone enters several values into the textfield and selects submit it will display a random result from one of the 6 textfields. I have also another textfeild and submit button. This is to add more textfields.
At the moment it is only displaying the result from the 6th textfield as I do not know how to correctly store the results into an array and also im unsure on how to add extra textfields. can anyone help please?



#2 davidannis

davidannis

    Freak Dr.

  • Members
  • PipPipPip
  • 611 posts
  • LocationOkemos, MI USA

Posted 24 March 2013 - 11:25 AM

You can have multiple field with the same name by using []

<input type="text" name="myname[]"

you can fill the brackets with your values of $i or leave it blank.



#3 davidannis

davidannis

    Freak Dr.

  • Members
  • PipPipPip
  • 611 posts
  • LocationOkemos, MI USA

Posted 24 March 2013 - 11:26 AM

or you can do what you tried with

echo $i.'<input type="text" name="option_'.$i.'">'

Edited by davidannis, 24 March 2013 - 11:26 AM.


#4 Teldosh

Teldosh

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 24 March 2013 - 11:42 AM

Thanks alot, that solves my problem. I was trying something similar but was not using the seperators so I was just recieving error messages. How can I type an integer into "add_field" and when submitted it adds more textfields?



#5 davidannis

davidannis

    Freak Dr.

  • Members
  • PipPipPip
  • 611 posts
  • LocationOkemos, MI USA

Posted 24 March 2013 - 01:49 PM

$maxfields=intval($POST['add_field']);
    for ($i=1; $i<=$maxfields; $i++){
        echo $i.'<input type="text" name="option_"> '.'<br>';

with html that looks like:

<input name="add_field" type="text">

Edited by davidannis, 24 March 2013 - 01:50 PM.


#6 davidannis

davidannis

    Freak Dr.

  • Members
  • PipPipPip
  • 611 posts
  • LocationOkemos, MI USA

Posted 24 March 2013 - 01:52 PM

I'd consider using a <select> to input the integer instead of a text field.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com