944,131 Members | Top Members by Rank

Ad:
Feb 24th, 2006
0

Form Fields

Expand Post »
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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. elseif (isset($_POST['submit1']))
  2. {
  3. $question = $_POST['question'];
  4. $totalchoices= $_POST['totalchoices'];
  5. $startdate = $_POST['startdate'];
  6. $enddate = $_POST['enddate'];
  7.  
  8. $php_self = $_SERVER['PHP_SELF'];
  9.  
  10. $steptwodata = <<<endofsteptwodata
  11. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  12. <html xmlns="http://www.w3.org/1999/xhtml">
  13. <head>
  14. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  15. <title>Web Poll Generator</title>
  16. <script language="javascript" type="text/javascript">
  17. function checkForm(form)
  18. {
  19. var howMany = form.elements.length;
  20. var i = 0;
  21.  
  22. for (count = 0; count < howMany; count++)
  23. {
  24. document.write(form.elements[count].value +"<br />");
  25. }
  26. return false;
  27. }
  28. </script>
  29. </head>
  30. <body>
  31. <form name="formgenerator2" action="$php_self" method="post" onSubmit="return checkForm(this);">
  32. <div align="center">
  33. <table border="1" cellpadding="5" cellspacing="0" width="600" bordercolor="#CCCCCC" bgcolor="#FFFF00">
  34. <tr>
  35. <td>
  36. <div align="center">
  37. <table border="1" cellpadding="5" cellspacing="0" width="100%" bordercolor="#DDDDDD" bgcolor="#FFFFFF" id="table2" style="font-family: Tahoma; font-size: 12px; color: #000000">
  38. <tr>
  39. <td colspan="2"><p align="center"><font color="#008000"><b>Web Poll Generator</b></font></td>
  40. </tr>
  41. endofsteptwodata;
  42. for ($datacount = 0,$choicecount = 1; $datacount < $totalchoices; $datacount++,$choicecount++)
  43. {
  44. $steptwodata .= <<<endofsteptwodata
  45.  
  46. <tr>
  47. <td width="126" align="left">Choice $choicecount:</td>
  48. <td align="left"><input type="text" name="choice$choicecount" size="30" tabindex="$choicecount" style="font-family: Tahoma; font-size: 12px; color: #800000"></td>
  49.  
  50. </tr>
  51. endofsteptwodata;
  52. }
  53. $steptwodata .= <<<endofsteptwodata
  54. <tr>
  55. <input type="hidden" name="question" value="$question" />
  56. <input type="hidden" name="totalchoices" value="$totalchoices" />
  57. <input type="hidden" name="startdate" value="$startdate" />
  58. <input type="hidden" name="enddate" value="$enddate" />
  59. <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>
  60. </tr>
  61. <tr>
  62. </table>
  63. </div>
  64. </td>
  65. </tr>
  66. </table>
  67. </div>
  68. </form>
  69. endofsteptwodata;
  70. 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kurupt4 is offline Offline
1 posts
since Feb 2006
Mar 5th, 2006
0

Re: Form Fields

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: [PHP]document.write(form.elements[count].value +"<br />");[/PHP]
If that's the case, I'd use a DIV. In your page, where you want the values to appear, add this line:
[PHP]<div id="values"></div>[/PHP]
Then, in your checkForm() function, replace your document.write line with something like:
[PHP]
document.getElementById('values').innerHTML += form.elements[count].value+"<br />";
[/PHP]

I'm not sure if I understood the issue--does this help you?
Reputation Points: 36
Solved Threads: 6
Posting Whiz
Troy is offline Offline
354 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Help needed on select option (multible) [list box]
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: How to create Dynamic Page Break in HTML?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC