I am trying to code a form that has multiple textbox without refershing page using ajax, then after each textbox threre will be link called add which POST call the other php called addnew.php.
In addnew.php data will we added to database(postgres). But I am geting problem while getting the post variable itself.For testing I added the alert in script.
this is my code for form (I will change for multiple textbox once it works fine)
script code
<script type='text/javascript'>
//<![CDATA[
$(window).load(function() {
jQuery(document).ready(function() {
jQuery('.add').live('click', function(event) {
//var da='da='+ $('#da').attr('value');
//var da = 'test';
var da = $('form#myform').serialize();
alert(da);
$.ajax({
type: "POST",
url: "addnew.php",
data:da,
success: function(data) {
if (data === "ok") {
$(this).parent().fadeOut();
$('#results').html(data);
}
else {
alert(data);
}
}
});
});
});
});
//]]>
</script>
form code
<body>
<?php
for ($i=1;$i<2;++$i) {//currently only one textbox for testing purpose
echo "<form name='myform' id='myform'>";
echo "<input name='da' type='text' id='da' value='none'>";
echo "<a href='#' class='add'>Add</a>";
echo "</form>";
}
?>
<div id="results"><div>
</body>
addnew.php code
<? php
if( isset($_POST['da']) )
{
echo (da);
}
?>
when page is rendered will have like this.
<textbox1 data> <add button>
<textbox1 data> <add button>
<textbox1 data> <add button>
...
<textbox10 data> <add button>
what I am trying is
- Created each textbox and add button pair inside each form dynamically using for loop. (for testing i created only one pair).Should I have form for every pair?
- when add is clicked value within textbox (#da) should be send to addnew.php through ajax.
following code is displaying data correctly
alert(da);
but in addnew.php file I am not getting $_POST(['da']). Is that means data is not passed to the file or is there something wrong ajax code and finally can I have multiple form with same id. If not then how i can send the only one textbox data ie just before the add button when form is submitted.