Jump to content

Recommended Posts

Hi,

 

 

First thing PHP freaks Rocks. People are helpful, quick, genius and dynamic :).

Thank you PHP freaks and all the people in here.

 

 

I'm a beginner in PHP.

I have a dynamic form. It generates n no. of text fields at 1 shot.

 

Here is the HTML Code:

 

/// HTML Code Starts here ///

 

<html><head>

<title>My Test Page</title>

<script type="text/javascript">

 

function addFieldz()

{

var i = document.getElementById('rows').value;

alert ("i val = "+i);

while (i > 0)

{

txtFields.innerHTML = txtFields.innerHTML+"<br><input type=\"text\" name=\"t"+i+"\" id=\"t"+i+"\" \/><br/>";

i--;

}

}

</script></head>

<body>

<form method="post" action="test-js-php.php">

No. of Text Fields: <input type="text" name="rows" id="rows" style="width:30px;"> <input type="button" onClick="addFieldz()" name="add" id="add" value="Add">

<div id="txtFields">

</div>

<br/>

<input type="submit" name="submit" id="submit" value="submit">

<input type="reset" name="reset" id="reset" value="reset">

</form>

</body></html>

 

 

/// HTML Code Ends here ///

 

So Initally I wanted to test it for 3 text fields.

Here is my PHP code.

 

<?php

$val1 = $_POST['t1'];

$val2 = $_POST['t2'];

$val3 = $_POST['t3'];

 

echo $val1.$val2.$val3;

 

?>

This works. I was really happy by this achievement.

 

But now comes the real problem. How can I retrieve the values if there 100 text fields ?? :(.

 

Is there a way in php, so we can get the post values in a loop by some conditions??

 

Please help.

 

Thanks in Advance.

 

Link to comment
https://forums.phpfreaks.com/topic/141623-solved-dynamic-form-and-php-help/
Share on other sites

Hi

 

Thank you for replying back.

 

As I'm new I'm trying to understand the concept.

 

You mean to say I have to change the following statement

 

 

from

 

txtFields.innerHTML = txtFields.innerHTML+"<br><input type=\"text\" name=\"t"+i+"\" id=\"t"+i+"\" \/><br/>";

 

 

to

 

 

txtFields.innerHTML = txtFields.innerHTML+"<br><input type=\"text\" name=\"t["+i+"]\" id=\"t["+i+"]\" \/><br/>";

 

 

 

So that I can read it as array in php or something like that?

 

Please let me know.

 

Thanks again

Giri

Hi

 

I've changed my JS code to what is suggested.

So My Js dynamic test field looks like this

 

txtFields.innerHTML = txtFields.innerHTML+"<br><input type=\"text\" name=\"t["+i+"]\" id=\"t["+i+"]\" \/><br/>";

 

 

and I've used the code suggested for PHP

my php code is like this

 

<?php

foreach ($_POST as $key => $value) {

echo "<br/>key: " . $key . " <br/>value: " . $value . "<br/>";

?>

 

 

This is the output I get

 

 

key: rows

value: 2

 

key: t

value: Array

 

key: submit

value: submit

 

 

aah I'm confused. The " key:t value:Array " are actually the text fields that are dynamically generated. I need the values in that text fields. Is there a way we can retrieve them?

I've tried $t[0] but it didn't work.

 

Any help??

 

Thanks in advance.

Hi Maq,

 

Thanks a lot for the help.

I've just made some experiments based on the change you told and went through the following link

 

http://www.phpfreaks.com/forums/index.php/topic,216783.0.html

 

and finally my problem is solved. now I generate 100 text fields and I'm able to get all the data in one shot :)

 

thanks PHP Freaks !

 

If some one out there having some similar problem like mine here is the solution.

 

 

MY HTML CODE : (Dynamic Form using JS for generating the textfields)

 

 

<html><head>

<title>My Test Page - Giri JS + PHP + Bynamic Form ;) it works</title>

<script type="text/javascript">

 

function addFieldz()

{

var i = document.getElementById('rows').value;

alert ("i val = "+i);

while (i > 0)

{

txtFields.innerHTML = txtFields.innerHTML+"<br><input type=\"text\" name=\"t[]\" id=\"t[]\" \/><br/>";

i--;

}

}

</script></head>

<body>

<form method="post" action="test-js-php.php">

No. of Text Fields: <input type="text" name="rows" id="rows" style="width:30px;"> <input type="button" onClick="addFieldz()" name="add" id="add" value="Add">

<div id="txtFields">

</div>

<br/>

<input type="submit" name="submit" id="submit" value="submit">

<input type="reset" name="reset" id="reset" value="reset">

</form>

</body></html>

 

 

MY PHP CODE:

 

 

<?php

$num = $_POST['rows'];

 

//echo "<br/>num val outside - ".$num;

 

while ($num > 0)

{

 

//echo "<br/>num val in while  - ".$num;

echo "<br/>form value:  ".$_POST['t'][$num-1]."<br/>";

$num--;

 

}

 

?>

 

Hope this helps someone. :)

A 100,000 thanks to Maq and 1,000,000,000 thanks to PHP Freaks !

 

Giri.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.