phgdanny
phgdanny

Reputation: 51

Code works fine on it's own, but not when included in my form

I have 2 files: add.php (a form to add a recipe into a database, and numbers.php (a list for displaying ingredient options).

Both scripts/pages work separately...but when I include ('numbers.php') the onchange code does not work.

//numbers.php
<form name="numbers" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="select" onchange="javascript: document.numbers.submit();">
  <option value=0>-</option>
  <option value=1>1</option>
  <option value=2>2</option>
  <option value=3>3</option>
  <option value=4>4</option>
</select>
</form>
<?php
$num_ingredients = $_REQUEST["select"];
$count = 0;
while ($count < $num_ingredients) {
  $count++;
  include ('../ingredients/list.php');
  echo '<br />';
}
?>

//add.php
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Recipe Title:
  <input type="text" name="recipe_name" maxlength="30" value="<?php echo $recipe_name; ?>" /><br />
Ingredients:
  <?php include ('numbers.php'); ?><br />
<input type="submit" value="Add Recipe" />
<input type="button" onclick="history.go(0)" value="Clear" /><br />
</form>

Any help is greatly appreciated, as always :)

------- EDIT --------------------------------------

Here is the page source from add.php as requested


<!DOCTYPE html>
<html lang="en-US">
<head>
<meta http-equiv="Content Type" content="text/html; charset=UTF-8" />
<title>POWERCHEF</title>
  <meta name="robots" content="noindex, nofollow">
  <meta name="distribution" content="iu">
  <meta name="format-detection" content="telephone=yes">
  <meta name="copyright" content="Copyright 2012">
</head>
<body>
<!-- ON WITH THE SHOW --><h3>ADD RECIPE</h3><html>
<form enctype="multipart/form-data" action="/kitchen/recipes/add.php" method="POST">

Recipe Title:
  <input type="text" name="recipe_name" maxlength="30"
  value="" /><br />
Subtitle:
  <input type="text" name="recipe_subtitle" maxlength="50"
  value="" /><br />
Category:
  <input type="text" name="recipe_category" maxlength="50"
  value="" /><br />
Subcategory:
  <input type="text" name="recipe_subcategory" maxlength="50"
  value="" /><br />
  <br />
Photo:<br />
  <input type="hidden" name="MAX_FILE_SIZE" value="200000" />
  <input type="file" name="recipe_photo" value="" /><br />
  <br />
Ingredients:<br />
  <form name="numbers" method="post"
  action="/kitchen/recipes/add.php">
<select name="select" onchange="javascript: document.numbers.submit();">
  <option value=0>-</option>
  <option value=1>1</option>
  <option value=2>2</option>
  <option value=3>3</option>
  <option value=4>4</option>
  <option value=5>5</option>
  <option value=6>6</option>
  <option value=7>7</option>
  <option value=8>8</option>
  <option value=9>9</option>
  <option value=10>10</option>
  <option value=11>11</option>
  <option value=12>12</option>
  <option value=13>13</option>
  <option value=14>14</option>
  <option value=15>15</option>
  <option value=16>16</option>
  <option value=17>17</option>
  <option value=18>18</option>
  <option value=19>19</option>
  <option value=20>20</option>
  <option value=21>21</option>
  <option value=22>22</option>
  <option value=23>23</option>
  <option value=24>24</option>
  <option value=25>25</option>
  <option value=26>26</option>
  <option value=27>27</option>
  <option value=28>28</option>
  <option value=29>29</option>
  <option value=30>30</option>
</select>
</form>
<br />
<br />
Prep Instructions:<br />
  <textarea name="recipe_prep_instructions" class="" cols="88" rows="15"
  value="" /></textarea><br />
  <br />
Cooking Instructions:<br />
  <textarea name="recipe_prep_instructions" class="" cols="88" rows="15"
  value="" /></textarea><br />
  <br />
<input type="submit" value="Add Recipe" />
<input type="button" onclick="history.go(0)" value="Clear" /><br />
<br />
</form>
</html>
<footer>
  <p>
    <small><strong>&copy; 2012 </strong> -&nbsp;All Rights Reserved</small>
  </p>
</footer>
<!-- THANKS FOR STOPPING BY! -->
</body>
</html>

Upvotes: 0

Views: 110

Answers (2)

John V.
John V.

Reputation: 4670

You have a typo:

<input type="text" name="recipe_name" maxlength="30" value="<?php echo $recipe_name"; ?>" /><br />

Should be: (notice the removal of the quote in the php block)

 <input type="text" name="recipe_name" maxlength="30" value="<?php echo $recipe_name; ?>" /><br />

The quote could possibly be messing up the other script.

EDIT: Ok, was this also just a copy paste typo?

<form name="numbers" method="post action="<?php echo $_SERVER['PHP_SELF']; ?>">

Should be:

<form name="numbers" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

EDIT2: Ok, the problem is that the form "numbers" is inside the other form, so it's having trouble accesing it with document.numbers. To fix it you are going to need to access it a different way, such as giving it an id and using getElementById.

Upvotes: 2

phgdanny
phgdanny

Reputation: 51

the form on add.php did not have a name

Upvotes: 0

Related Questions