Avatar of s3adcom
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('input');
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().slideDown("normal");
                           
                        }
                    );
                return false;
            });
            $("div#msgbox").click(function(){
            $(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>
PHPJScriptAJAX

Avatar of undefined
Last Comment
s3adcom
Avatar of leakim971
leakim971
Flag of Guadeloupe image

add this (in bold) :
var cell8 = row.insertCell(8);
var itAd = document.createElement('input');
itAd.setAttribute('type', 'submit');
itAd.setAttribute('name', INPUT_NAME_PREFIX + iteration);
itAd.style.width = "100%";
itAd.setAttribute('value', "Add");
itAd.id="add"+iteration;
itAd.className = "addButton";

and replace :
$("#add".iteration).click(function() {
by :
$(".addButton").live("click", function() {

more info : http://api.jquery.com/live/
Avatar of leakim971
leakim971
Flag of Guadeloupe image

hmm... I miss the point you want to post the right data, so use :

                $(".addButton").live("click", 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().slideDown("normal");
                           
                        }
                    );
                return false;
            });
Avatar of leakim971
leakim971
Flag of Guadeloupe image

sorry correction :

                $(".addButton").live("click", 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().slideDown("normal");
                           
                        }
                    );
                return false;
            });
Avatar of leakim971
leakim971
Flag of Guadeloupe image

this line :
var iteration = $(this).attr("id").replace("itAd","");

help us to retrieve the iteration from the button ID, we replace itAd by nothing
Avatar of s3adcom
s3adcom

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..
Avatar of leakim971
leakim971
Flag of Guadeloupe image

please provide a link to your page
Avatar of s3adcom
s3adcom

ASKER

Attached is my page! Thanks a lot for your help. InventoryManagement.php
Avatar of leakim971
leakim971
Flag of Guadeloupe image

everything run fine on my side
I asked a link to your page to see it running not the source
Avatar of s3adcom
s3adcom

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.
Avatar of s3adcom
s3adcom

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.
Avatar of leakim971
leakim971
Flag of Guadeloupe image

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
Avatar of s3adcom
s3adcom

ASKER

I'm sorry, but its not working!
Really thank you for your time and cooperation, means a lot!
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of s3adcom
s3adcom

ASKER

Thank you very much!! It works now.
Avatar of s3adcom
s3adcom

ASKER

End up following this guy!
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo