1

I have a drop down menu

<?php
    for ($i=1; $i<=52; $i++)
    {
        ?>
            <option value="<?php echo $i;?>"><?php echo $i;?></option>
        <?php
    }
?>

once a user has selected a number I need it to return that number of empty forms- with its number contained inside it. can anyone help?

st3ph2010
  • 11
  • 5

3 Answers3

0

You will need two PHP files : in one you get the number, in the second file you generate the fields :

number_of_fields.php

<?php
// SESSION IS NECESSARY TO PASS DATA BETWEEN PAGES.
session_start();
?>
<html>
  <head>
    <title>By José Manuel Abarca Rodríguez</title>
  </head>
  <body>
<!-- FORM TO SEND THE CHOSEN NUMBER TO THE PHP SCRIPT. -->
    <form method="post" action="empty_fields.php" >
      <select name="my_select">
<?php
    for ($i=1; $i<=52; $i++)
    {
        ?>
            <option value="<?php echo $i;?>"><?php echo $i;?></option>
        <?php
    }
?>
      </select>
      <input type="submit" value="Generate empty fields"/>
    </form>
<?php
// IF IT'S COMING BACK FROM PHP SCRIPT, DISPLAY THE DATA.
if ( IsSet( $_SESSION["empty_fields"] ) )
   { echo "Empty fields generated:" . 
          "<br/>" .
          $_SESSION["empty_fields"];
     unset( $_SESSION["empty_fields"] );
   }
?>
  </body>
</html>

empty_fields.php

<?php
session_start();
$fields = (int) $_POST["my_select"]; // NUMBER SENT FROM FIRST PAGE.
$my_str = "";
for ( $i = 0; $i < $fields; $i++ )
  $my_str .= "<input type='text' value='" . ($i+1) . "' /><br/>";
$_SESSION["empty_fields"] = $my_str; // SAVE DATA HERE FOR FIRST PAGE.
header( "Location: number_of_fields.php" ); // BACK TO FIRST PAGE.
?>

Save previous codes in two files with the given names in your www folder. Open your browser and run localhost/number_of_fields.php

  • Thank you. That is brill!!!! is there a way the fields can appear when the number is hovered over? as i will have several of these drop down menus on one page and i do not want the previous data to be lost when switching from pages – st3ph2010 Mar 27 '15 at 18:23
  • Right now we are sending a form, by pressing the SUBMIT button. To use hover we would need to add the "onmouseover" event to the submit button, then create a JavaScript method that is called when onmouseover executs, this JavaScript method will submit the form programmatically. – Jose Manuel Abarca Rodríguez Mar 27 '15 at 18:32
  • could you help me with that. the previous answer was fantastic – st3ph2010 Mar 27 '15 at 19:19
  • I am posting another answer with onmouseover (you may upvote both! :) – Jose Manuel Abarca Rodríguez Mar 27 '15 at 19:41
  • Hi. Thank you so much. to be able to use more than one drop down do i just change names so all dropdown menus may have different values. You have been a brilliant help!! – st3ph2010 Mar 27 '15 at 19:53
  • Remember to use different $_SESSION variables for each dropdown. – Jose Manuel Abarca Rodríguez Mar 27 '15 at 19:54
0

I have modified the code to make it work with onmouseover. Both files changed :

number_of_fields.php

<?php
session_start();
// FIND OUT IF PAGE IS COMING BACK FROM EMPTY_FIELDS.PHP.
if ( IsSet( $_SESSION["empty_fields"] ) )
     $fields = $_SESSION["empty_fields"];
else $fields = "";
?>
<html>
  <head>
    <title>By José Manuel Abarca Rodríguez</title>
    <script type="text/javascript">

// SENDS FORM PROGRAMMATICALLY.
function send_form () {
document.getElementById( "frm" ).submit();
}
    </script>
  </head>
  <body>
    <form method="post" action="empty_fields.php" id="frm">
      <select name="my_select">
<?php
// DISPLAY SELECT OPTIONS.
for ( $i=1; $i<=52; $i++ )
{ if ( $i == $_SESSION["selected_number"] )
       $sel = "selected";
  else $sel = "";
  echo "<option value='" . $i . "' " . $sel . ">" . $i . "</option>";
}
?>
      </select>
      <input type="submit" value="Generate empty fields" onmouseover="send_form();"/>
    </form>
<?php
// DISPLAY INPUT FIELDS (IF IT IS COMING FROM EMPTY_FIELDS.PHP.
if ( strlen( $fields ) > 0 )
   { echo "Empty fields generated:" . 
          "<br/>" .
          $_SESSION["empty_fields"];
     unset( $_SESSION["empty_fields"] );
   }
?>
  </body>
</html>

empty_fields.php

<?php
session_start();
$fields = (int) $_POST["my_select"];
$my_str = "";
for ( $i = 0; $i < $fields; $i++ )
  $my_str .= "<input type='text' value='" . ($i+1) . "' /><br/>";
$_SESSION["empty_fields"] = $my_str;
$_SESSION["selected_number"] = $fields;
header( "Location: number_of_fields.php" );
?>
0

Probably, you want the code to fire after the page has already loaded and after the user has clicked the drop-down box.

Probably, you want the number of forms to load onto the same page without refreshing the page?

In this case, you cannot use PHP -- it has already finished rendering the page. You need to use javascript (or, better yet, jQuery) in order to interact with the user.

If I understand correctly, the best solution is to use AJAX. Not to worry, it is actually super simple, especially when using jQuery.

Here are some simple examples for getting the basics of AJAX. Do not just review them -- copy the code, and try them yourself. Both jQuery and AJAX are much simpler than you may think.

A simple AJAX example

More complicated example

Populate dropdown 2 based on selection in dropdown 1


Note that you must load the jQuery library, usually in the <head> tags like this:

<head>
    <!-- other stuff in head -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>

If you use a CDN to load jQuery, as in the above example, it is probably already be pre-loaded from other websites.


If you want some fast lessons on jQuery, find free video tuts here:

https://www.thenewboston.com/videos.php?cat=32

or at

http://phpacademy.org

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111