I have the following html:
<h4>Add Steps</h4>
<ul id="pasi">
<li>
<span>Pasul 1: </span><textarea id="pas"></textarea>
<input id="add_pas" type="button" value="Add Step" onclick="add_pas()"/>
</li>
<input id="" type="button" value="Add recipe" />
and this jquery function:
function add_pas() {
var pas = "Pasul " + i;
var list = $('#pasi');
var html = "<li class='pas-"+ pas +"'><span>" + pas + "</span><textarea></textarea><img src='../images/x.png' /></li>";
list.each(function(){
$(this).append(html);
});
i++
}
The function adds a new step when the button is clicked.
SQL:
TABLE recipe_steps (id, recipe_id,sort_oder,description)
My question is how do I insert the custom number of steps into the database, and then how to retrieve it. A first thaught was to insert all the steps into a vector(steps[]) but I don't really know where to start. Please take in consideration that I'm a begginer in php, mysql, jquery . Thank you and I hope I made it as clear as possible.
I came up with this that worked
function trimite(){
var pasi = {}; //initializing an array
var k=0;
$(".nimic").each(function () { //class of the <textarea>
pasi[k]=$(this).val(); //takes the value for each of the textareas created
k++;
});
$.ajax({ // sends all the textarea created to the php processing script
type:'POST',
url:"functions/add_recipe.php",
dataType:"json",
data:{
'pasi':pasi
},
success: function(data){
},
error: function(){
alert('error');
}
});
return false;
}
I don't understand how you're transitioning from js to inserting into SQL (but I may just be missing something!), but IMO you should use a jquery $.post function to call a separate .php file to do the insertion
$("#FORM_SUBMIT_ID").click(function() {
$("#ENTIRE_FORM_NAME").submit( function () {
$.post(
'SQL_INSERTION.PHP', //NOTE <-- assumes .php file is in same dir as curr file
$(this).serialize(), //This takes your form and auto separates it
function(data){
$("#ERRORS").html(data); //This will append error text to a div if you wish
}
);
return false;
});
});
Then, on your .php file you will have an array in $_POST with ALL the form values that were filled out. To test how serialize() formats your form into an array, on your php file just write
<?php print_r($_POST); ?>
I believe that should be enough. Then from there, for custom insertion, depending on how many values you have that can vary in presence, simple if/else if statements may be good enough.
i.e. if ($_POST['name']){ //code}
Alternatively, you can pass in a form "type" along with the serialized data to .php, and that will go into the $_POST array.