0

I have a site that helps students make schedule's by inputting their classes. When they click a button, they can select the course from a dropdown and enter the class # in a box to the right. I am trying to use php to get the contents of the box, but it's not working. Part of the problem may be that the id of the box is assigned a number (like 1, 2, 3) by javascript.

Here's the JSFIDDLE (only add 1 class, I haven't worked on adding 2 classes yet. ALSO the first number (like 100) is from the dropdown.)

HTML:

<form action='http://people.brandeis.edu/~rnewman/schedule.php' method='POST'>
<div id="boxes"></div>
<br>
<input type='button' value='Add Class' id='add' style='height: 40px;'>
<input type='submit' style='height: 40px;' value='Create Schedule'>
</form>​

JQUERY:

var x = 0;
 $(document).ready(function(){
 $("#add").click(function(){

   $('#boxes').append('<input type="text" id="' + ++x + '"><br>');  //the id isn't working
  });
 });

FROM schedule.php

echo $_POST['course'];
echo "<br>";
echo $_POST['0'];
echo $_POST['1'];
echo $_POST['2'];
user176105
  • 211
  • 1
  • 4
  • 23
  • Please post your code directly in the question, instead of merely linking to another site. Thanks. –  Aug 06 '12 at 13:32
  • That jsfiddle is A LOT of spaghetti code to try to read. Can you post something a little smaller? We just need the relevant code. – Matt Aug 06 '12 at 13:32
  • Aren't you assigning the text box's ID attribute? Why not just assign the text box an alpha ID value, like "textbox"? – David Hoerster Aug 06 '12 at 13:33
  • @DavidHoerster because eventually there will be multiple textboxes. i need unique ids – user176105 Aug 06 '12 at 13:35
  • Then have a static string like "textbox" and append the value of x to the end of it. `id="textbox' + (++x) + '">` – David Hoerster Aug 06 '12 at 13:37
  • @DavidHoerster why do i even need the word textbox. All i need is a number as the `id` – user176105 Aug 06 '12 at 13:38
  • 1
    You may also want to assign the identifier to your input's `name` property, too. I'm not a PHP person, but some other frameworks look for the name property, not the ID. `id="textbox' + (++x) + ' name="textbox' + (++x) + '">` – David Hoerster Aug 06 '12 at 13:38
  • @DavidHoerster AH! that's it. i forgot to use `name` instead of `id`. make that comment an answer so i can accept it – user176105 Aug 06 '12 at 13:40
  • It's there - glad I could help! – David Hoerster Aug 06 '12 at 13:44

3 Answers3

2

I'm not a PHP person, but usually frameworks look for an element's name attribute instead of the id attribute. If you add a name attribute to your input text box, it should work:

$('#boxes').append('<input type="text" id="' + ++x + '" name="' + ++x + '"><br>');

Hope this helps!

David Hoerster
  • 28,421
  • 8
  • 67
  • 102
2

As you can see on this question on stackoverflow:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed 
by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), 
colons (":"), and periods (".").

So, in short, you can't have your id's just as numbers, and yes, as others say you should add a name attribute.

Community
  • 1
  • 1
Nikola
  • 14,888
  • 21
  • 101
  • 165
  • @user176105: I wouldn't be so harsh and just like that go and say `not true`, without the proper proof to back that up (an official one whould suffice in my book). It might work in your browser, but what about dozens of other browsers (and god forsake the older versions of 'popular' browsers)? As a rule of thumb you can't rely on something what is not defined as a standard and it's something your chose browser supports as a corner case. Trust me I had the same issue when something was working perfectly in chrome but not in firefox. – Nikola Aug 06 '12 at 18:32
  • perhaps i was a bit harsh, but the statement "ID and NAME tokens must begin with a letter" is not true. Having it work in one browser proves that statement false. – user176105 Aug 06 '12 at 22:27
  • @user176105: yes, but as I said, relaying on "working on one browser" will only bring you into trouble sooner or later with some "weird" browser. – Nikola Aug 06 '12 at 23:26
0

You should give the input a NAME attribute. That's what PHP will use to index the input.

<input type="text" id="' + ++x + '" name="myNewInputs[]" />
Scott Saunders
  • 29,840
  • 14
  • 57
  • 64