scripting - Unlimited fields in PHP? -


How do I make unlimited fields in PHP? Here's the scenario:

First of all, there are only 2 fields, it says: First name 1, Last name 1

What do I have to do, when I use the "Add" button, This will add another 2 fields to the new line, field label / name name should be first 2, last name 2. And when I click again, its first name will be 3, last name 3, and so on ..

Can someone give me some sample scripts in php? I'm new to PHP

The form should be in HTML if an Ajax sample code can be given, then this will be a big plus.

It depends on what you mean by "farm." It seems that you are talking about a form, which will not be PHP, but rather HTML. You can have a button [add] posting back to the server, after which the page is refreshed with other form-inputs, even without refreshing the page you do this via javascript.

Simple JavaScript (jQuery) Example:

Click on $ (document) .ready (function () {$ ("input [value = 'add']") Do (event (event) {event.preventDefault (); $ ("p.field: last") .clone () insertAfter ("p.field: last") ..;});}); & Lt; Form method = "post" & gt; & Lt; P class = "field" & gt; & Lt; Input type = "text" name = "firstname []" value = "" /> & Lt; Input type = "text" name = "lastname []" value = "" /> & Lt; / P & gt; & Lt; P & gt; & Lt; Input type = "submit" name = "submit" value = "add" /> & Lt; Input type = "submit" name = "submit" value = "Done" /> & Lt; / P & gt; & Lt; / Form & gt;

Simple PHP Example:

I do not encourage you to use it

  & Lt ;? Php $ count = 1; If ($ _POST ["submit"] == "add") {$ count = ($ _POST ["first name"])? (Count ($ _ POST ["First name"]) + 1): 1; } And if ($ _POST ["Submit"] == "Done") {Print "& lt; Pre & gt;"; Print_r ($ _ post ["firstname"]); Print_r ($ _ post ["nickname"]); Print "& lt; / pre & gt;"; }? & Gt; & Lt; Form method = "post" & gt; & Lt ;? For Php ($ i = 0; $ i & lt; $ count; $ i ++) {? & Gt; & Lt; P class = "field" & gt; & Lt; Input type = "text" name = "firstname []" value = "& lt ;? php print $ _POST [" firstname "] [$ i] ;? & gt;" / & Gt; & Lt; Input type = "text" name = "lastname []" value = "& lt ;? php print $ _POST [" lastname "] [$ i] ;; & gt; / & Gt; & Lt; / P & gt; & Lt ;? Php}? & Gt; & Lt; P & gt; & Lt; Input type = "submit" name = "submit" value = "add" /> & Lt; Input type = "submit" name = "submit" value = "Done" /> & Lt; / P & gt; & Lt; / Form & gt;  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -