So, i've got this page:
And i need the contents of the ingredients boxes to be added to a cookie in the format:
ingredient:quantity
And i also need ajax to remove the cookie and display the values of the cookies in the table.
So far, i've worked out that:
<?php print_r($_COOKIE); ?>
Will display all the cookies on the same domain as the script.
So that can be used for loading all the cookies, then using PHP to explode out the ':'s.
But i've never used ajax before, so i need some help. My attempt failed - nothing happened when i clicked the 'Add Ingredient' button.
Can anyone with Ajax experience help me?
Thanks
If you are using cookies to pass the information to PHP, it isn't Ajax
In this case I don't even think you'll be needing Ajax. Just some Javascript will be enough.
I guess this is for a MyBB plugin? And everything is in a form?
If this is for MyBB you can use the Prototype JS which is already include in the core to help you a bit along.
I scribbled some code together for an example:
http://jsbin.com/ujoso5/edit (click preview to see it in action)
Fill in an ingredient and the quantity and click the Add button. This will insert a new row above the current row with the values you just entered and the add button will be replaced by a remove button. Because the input fields are named "ingredient[]" and "quantity[]" these fields will show up as an array in the $_POST or $mybb->input variables. You can loop through them to when handling the form submission like this:
$ingredient_count = count($mybb->input['ingredient']);
for($i=0; $i < $ingredient_count; $i++)
{
// ignore empty ingredients
if(empty($mybb->input['ingredient'][$i])) continue;
$ingredient = $mybb->input['ingredient'][$i];
$quantity = $mybb->input['quantity'][$i];
// do something with those variables
}
I hope this helps you.
Now this is nifty, and I will be looking at it in more detail - but - in the demo - the text we enter does not stay visible....
Is that because its the demo?
Its JUST what I have been looking for to use in threads submissions
There is no save option in the demo, no. This is only to demonstrate the Javascript because that is what, I think, the TS needed in this case.
Its not for a MyBB plugin, but that code is perfect
Thank you sooo much
However, there is a problem - i need to echo the individual ingredients onto a blank page, but i can't get them to be seperate.
This is the code i'm using:
$ingredient_count = count($_POST['ingredient']);
for($i=0; $i < $ingredient_count; $i++) {
// ignore empty ingredients
if(empty($_POST['ingredient'][$i])) continue;
$ingredient = $_POST['ingredient'][$i].":".$_POST['quantity'][$i];
}
echo $ingredient;
However, it only displays the last ingredient that was in the textboxes.
You overwrite the variable each time. Let me guess: you only see the last one?
Try this:
$ingredient_count = count($_POST['ingredient']);
$ingredients = "";
for($i=0; $i < $ingredient_count; $i++) {
// ignore empty ingredients
if(empty($_POST['ingredient'][$i])) continue;
$ingredients .= $_POST['ingredient'][$i].":".$_POST['quantity'][$i] ."<br/>";
}
echo $ingredients;
I already solved this on msn, sorry for not letting you know
(2010-12-24, 10:22 PM)Charlie Hadden Wrote: [ -> ]I already solved this on msn, sorry for not letting you know
Its always handy to have the information in the thread though - I think a lot of people are interested in this.