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'];
9
  • Please post your code directly in the question, instead of merely linking to another site. Thanks. Commented Aug 6, 2012 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. Commented Aug 6, 2012 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"? Commented Aug 6, 2012 at 13:33
  • @DavidHoerster because eventually there will be multiple textboxes. i need unique ids Commented Aug 6, 2012 at 13:35
  • 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) + '"> Commented Aug 6, 2012 at 13:38

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

@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.
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: yes, but as I said, relaying on "working on one browser" will only bring you into trouble sooner or later with some "weird" browser.
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[]" />

1 Comment

I just saw David Hoerster comments. His answer should be marked as the correct one, if he adds it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.