Jump to content

Looping and Forms


shadowayex

Recommended Posts

I have a JavaScript function used to write new fields inside a form, each one is given it's own name. The code for that looks like this:

<script type="text/javascript">var i = 1;function addChoices() { numofchoices = prompt("How many choices?","1"); if(i > 1) {  numofchoices = (i * 1) + (numofchoices * 1);  numofchoices = (numofchoices * 1) - 1; } while(i <= numofchoices) {  document.getElementById("newchoices").innerHTML += "Choice " + i + ": ";  document.getElementById("newchoices").innerHTML += "<input type=\"text\" style=\"width: 400px;\" name=\"choice" + i + "\" value=\"\" /><br />";  i = 1 + (i*1); }}

How do I make it so the PHP code I'm using will loop and get the data from each one written? I tried building loops but I don't know how to let it know how many fields there are and have it know to look for "choice1, choice2, etc." until the point when the user stopped adding fields.

Link to comment
Share on other sites

  document.getElementById("newchoices").innerHTML += "<input type=\"text\" style=\"width: 400px;\" name=\"choice[]\" value=\"\" /><br />";

I change the name of the input field to choice[] rather than choice+i. If you want, you could also use choice. Either way will work.To loop through the input fields via PHP:

if (isset($_POST['choice']) and is_array($_POST['choice'])):	  foreach ($_POST['choice'] as $key => $v):		 echo 'Choice ', $key, ': ', $v;	  endforeach;endif;

Link to comment
Share on other sites

Alright. I built the whole loop thing, but it's not doing anything. Here's the three chunks of code I have:The Form:

	echo '<p style="font-weight: bold;">What should the next update be?</p>';	echo '<form action="vote.php?mode=castvote" method="post">';	echo '<div>';	$choices = mysql_query("SELECT * FROM votes");	$descript = mysql_query("SELECT * FROM votecomments WHERE VID = '1'");	$descript = mysql_fetch_array($descript);	echo $descript['Comment'];	while($row = mysql_fetch_array($choices)) {	    echo '<input type="radio" name="vote" value="{$row[\'VID\']}" /> {$row[\'Choice\']}<br />';	}	echo '<p>Comment:<br />';	echo '<textarea rows="5" cols="50" name="comment"></textarea></p>';	echo '<input type="submit" name="submit" value="Vote" />';	echo '</div>';	echo '</form>';

The JavaScript Function:

var i = 1;function addChoices() {    document.getElementById("newchoices").style.visibility = "visible";	document.getElementById("newchoices").style.overflow = "";	document.getElementById("newchoices").style.height = "";    numofchoices = prompt("How many choices?","1");	if(i > 1) {	    numofchoices = (i * 1) + (numofchoices * 1); 		numofchoices = (numofchoices * 1) - 1;	}	while(i <= numofchoices) {	    document.getElementById("newchoices").innerHTML += "Choice " + i + ": ";		document.getElementById("newchoices").innerHTML += "<input type=\"text\" style=\"width: 400px;\" name=\"choice" + i + "\" value=\"\" /><br />";		i = 1 + (i*1);	}	document.getElementByName("numofchoices").value = numofchoices;}

The Processing Code:

elseif($mode == "addchoice") {	$numofchoices = $_POST['numofchoices'];	for($i=1; $i<=$numofchoices; $i++) {        $choice = mysql_real_escape_string($_POST['choice' . $i]);		$choice = mysql_query("INPUT INTO votes (Choice, NumOfVotes) VALUES ('$choice', '0')");	}	$descript = mysql_real_escape_string($_POST['descript']);	$descript = mysql_query("UPDATE votecomments SET Comment = '$comment' WHERE VID = '1'");	$desctime = mysql_query("UPDATE votecomments SET Time = '$timestamp' WHERE VID = '1'");}

At this point I'm getting irritated, because I don't even know why it doesn't work. The logic seems fine.

Link to comment
Share on other sites

document.getElementByName("numofchoices").value = numofchoices;

Try

document.getElementById("numofchoices").value = numofchoices;

Link to comment
Share on other sites

:) sorry I just noticed
$choice = mysql_query("INPUT INTO votes (Choice, NumOfVotes) VALUES ('$choice', '0')");

That should be

$choice = mysql_query("INSERT INTO votes (Choice, NumOfVotes) VALUES ('$choice', '0')");

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...