Hello Everyone,

I have a form (it is for a web based poll) it collects 4 pieces of information:

1) the poll question
2) the amoutn of choices
3) the start date of poll
4) the end date for poll

Now when this form is submitted(same script $php_self) it takes the amount of choices entered and
I run a for loop to get the amount of text-boxes I need in the new form, they are dynamically created using names choice1, choice2
and so on depending on the amount entered in the form before.

That is not the problem, that part is working great.

The problem comes in when I want to run a javascript script to get the values of the text fields, I use the following code:

elseif (isset($_POST['submit1'])) 
{ 
    $question = $_POST['question']; 
    $totalchoices= $_POST['totalchoices']; 
    $startdate = $_POST['startdate']; 
    $enddate = $_POST['enddate']; 
     
    $php_self = $_SERVER['PHP_SELF']; 
     
$steptwodata = <<<endofsteptwodata 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Web Poll Generator</title> 
<script language="javascript" type="text/javascript"> 
function checkForm(form)  
{    
    var howMany = form.elements.length; 
    var i = 0; 
     
                for (count = 0; count < howMany; count++) 
               { 
                    document.write(form.elements[count].value +"<br />"); 
               }     
    return false; 
} 
</script> 
</head> 
<body> 
<form name="formgenerator2" action="$php_self" method="post" onSubmit="return checkForm(this);"> 
<div align="center"> 
    <table border="1" cellpadding="5" cellspacing="0" width="600" bordercolor="#CCCCCC" bgcolor="#FFFF00"> 
        <tr> 
            <td> 
            <div align="center"> 
                <table border="1" cellpadding="5" cellspacing="0" width="100%" bordercolor="#DDDDDD" bgcolor="#FFFFFF" id="table2" style="font-family: Tahoma; font-size: 12px; color: #000000"> 
                    <tr> 
                        <td colspan="2"><p align="center"><font color="#008000"><b>Web Poll Generator</b></font></td> 
                    </tr> 
endofsteptwodata; 
for ($datacount = 0,$choicecount = 1; $datacount < $totalchoices; $datacount++,$choicecount++) 
{ 
$steptwodata .= <<<endofsteptwodata 

                    <tr> 
                        <td width="126" align="left">Choice $choicecount:</td> 
                        <td align="left"><input type="text" name="choice$choicecount" size="30" tabindex="$choicecount" style="font-family: Tahoma; font-size: 12px; color: #800000"></td> 
                         
                    </tr> 
endofsteptwodata; 
} 
$steptwodata .= <<<endofsteptwodata 
                    <tr> 
                        <input type="hidden" name="question" value="$question" /> 
                        <input type="hidden" name="totalchoices" value="$totalchoices" /> 
                        <input type="hidden" name="startdate" value="$startdate" /> 
                        <input type="hidden" name="enddate" value="$enddate" /> 
                        <td colspan="2" align="center"><input type="submit" value="Generate Poll" name="submit2" tabindex="99" style="font-family: Tahoma; font-size: 12px; color: #008000"></td> 
                    </tr> 
                    <tr> 
                </table> 
            </div> 
            </td> 
        </tr> 
    </table> 
</div> 
</form> 
endofsteptwodata; 
print $steptwodata;

The problem is that for some reason it ONLY recognizes the very first text input and not the others.
All I get on the output is the value of the first text box not the others.

is there something I am doing wrong?

Is there anything in PHP that I could do to accomplish the same thing?

Any and all help will be greatly appreciated.

Thank you in advance

Leo Zayas

I'm not sure I understand your goal, but it appears that when a user submits the form, you don't want the form to submit anywhere--instead you want to display the values from the form at the bottom of the page--one per line?
From your code:

document.write(form.elements[count].value +"<br />");

If that's the case, I'd use a DIV. In your page, where you want the values to appear, add this line:

<div id="values"></div>

Then, in your checkForm() function, replace your document.write line with something like:

document.getElementById('values').innerHTML += form.elements[count].value+"<br />";

I'm not sure if I understood the issue--does this help you?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.