I google'd myself silly, I can't find tutorials for this. I hope someone could lend me a hand.
I'll try to make this simple. I have a PHP & MySQL driven site. I have a form for adding new content.
The form has a set of check box options that are pulled from MySQL.
At the bottom of the check boxes there is an "Add New" input field which I want to be used to add new check box options right above it.
database
table options
id int 255 auto increment primary key
name varchar 255
index.php
<form action="saveinfo.php" method="post">
<?php
$gf = mysql_query("SELECT * FROM options");
while($agf = mysql_fetch_array($gf)){
$id = $agf['id'];
$option_name = $agf['name'];
echo "<input type=\"checkbox\" name=\"option[$id]\" value=\"true\" />$option_name<br />";
}
?>
<div id="newoptions">[...New options here...]</div>
<input type="text" name="addnew" value="" placeholder="Add New" />
[...the rest of the form...]
<input type="submit" name="submit" value="Save Information"/>
</form>
So as I understand how this should work, when I click in the 'add new' text field, type something in, then press enter - the value of the add new field should be sent to a php script, addnew_backend.php. The form itself should not submit it's data. The PHP script will add the new value to the mysql table of options, and if successful, returns the newley generated checkbox which should fade in right above the add new text field into div#newoptions.
addnew_backend.php
<?php
//connect to mysql
include("config.php");
$name = mysql_real_escape_string(trim($_POST['addnew']));
$ins = mysql_query("INSERT INTO options (id,name) VALUES (id,'$name')");
//success
if($ins){
$insertid = mysql_insert_id();
echo "<input type=\"checkbox\" name=\"option[$insertid]\" value=\"true\" checked=\"checked\" />";
}else{
//fail
echo mysql_error();
}
?>
So I think I understand what needs to happen, could someone help me with the actual code?
Thank you!