Skip to content

Instantly share code, notes, and snippets.

@obojdi
Created November 6, 2014 15:14
Show Gist options
  • Save obojdi/93478a95a0be00f602d2 to your computer and use it in GitHub Desktop.
Save obojdi/93478a95a0be00f602d2 to your computer and use it in GitHub Desktop.
Handle POST from dynamically added form fields
<div class="button insert">Добавить поле</div>
<form class="ui-filterable" method="POST">
<?
$sum=4;
for ($count=0; $count <$sum ; $count++)
{
?>
<input type="text" class="input text" name="preset[]" placeholder="text" value="preset text <?=$count?>"/>
<?}?>
<input class="button submit" type="submit">
<?
if($_SERVER['REQUEST_METHOD']=='POST')
{
if($_POST['preset'])
{
foreach ($_POST['preset'] as $preset => $value)
{
echo "UPDATE db SET text='{$value}' WHERE id=".$preset;
echo "<br/>";
}
}
echo "<br/>";
if($_POST['text'])
{
// var_dump($_POST['text']);
foreach ($_POST['text'] as $text => $value)
{
echo "INSERT INTO db (text) VALUES ('{$value}')";
echo "<br/>";
// echo "text: ".$text."<br/>";
// echo "value: ".$value."<br/>";
}
}
else
{
echo "no post text";
}
}
else
{
echo "no post";
}
?>
</form>
<script type="text/javascript">
var i=0;
function createTable (argument)
{
$('.submit').before('<input type="text" name="text[]" placeholder="text" value="test text '+i+'" class="input text"/>')
i++;
}
$(document).ready(function()
{
$('.button.insert').click(function()
{
createTable();
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment