0

I am using redbean to create my site and I am currently implementing a recipe upload feature for users.

In the 'upload recipe' form I have used javascript to dynamically add 4 input fields together (amount, measurement, ingredient, notes). I don't however know how to use redbean to store these once the user clicks submit. Could anyone suggest something? Would I have to use a loop to loop through each field added and store it to the database?

Here is the code I am using:

var count = 0;

$(function(addfield){
    $('p#add_field').click(function(){  
        count += 1;  
        $('#container').append(   
            '<strong>Ingredient ' + count + '</strong><br />'    
                + '`<`input id="field_' + count + 'name="amount" type="text" placeholder="Amount" `/>`'     +  
                 '<input id="field_' + count + 'name="measurement" type="text" placeholder="Measurement" `/>`'+   
                 '<input id="field_' + count + 'name="ingredient" type="text" placeholder="Ingredient" `/>`' +  
                 '<input id="field_' + count + 'name="notes" type="text" placeholder="Notes" `/>`' +   
        );  
    });  
});

And this is the link to call the function:

<p id="add_field"><a href="#addfield"><span>&raquo; Click to add ingredient</span></a></p>
GargantuChet
  • 5,691
  • 1
  • 30
  • 41
MikeHolford
  • 1,851
  • 2
  • 21
  • 32
  • It's not clear how the use of a loop factors in. You might want to show what you've tried, and use pseudocode to show what you're considering working toward. And by the way, welcome to Stack Overflow! – GargantuChet Dec 20 '12 at 16:20
  • Thank you! Ill give it a couple more tries with the answer below and if not ill give a more thorough question! :) – MikeHolford Dec 23 '12 at 12:51
  • I have added in my code that Im using, any suggestions? – MikeHolford Dec 23 '12 at 14:24

1 Answers1

0

You would just put the code in a loop:

for($a=0;$a<4;$a++){
    $recipe=R::dispense('recipe');
    $recipe->import($_POST['recipe_'.$a]);//You need to configure this portion
    //OR
    $recipe->title=$_POST['recipe'][$a]['title'];
    R::store($recipe);
}
Tim Withers
  • 12,072
  • 5
  • 43
  • 67