s3adcom
asked on
dynamic button & php
Hey guys,
I'm new in html and php so, sorry if my question seems clear and easy! Anyway, In my table I've a problem with dynamic Buttons! I'm trying to create a dynamic buttons each with same id+1 (starting from id 1) and I want to sent the data from also dynamic textfield to my database! Knowing that I'm using javascript to generate these buttons and fields and jQuery to parse the data to .php file (if there is easiest way please help me!).
Here is a part of my code..
using javascript i generate a button:
var cell8 = row.insertCell(8);
var itAd = document.createElement('in put');
itAd.setAttribute('type', 'submit');
itAd.setAttribute('name', INPUT_NAME_PREFIX + iteration);
itAd.style.width = "100%";
itAd.setAttribute('value', "Add");
itAd.id="add"+iteration;
itAd.setAttribute('onclick ', 'addRowToTable()');
$(document).ready(function (){
$("div#msgbox").hide();
$("#add".iteration).click( function() {
$.post('insert.php', {
proid: $("#proid".iteration).val( ),
brand: $("#brand".iteration).val( )
},
function(response){
$("div#msgbox #msg").html("Item added successfully..");
$("div#msgbox").hide().sli deDown("no rmal");
}
);
return false;
});
$("div#msgbox").click(func tion(){
$(this).slideUp("normal");
})
});
The HTML code is:
<table border=1 id="item_Table" width=73%>
<tr>
<th style="width:4%; size:20px;">#</th>
<th style="width:10%; size: 5px">Product ID</th>
<th style="width:10%; size: 5px">Brand</th>
<th style="width:4%"></th>
</tr>
</table>
I'm new in html and php so, sorry if my question seems clear and easy! Anyway, In my table I've a problem with dynamic Buttons! I'm trying to create a dynamic buttons each with same id+1 (starting from id 1) and I want to sent the data from also dynamic textfield to my database! Knowing that I'm using javascript to generate these buttons and fields and jQuery to parse the data to .php file (if there is easiest way please help me!).
Here is a part of my code..
using javascript i generate a button:
var cell8 = row.insertCell(8);
var itAd = document.createElement('in
itAd.setAttribute('type', 'submit');
itAd.setAttribute('name', INPUT_NAME_PREFIX + iteration);
itAd.style.width = "100%";
itAd.setAttribute('value',
itAd.id="add"+iteration;
itAd.setAttribute('onclick
$(document).ready(function
$("div#msgbox").hide();
$("#add".iteration).click(
$.post('insert.php', {
proid: $("#proid".iteration).val(
brand: $("#brand".iteration).val(
},
function(response){
$("div#msgbox #msg").html("Item added successfully..");
$("div#msgbox").hide().sli
}
);
return false;
});
$("div#msgbox").click(func
$(this).slideUp("normal");
})
});
The HTML code is:
<table border=1 id="item_Table" width=73%>
<tr>
<th style="width:4%; size:20px;">#</th>
<th style="width:10%; size: 5px">Product ID</th>
<th style="width:10%; size: 5px">Brand</th>
<th style="width:4%"></th>
</tr>
</table>
hmm... I miss the point you want to post the right data, so use :
$(".addButton").live("clic k", function() {
$.post('insert.php', {
var iteration = $(this).attr("id").replace ("itAd","" );
proid: $("#proid" + iteration).val(),
brand: $("#brand" + iteration).val()
},
function(response){
$("div#msgbox #msg").html("Item added successfully..");
$("div#msgbox").hide().sli deDown("no rmal");
}
);
return false;
});
$(".addButton").live("clic
$.post('insert.php', {
var iteration = $(this).attr("id").replace
proid: $("#proid" + iteration).val(),
brand: $("#brand" + iteration).val()
},
function(response){
$("div#msgbox #msg").html("Item added successfully..");
$("div#msgbox").hide().sli
}
);
return false;
});
sorry correction :
$(".addButton").live("clic k", function() {
var iteration = $(this).attr("id").replace ("itAd","" );
$.post('insert.php', {
proid: $("#proid" + iteration).val(),
brand: $("#brand" + iteration).val()
},
function(response){
$("div#msgbox #msg").html("Item added successfully..");
$("div#msgbox").hide().sli deDown("no rmal");
}
);
return false;
});
$(".addButton").live("clic
var iteration = $(this).attr("id").replace
$.post('insert.php', {
proid: $("#proid" + iteration).val(),
brand: $("#brand" + iteration).val()
},
function(response){
$("div#msgbox #msg").html("Item added successfully..");
$("div#msgbox").hide().sli
}
);
return false;
});
this line :
var iteration = $(this).attr("id").replace ("itAd","" );
help us to retrieve the iteration from the button ID, we replace itAd by nothing
var iteration = $(this).attr("id").replace
help us to retrieve the iteration from the button ID, we replace itAd by nothing
ASKER
Thanks for answering my question, but I still have one problem which is I can't access the page "insert.php". It seems that the $.post doesn't work correctly! I tried to put it in the <form> and out of it but still facing the same problem!
Again thanks and hope you can help me on this..
Again thanks and hope you can help me on this..
please provide a link to your page
ASKER
Attached is my page! Thanks a lot for your help. InventoryManagement.php
everything run fine on my side
I asked a link to your page to see it running not the source
I asked a link to your page to see it running not the source
ASKER
I'm running my code in locally, where "insert.php" is just print anything just to make sure it goes there, because now nothing happen if when I call it! Though, not sure what you mean.
ASKER
Yes this is work fine! however, I want to add these fields to my db and first I want to know if the $.post works or not when I click on "Add" button. It seems that $.post doesn't go to "insert.php"!! Just to make it clear, the "Add" button should insert the row to my db and also add new row in my table.
replace :
itTp.id="Type"+iteration;
by :
itTp.id="type"+iteration;
and replace :
var iteration = $(this).attr("id").replace ("itAd","" );
by :
var iteration = $(this).attr("id").replace ("add","") ;
it will work
itTp.id="Type"+iteration;
by :
itTp.id="type"+iteration;
and replace :
var iteration = $(this).attr("id").replace
by :
var iteration = $(this).attr("id").replace
it will work
ASKER
I'm sorry, but its not working!
Really thank you for your time and cooperation, means a lot!
Really thank you for your time and cooperation, means a lot!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much!! It works now.
ASKER
End up following this guy!
var cell8 = row.insertCell(8);
var itAd = document.createElement('in
itAd.setAttribute('type', 'submit');
itAd.setAttribute('name', INPUT_NAME_PREFIX + iteration);
itAd.style.width = "100%";
itAd.setAttribute('value',
itAd.id="add"+iteration;
itAd.className = "addButton";
and replace :
$("#add".iteration).click(
by :
$(".addButton").live("clic
more info : http://api.jquery.com/live/