How to take multiple input from user in php

To summarize from a previous question, the user selected a starting date, an ending date, and a region. From these choices, an array of weeks and regions are created, and users enter values ("points") that correspond to a particular week and region. This is what it looks like right now (some parts are hard-coded for testing purposes):

$date_array = Array("01/01/2012", "01/08/2012", "01/15/2012"); $region_array = Array("NYC", "DC"); <form name="test_form" id="test_form" method="post"> <table border="1"> <tr> <td></td> <?php foreach ($date_array as $date) { echo "<td>".$date."</td>"; } ?> </tr> <?php foreach ($region_array as $region) { echo "<tr>"; echo "<td>".$region."</td>"; foreach ($date_array as $date) { echo '<td><input type="text" name='.$region."-value1_".$date.'><input type="text" name='.$region."-value2_".$date.'></td>'; } echo "</tr>"; } ?> </table> <input type="submit" name="submit">

And the next part:

if (isset($_POST['submit'])) { foreach ($_POST as $k=>$v) { if ($k != "submit") // use even and odd count to differentiate TRP and CPP { // split the form input on _ $input = explode("_", $k); echo "<BR>Region: " . $input[0]; echo "<BR>Date: " . $input[1]; echo "<BR>Value: " . $v; } echo "<br/>"; } }

So basically, in this example it outputs 12 times:

Region: NYC-value1 Date: 01/01/2012 Value: 500 Region: NYC-value2 Date: 01/01/2012 Value: 5 Region: NYC-value1 Date: 01/08/2012 Value: 600

I want to catch the value1 and value2 while the loop is running, but as it stands it loops through both of them before going onto the next date/region. My idea, as I put in comments, was to have a counter of even and odd numbers and simply tell if it was doing value1 or value2 based on that.

Is this a good idea or am I missing something fundamentally simpler?

How to use PHP & Multiple Input Text Field/Textbox This tutorial how to using PHP read a variable from multiple input textbox

ShotDev Focus:
- Using PHP & Multiple Textbox

Example 1

php_multiple_textbox1.php

<html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <form action="php_multiple_textbox2.php" method="post" name="form1"> <input type="text" name="txtSiteName[]"><br> <input type="text" name="txtSiteName[]"><br> <input type="text" name="txtSiteName[]"><br> <input name="btnSubmit" type="submit" value="Submit"> </form> </body> </html>

php_multiple_textbox2.php

<html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <? for($i=0;$i<count($_POST["txtSiteName"]);$i++) { echo "txtSiteName $i = ".$_POST["txtSiteName"][$i]."<br>"; } ?> </body> </html>

Create a php file and save to path root-path/myphp/

Run
//localhost/myphp/php_multiple_textbox1.php

Screenshot


.
.

Example 2 (Create Element)

php_multiple_textbox3.php

<html> <head> <title>ShotDev.Com Tutorial</title> <script language="javascript"> function fncCreateElement(){ var mySpan = document.getElementById('mySpan'); var myElement1 = document.createElement('input'); myElement1.setAttribute('type',"text"); myElement1.setAttribute('name',"txtSiteName[]"); mySpan.appendChild(myElement1); var myElement2 = document.createElement('<br>'); mySpan.appendChild(myElement2); } </script> </head> <body> <form action="php_multiple_textbox4.php" method="post" name="form1"> <input type="text" name="txtSiteName[]"> <input name="btnButton" type="button" value="+" onClick="JavaScript:fncCreateElement();"><br> <span id="mySpan"></span> <input name="btnSubmit" type="submit" value="Submit"> </form> </body> </html>

php_multiple_textbox4.php

<html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <? for($i=0;$i<count($_POST["txtSiteName"]);$i++) { echo "txtSiteName $i = ".$_POST["txtSiteName"][$i]."<br>"; } ?> </body> </html>

Create a php file and save to path root-path/myphp/

Run
//localhost/myphp/php_multiple_textbox4.php

Screenshot

.
.
.

Download this script.

Category: PHP & HTML Form and Element by admin

(No Ratings Yet)

 Loading ...

One Response to “How to use PHP & Multiple Input Text Field/Textbox”

Leave a Reply

You must be logged in to post a comment.

How do I prompt in PHP?

PHP is a server side language, it can't do alert messages on the client side. But you can use javascript within the php to do the alert. <? php //prompt function function prompt($prompt_msg){ echo("<script type='text/javascript'> var answer = prompt('".

What is readline PHP?

Method 1: Using readline() function is a built-in function in PHP. This function is used to read console input.

How do I scan a value in PHP?

The sscanf() function parses input from a string according to a specified format. The sscanf() function parses a string into variables based on the format string. If only two parameters are passed to this function, the data will be returned as an array.

How can I get input field value in PHP without form?

If you don't want to submit a form, the only two other ways of accomplishing this are to click a link with the data as query parameters in the url, or use AJAX to send the data to the server in the background.

Postingan terbaru

LIHAT SEMUA