Jump to content

help with foreach statement


daveh33

Recommended Posts

how would I do it in javascript? I found this example - but dont no how to edit it with my $_POST['num'] variable

 

<script type="text/javascript">
<!--
var linebreak = "<br />";
document.write("For loop code is beginning");
document.write(linebreak);

for(i = 0; i < 5; i++){
document.write("Counter i = " + i);
document.write(linebreak);
}

document.write("For loop code is finished!");
</script>

i'd probably use a div tag and use innerHTML to rewrite that particular area with the new form (form part)

 

 

As for the $_POST['num'] bit i'm not sure where this is, if it's while generating the page then see this for an example:

<?php
print "<javascript>function doit() { var num = ".$_POST['num']."; } </javascript>";
?>

This code is untested and written on the fly...but it should look something like this:

 

<script type="text/javascript">
  function loadFields ( ele ) {
    var fieldNode = document.getElementById('formFields');
    fieldNodes.innerHTML = '';
    if(!ele.value.length) return;
    for(var n=0;n < ele.value;n++)
      fieldNodes.innerHTML += '<input type="text" name="name_' + n + '" /><br />';
  }
</script>
<form action="POST">
  Select the number of people: 
  <select name="num_people" onchange="loadFields();">
    <option value=""></option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
  </select>
  <div id="formFields"></div>
</form>

Yes, that demonstrates how to use PHP  to output javascript, you do understand that PHP is serverside, javascript and HTML are clientside. You use PHP to generate the page, you asked how to use $_POST['num'].

 

That wasn't a solution to your whole project! And I gotta ask, what did you expect it to output, but I bet it doesn't actually display that on the page?

If this is for a personal site, then a JS solution *may* be suitable. But, if it is for a buisiness site or a site in which errors are not acceptable, then JS is not an appropriate solution. Some user may not have JS enabled.

 

But, you can use a combined approach. I suggest having 10 name fields available by default. Then use JS to display the drop-down and hide the appropriate name fields. So, you will get the functionality you want, but users w/o JS will still be able to enter the names. you would also want the processing page to loop through the name fields and only process ones that have a value.

 

<html>
<head></head>
<body onload="changeNamesAllowed()">

<script type="text/javascript">

function changeNamesAllowed() {

 document.test.name_count.style.display = 'inline';
 names = document.test.name_count.value;

 for (i=1; i<=10; i++) {
   display = (i<=names)?'inline':'none';
   document.getElementById('name_'+i).style.display = display;
 }
}

</script>

<form name="test">

Names to show: 
<select name="name_count" style="display: none;" onchange="changeNamesAllowed();">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select><br><br>

<span id="name_1">Name 1: <input type="text" name="name_1" /><br /></span>
<span id="name_2">Name 2: <input type="text" name="name_2" /><br /></span>
<span id="name_3">Name 3: <input type="text" name="name_3" /><br /></span>
<span id="name_4">Name 4: <input type="text" name="name_4" /><br /></span>
<span id="name_5">Name 5: <input type="text" name="name_5" /><br /></span>
<span id="name_6">Name 6: <input type="text" name="name_6" /><br /></span>
<span id="name_7">Name 7: <input type="text" name="name_7" /><br /></span>
<span id="name_8">Name 8: <input type="text" name="name_8" /><br /></span>
<span id="name_9">Name 9: <input type="text" name="name_9" /><br /></span>
<span id="name_10">Name 10: <input type="text" name="name_10" /><br /></span>

</form>

</body></html>

Archived

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

×
×
  • 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.