Image

Imagejulisana wrote in Imagephp

This might seem a bit convoluted, so please bear with me.


In my (very limited) spare time, I'm working on a Recipe Database program. It's basically like a big online recipe box that I'll end up using to store all my recipes instead of having to create n logins to all the different recipe sites out there.

So, I'm working on getting a level of functionality just a tad above "rough" and I decided to use JavaScript to help me achieve a certain goal: dynamically adding more fields to create ingredients.

Right now, it doesn't look pretty, but it's going in the direction I want. I've got a page, add_ingredients.php, that using a small JavaScript/AJAXy function to auto-load the fields that allow the user to enter in the ingredient.

These fields are contained in a seperate file, ingredient.php. This file has a recursive link that, when clicked, will display more of the same fields. This can be clicked an infinite number of times, allowing the user (me) to make enough fields on the page to add all the data for the ingredients in before hitting the submit button.



The field names are differentiated from each other using an explicitly created element in $_SESSION. Each display of ingredient.php increases this number.

Now, if I try and echo the session_id() value in add_ingredients.php, nothing displays. Is there a way to send the information to this page? I'm looking for a convenient place to kill the session, and I figure using the submit button is as good of a place as any.


If you actually understood any of what I said, I really do commend you. I'm not sure I even understood it, and I wrote it!

If you want to try out what I was creating, go to http://www.julisana.com/recipes/add_ingredients.php and give that a try.


add_ingredients.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Recipe Database: Add an Ingredient</title>
<script type="text/javascript" src="functions.js"></script>
</head>

<body onload="makerequest('ingredient.php','ingredient')">
<form action="confirm_ingredient.php" method="POST">
<div id="ingredient"></div>
<input type="submit" name="submitIngredient" value="Submit Ingredient(s)" />
</form>
</body>
</html>



ingredient.php
<?
if (!session_start())
{
session_start();
header("Cache-control: private");
$_SESSION['num'] = 0;
echo $_SESSION['num']; //for debugging purposes
}
else
{
echo $_SESSION['num']; //for debugging purposes
$i = $_SESSION['num'];
}
?>

<table width="75%" cellspacing="2" cellpadding="0">
<tr>
<td>Ingredient Amount</td>
<td><input type="text" name="ingredientAmount<?= $i ?>" /></td>
</tr>
<tr>
<td>Ingredient Name</td>
<td><input type="text" name="ingredientName<?= $i ?>" /></td>
</tr>
<tr>
<td>Order in Recipe</td>
<td><input type="text" name="ingredientOrder<?= $i ?>" /></td>
</tr>
<tr>
<td>Notes</td>
<td><textarea name="notes<?= $i ?>" rows="5" cols="40"></textarea>
</tr>
</table>
<p>
<img id="opencloseimg" src="images/plus.gif" alt="" title="" style="border: none; width: 9px; height: 9px;" />
<a href="javascript://" onclick="showIngredient()">Next Ingredient</a>
<? $_SESSION['num']++; ?>
</p>
<div id="ingredient"></div>